Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Object): add mutable set & unset variants
- Loading branch information
1 parent
76eccea
commit 0d1451e
Showing
21 changed files
with
450 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opentf/std": minor | ||
--- | ||
|
||
Added mutable set & unset variant functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Sets the value to an object at the given path. | ||
<Callout emoji="✅" type="info"> | ||
Immutable: This does not mutate the original object. | ||
</Callout> | ||
|
||
### Related | ||
|
||
- [get](/Object/get) | ||
- [set](/Object/set) | ||
- [has](/Object/has) | ||
- [unset](/Object/unset) | ||
- [toUnset](/Object/toUnset) | ||
|
||
## Syntax | ||
|
||
```ts | ||
import { toSet } from '@opentf/std'; | ||
|
||
toSet<T>( | ||
obj: T, | ||
path: string | unknown[], | ||
value: unknown | ((val: unknown) => unknown) | ||
): T | ||
``` | ||
<Callout type="info"> | ||
The value param can be either any `value` or `callback` function. | ||
</Callout> | ||
|
||
<Callout type="info"> | ||
The `callback` fn can be called with the property path value if it exist. | ||
</Callout> | ||
|
||
## Examples | ||
|
||
```ts | ||
toSet({}, 'a', null) //=> { a: null } | ||
|
||
toSet({}, 'a', 1) //=> { a: 1 } | ||
|
||
toSet({}, 'a.b', 25) //=> { a: { b: 25 } } | ||
|
||
toSet({}, 'user.email', 'user@example.com') | ||
//=> | ||
// { | ||
// user: { email: 'user@example.com' } | ||
// } | ||
|
||
toSet({}, '0', 'Apple') //=> { '0': 'Apple' } | ||
|
||
toSet({}, 'fruits[0]', 'Apple') //=> { fruits: ['Apple'] } | ||
|
||
toSet({ a: 1 }, 'a', (val) => val + 1) //=> { a: 2 } | ||
|
||
const fn = () => render('My Component') | ||
toSet({ subscribeFns: [] }, 'subscribeFns[0]', () => fn) | ||
//=> { subscribeFns: [fn] } | ||
``` | ||
|
||
## Try | ||
|
||
<REPL code={`const { toSet } = require('@opentf/std'); | ||
log(toSet({}, 'a.b', 25)); | ||
toSet({ a: 1 }, 'a', (val) => val + 1) | ||
`} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Removes the property of the given object at the given path. | ||
<Callout emoji="✅" type="info"> | ||
Immutable: This does not mutate the original object. | ||
</Callout> | ||
|
||
<Callout type="default"> | ||
If an array value is removed, then it will not return a [sparsed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#sparse_arrays) array. | ||
</Callout> | ||
|
||
### Related | ||
|
||
- [get](/Object/get) | ||
- [has](/Object/has) | ||
- [set](/Object/set) | ||
- [unset](/Object/unset) | ||
- [toSet](/Object/toSet) | ||
|
||
## Syntax | ||
|
||
```ts | ||
import { toUnset } from '@opentf/std'; | ||
|
||
toUnset<T>( | ||
obj: T, | ||
path: string | unknown[], | ||
): T | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
const obj = { a: 1, b: 2 }; | ||
toUnset(obj, 'a') //=> { b: 2 } | ||
|
||
toUnset(obj, 'b') //=> { a: 1 } | ||
|
||
toUnset(obj, 'c') //=> { a: 1, b: 2 } | ||
|
||
const arr = [1, 2, 3]; | ||
toUnset(arr, '0') //=> [2, 3] | ||
|
||
toUnset(arr, '1') //=> [1, 3] | ||
|
||
toUnset(arr, '3') //=> [1, 2, 3] | ||
|
||
const nestedObj = { x: { y: { z: ['a', null, 'b'] } } }; | ||
toUnset(nestedObj, 'x.y.z') //=> { x: { y: {} } } | ||
``` | ||
|
||
## Try | ||
|
||
<REPL code={`const { toUnset } = require('@opentf/std'); | ||
const obj = { a: 1, b: 2 }; | ||
toUnset(obj, 'a'); | ||
`} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { toSet } from '../../src'; | ||
|
||
describe('Object', () => { | ||
test('toSet', () => { | ||
expect(toSet({}, 'a', null)).toEqual({ a: null }); | ||
|
||
expect(toSet({}, 'a', 1)).toEqual({ a: 1 }); | ||
|
||
expect(toSet({}, 'a.b', 25)).toEqual({ a: { b: 25 } }); | ||
|
||
expect(toSet({}, 'user.email', 'user@example.com')).toEqual({ | ||
user: { email: 'user@example.com' }, | ||
}); | ||
|
||
const obj = { name: 'x' }; | ||
const newObj = toSet(obj, 'name', 'xxx'); | ||
expect(newObj.name).toBe('xxx'); | ||
|
||
expect(toSet({}, '0', 'Apple')).toEqual({ | ||
'0': 'Apple', | ||
}); | ||
|
||
expect(toSet({}, 'fruits[0]', 'Apple')).toEqual({ | ||
fruits: ['Apple'], | ||
}); | ||
|
||
expect(toSet({ fruits: ['Apple'] }, 'fruits[0]', 'Mango')).toEqual({ | ||
fruits: ['Mango'], | ||
}); | ||
|
||
expect(toSet({ fruits: ['Apple'] }, 'fruits[1]', 'Mango')).toEqual({ | ||
fruits: ['Apple', 'Mango'], | ||
}); | ||
|
||
expect(toSet({ a: [{ b: { c: 3 } }] }, 'a[0].b.c', 4)).toEqual({ | ||
a: [{ b: { c: 4 } }], | ||
}); | ||
}); | ||
|
||
test('updating values', () => { | ||
expect(toSet({}, 'a', () => 1)).toEqual({ a: 1 }); | ||
expect(toSet({ a: 1 }, 'a', (val) => val + 1)).toEqual({ a: 2 }); | ||
expect( | ||
toSet({ a: 1, b: [2] }, 'b', (arr) => { | ||
arr.unshift(1); | ||
return arr; | ||
}) | ||
).toEqual({ | ||
a: 1, | ||
b: [1, 2], | ||
}); | ||
const fn = (a, b) => a ** b; | ||
expect(toSet({ a: 1 }, 'b', (val) => fn)).toEqual({ a: 1, b: fn }); | ||
}); | ||
}); |
Oops, something went wrong.