You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+82-3Lines changed: 82 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,28 @@
4
4
5
5
`mergePartially` is a convenience method for overwriting only the values you want
6
6
7
-
## I see lots of TypeScript stuff. Can I use this in JavaScript too?
7
+
<!-- toc -->
8
8
9
-
Yes. Even though the examples are in TypeScript (since it helps to illustrate the problem that `mergePartially` solves), you can just remove the type annotations when using `mergePartially`.
9
+
-[Design Goals](#design-goals)
10
+
-[Why would you want to use this:](#why-would-you-want-to-use-this)
11
+
*[First, let's try to write the flexible factory function without mergePartially](#first-lets-try-to-write-the-flexible-factory-function-without-mergepartially)
12
+
*[Now let's refactor using mergePartially](#now-lets-refactor-using-mergepartially)
*[Why wouldn't I just use Object.assign or the spread operator?](#why-wouldnt-i-just-use-objectassign-or-the-spread-operator)
16
+
*[I see lots of TypeScript stuff. Can I use this in JavaScript too?](#i-see-lots-of-typescript-stuff-can-i-use-this-in-javascript-too)
17
+
*[What's the difference between .deep and .shallow?](#whats-the-difference-between-deep-and-shallow)
18
+
*[Why is `.shallow` even necessary?](#why-is-shallow-even-necessary)
19
+
*[Why is my return type some strange error string?](#why-is-my-return-type-some-strange-error-string)
20
+
-[Contributions](#contributions)
21
+
22
+
<!-- tocstop -->
23
+
24
+
## Design Goals
25
+
26
+
1. the resulting object will always be the same type/`interface` as the seed object
27
+
2. it will always be “Typescript first” so you know the type definitions will not differ at runtime (like many of this library's competitors)
28
+
3. all PRs should allow consumers of the library to feel confident to use this library in production and bullet-proof testing scenarios. High code-coverage percentages gaurantee this.
10
29
11
30
## Why would you want to use this:
12
31
@@ -70,7 +89,7 @@ Wow look how much fewer lines and characters we have to write to accomplish the
function makeFakeUser(overrides?:NestedPartial<IUser>):IUser {
73
-
returnmergePartially(
92
+
returnmergePartially.deep(
74
93
{
75
94
id: 1,
76
95
age: 42,
@@ -85,3 +104,63 @@ function makeFakeUser(overrides?: NestedPartial<IUser>): IUser {
85
104
## Examples
86
105
87
106
See [the unit tests](https://github.com/dgreene1/merge-partially/blob/master/src/index.spec.ts) for various examples.
107
+
108
+
## F.A.Q. / Troubleshooting
109
+
110
+
### Why wouldn't I just use Object.assign or the spread operator?
111
+
112
+
These two functions have different goals. `Object.assign` can merge two different types into a combination type. `mergePartially` always returns the same type as the seed object. That's one of many reasons why `mergePartially` is safer than `Object.assign`.
113
+
114
+
### I see lots of TypeScript stuff. Can I use this in JavaScript too?
115
+
116
+
Yes. Even though the examples are in TypeScript (since it helps to illustrate the problem that `mergePartially` solves), you can just remove the type annotations when using `mergePartially`.
117
+
118
+
### What's the difference between .deep and .shallow?
119
+
120
+
- The main difference is that `.deep` allows you to pass multiple levels of partially supplied objects but `.shallow` only allows partial objects at the first level.
121
+
- On a more technical level, `.deep` allows you to pass in `NestedPartial<T>` as where `.shallow` only accepts `Partial<T>`
c: 'I had to supply a value for c here but I did not have to supply it in .deep',
150
+
d: 'new d',
151
+
},
152
+
},
153
+
});
154
+
```
155
+
156
+
### Why is `.shallow` even necessary?
157
+
158
+
There are some data types that are "less-compatible" with the library and therefore require a workaround ([click here for the description](https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md)). It should be rare that you need to use `.shallow`, but you might prefer `.shallow` over `.deep` anyway for explicitness.
159
+
160
+
### Why is my return type some strange error string?
161
+
162
+
In order to meet the design goals (see above), mergePartially proactively prevents certain data combinations. See this link for more information on the soluton: [https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md](https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md)
163
+
164
+
## Contributions
165
+
166
+
PRs are welcome. To contribute, please either make a Github issue or find one you'd like to work on, then fork the repo to make the change.
0 commit comments