> 💡 This is a fork of the main [updeep](https://github.com/substantial/updeep) package. For ease of reading — not to mention ease of shamelessly lifting large pieces of the original documentation — in this documentation all mentions of `updeep` refers to this fork.
Because of this, everything returned by updeep is frozen. Not only that, but
updeep assumes that every object passed in to update is immutable, so it may
freeze objects passed in as well. Note that the freezing only happens in
development.
This fork of updeep requires Remeda, but works very well with any other utility function ([lodash], [Ramda], etc).
## Differences with the original Updeep
- Under the hood, the use of lodash has
been replaced by Remeda (for better type support and tree-shaking abilities).
- The codebase has been ported to TypeScript (mostly for the lulz).
- The order of parameters in the non-curryied invocation of functions has been modified. In the original updeep the input object is the last parameter, whereas here it's the first.
```js
// original updeep
const dataIn = { a: 1, b: 2 };
let dataOut = u({ c: 3 }, dataIn); // simple call
dataOut = u({ c: 3 })(dataIn); // curried
// updeep-remeda
dataOut = u(dataIn, { c: 3 }); // simple call
dataOut = u({ c: 3 })(dataIn); // curried
```
-`withDefault` has been removed as the behavior can be implemented using
Remeda's `pipe`, or a simple `??`.
-`u.omitted` has been renamed `u.skip`.
## Installation
```bash
$ npm install @yanick/updeep-remeda
# or
$ pnpm install @yanick/updeep-remeda
```
## Full example
```js
import u from "@yanick/updeep-remeda";
const person = {
name: { first: "Bill", last: "Sagat" },
children: [
{ name: "Mary-Kate", age: 7 },
{ name: "Ashley", age: 7 },
],
todo: ["Be funny", "Manage household"],
email: "bill@example.com",
version: 1,
};
const inc = (i) => i + 1;
const eq = (x) => (y) => x === y;
const newPerson = u(person, {
// Change first name
name: { first: "Bob" },
// Increment all children's ages
children: u.map({ age: inc }),
// Update email
email: "bob@example.com",
// Remove todo
todo: u.reject(eq("Be funny")),
// Increment version
version: inc,
});
// => {
// name: { first: 'Bob', last: 'Sagat' },
// children: [
// { name: 'Mary-Kate', age: 8 },
// { name: 'Ashley', age: 8 }
// ],
// todo: [
// 'Manage household'
// ],
// email: 'bob@example.com',
// version: 2
//}
```
## API
> 💡 All functions are curried, Remeda-style, so if you see `f(dataIn, ...others)`, it can be called with either `f(dataIn, ...others)` or `f(...others)(dataIn)`.
### Importing
`updeep-remeda` exports a default function that is an alias to `u.update` and
import { updateIn, omit } from '@yanick/updeep-remeda';
```
### `u(dataIn, updates)`
### `u.update(dataIn, updates)`
Update as many values as you want, as deeply as you want. The `updates` parameter can either be an object, a function, or a value. Everything returned from `u` is frozen recursively.
If `updates` is an object, for each key/value, it will apply the updates specified in the value to `object[key]`.
If `updates` is a function, it will call the function with `object` and return the value.
If `updates` is a value, it will return that value.
Sometimes, you may want to set an entire object to a property, or a function. In that case, you'll need to use a function to return that value, otherwise it would be interpreted as an update. Ex. `function() { return { a: 0 }; }`.
Non-trivial array manipulations, such as element removal/insertion/sorting, can be implemented with functions. Because there are so many possible manipulations, we don't provide any helpers and leave this up to you. Simply ensure your function is pure and does not mutate its arguments.
Freeze your initial state to protect against mutations. Only performs the freezing in development, and returns the original object unchanged in production.
```js
const state = u.freeze({ someKey: "Some Value" });
state.someKey = "Mutate"; // ERROR in development
```
### `u.updateIn(dataIn, path, value)`
Update a single value with a simple string or array path. Can be use to update nested objects, arrays, or a combination. Can also be used to update every element of a nested array with `'*'`.