-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
362f148
commit 39f3fc1
Showing
31 changed files
with
497 additions
and
85 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/utils": minor | ||
--- | ||
|
||
Added object omit & pick util fns. |
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
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,43 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Removes all the property paths from the given object for the given list of paths. | ||
<Callout type="info"> | ||
The source object can `Plain Object` or `Array`. | ||
</Callout> | ||
|
||
## Syntax | ||
|
||
```ts | ||
import { omit } from '@opentf/utils'; | ||
|
||
omit(obj: object, ...paths: (string | unknown[])[]) | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
omit({ a: 1, b: 2 }, 'a') //=> { b: 2 } | ||
|
||
omit({ a: 1, b: 2 }, ['a']) //=> { b: 2 } | ||
|
||
omit([1, 2, 3], '1') //=> [1, 3] | ||
|
||
omit([1, 2, 3], 2) //=> [1, 2] | ||
|
||
omit({ a: 1, b: 2, c: 3, d: 4 }, 'b', 'd') | ||
//=> { | ||
// a: 1, | ||
// c: 3, | ||
// } | ||
|
||
obj = { a: { b: [1, 3, 5] } }; | ||
omit(obj, 'a.b[2]') //=> { a: { b: [1, 3] } }); | ||
``` | ||
## Try | ||
|
||
<REPL code={`const { omit } = require('@opentf/utils'); | ||
omit({ a: 1, b: 2 }, '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Includes all the property paths from the given object for the given list of paths. | ||
<Callout type="info"> | ||
The source object can `Plain Object` or `Array`. | ||
</Callout> | ||
|
||
## Syntax | ||
|
||
```ts | ||
import { pick } from '@opentf/utils'; | ||
|
||
pick(obj: object, ...paths: (string | unknown[])[]) | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
pick({ a: 1, b: 2 }, 'a') //=> { a: 1 } | ||
|
||
pick([1, 2, 3], '0') //=> [1] | ||
|
||
pick({ a: { b: { c: 1 }, d: null, e: [10, 20, 30] } }, 'a.b', ['a', 'e', '2']) | ||
//=> { | ||
// a: { b: { c: 1 } }, | ||
// e: [30] | ||
// } | ||
``` | ||
## Try | ||
|
||
<REPL code={`const { pick } = require('@opentf/utils'); | ||
pick({ a: 1, b: 2 }, '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,62 @@ | ||
import { omit } from '../../src'; | ||
|
||
describe('Object > omit', () => { | ||
test('remove a prop', () => { | ||
expect(omit({ a: 1, b: 2 }, 'a')).toEqual({ b: 2 }); | ||
expect(omit({ a: 1, b: 2 }, ['a'])).toEqual({ b: 2 }); | ||
expect(omit({ 'a.b': 1, b: 2 }, ['a.b'])).toEqual({ b: 2 }); | ||
}); | ||
|
||
test('remove multiple props', () => { | ||
expect(omit({ a: 1, b: 2, c: 3, d: 4 }, 'b', 'd')).toEqual({ | ||
a: 1, | ||
c: 3, | ||
}); | ||
expect(omit({ a: 1, b: 2, c: 3, d: 4 }, 'a', 'b', 'c')).toEqual({ d: 4 }); | ||
expect(omit({ 'a.b': 1, b: 2 }, ['a.b'], 'b')).toEqual({}); | ||
}); | ||
|
||
test('remove a deep prop', () => { | ||
expect(omit({ a: { b: { c: 1 } } }, 'a.b.c')).toEqual({ a: { b: {} } }); | ||
}); | ||
|
||
test('remove multiple deep props', () => { | ||
expect( | ||
omit({ a: { b: { c: 1, d: 2, e: 5, f: 6 } } }, 'a.b.c', 'a.b.d', 'a.b.e') | ||
).toEqual({ a: { b: { f: 6 } } }); | ||
}); | ||
|
||
test('remove an index from array', () => { | ||
expect(omit([1, 2, 3], ['0'])).toEqual([2, 3]); | ||
expect(omit([1, 2, 3], ['1'])).toEqual([1, 3]); | ||
expect(omit([1, 2, 3], ['2'])).toEqual([1, 2]); | ||
expect(omit([1, 2, 3], ['3'])).toEqual([1, 2, 3]); | ||
expect(omit([1, 2, 3], ['-1'])).toEqual([1, 2, 3]); | ||
}); | ||
|
||
test('remove multiple indexes from array', () => { | ||
expect(omit([1, 2, 3], '1', '2')).toEqual([1]); | ||
}); | ||
|
||
test('remove array items from nested deep obj', () => { | ||
let obj = { a: [1, 3, 5] }; | ||
expect(omit(obj, 'a.[0]')).toEqual({ a: [3, 5] }); | ||
|
||
obj = { a: { b: [1, 3, 5] } }; | ||
expect(omit(obj, 'a.b[2]')).toEqual({ a: { b: [1, 3] } }); | ||
|
||
obj = { a: { b: { c: [1, 3, 5] } } }; | ||
expect(omit(obj, 'a.b.c[1]')).toEqual({ a: { b: { c: [1, 5] } } }); | ||
|
||
obj = { | ||
a: { | ||
b: { c: [1, 3, 5, { d: { e: [10, 20, 40] } }], f: { g: [1, 2, 3] } }, | ||
}, | ||
}; | ||
expect( | ||
omit(obj, ['a', 'b', 'c', 3], ['a', 'b', 'f', 'g', 0], 'a.b.f.g[2]') | ||
).toEqual({ | ||
a: { b: { c: [1, 3, 5], f: { g: [2] } } }, | ||
}); | ||
}); | ||
}); |
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,82 @@ | ||
import { pick } from '../../src'; | ||
|
||
describe('Object > pick', () => { | ||
test('invalid', () => { | ||
expect(pick()).toEqual({}); | ||
expect(pick(null)).toEqual({}); | ||
expect(pick(null, '')).toEqual({}); | ||
expect(pick({})).toEqual({}); | ||
expect(pick({}, 'a')).toEqual({}); | ||
expect(pick({ a: 1 }, 'b')).toEqual({}); | ||
expect(pick([])).toEqual([]); | ||
expect(pick([], '0')).toEqual([]); | ||
expect(pick([1], '1')).toEqual([]); | ||
}); | ||
|
||
test('pick a first level prop', () => { | ||
expect(pick({ a: 1, b: 2 }, 'a')).toEqual({ a: 1 }); | ||
expect(pick({ a: 1, b: 2 }, 'b')).toEqual({ b: 2 }); | ||
expect(pick({ 'a.b': 1, 'a.c': 2 }, ['a.c'])).toEqual({ 'a.c': 2 }); | ||
expect(pick([1, 2, 3], '0')).toEqual([1]); | ||
expect(pick([1, 2, 3], 2)).toEqual([3]); | ||
expect(pick({ '0': 'a', '1': 'b' }, 0)).toEqual({ '0': 'a' }); | ||
}); | ||
|
||
test('pick first level multiple props', () => { | ||
expect(pick({ a: 1, b: 2, c: 3 }, 'a', 'b')).toEqual({ a: 1, b: 2 }); | ||
expect(pick({ a: 1, b: 2, c: 3 }, 'b', 'c')).toEqual({ b: 2, c: 3 }); | ||
expect(pick([1, 2, 3], '0', '2')).toEqual([1, 3]); | ||
expect(pick([1, 2, 3], '1', 3)).toEqual([2]); | ||
}); | ||
|
||
test('pick second level props', () => { | ||
expect(pick({ a: { b: 2 } }, 'a.b')).toEqual({ a: { b: 2 } }); | ||
expect(pick({ a: { b: 2, c: 3 } }, 'a.c')).toEqual({ a: { c: 3 } }); | ||
expect( | ||
pick({ a: { b: { c: 1, d: [1, 2, 3], e: null } } }, 'a.b.d[2]', [ | ||
'a', | ||
'b', | ||
'e', | ||
]) | ||
).toEqual({ a: { b: { d: [3], e: null } } }); | ||
expect( | ||
pick({ a: { b: { c: 1 }, d: null, e: [10, 20, 30] } }, 'a.b', [ | ||
'a', | ||
'e', | ||
'2', | ||
]) | ||
).toEqual({ a: { b: { c: 1 }, e: [30] } }); | ||
}); | ||
|
||
test('pick deep level props', () => { | ||
expect(pick({ a: { b: { c: 1 } } }, 'a')).toEqual({ | ||
a: { b: { c: 1 } }, | ||
}); | ||
|
||
expect(pick({ a: { b: { c: 1 }, d: null } }, 'a.b')).toEqual({ | ||
a: { b: { c: 1 } }, | ||
}); | ||
|
||
expect(pick({ a: { b: { c: 1 } } }, 'a.b.c')).toEqual({ | ||
a: { b: { c: 1 } }, | ||
}); | ||
|
||
expect( | ||
pick( | ||
{ a: { b: { c: 1, d: 3 } }, a2: { a2b1: 5, a2b2: 6 } }, | ||
'a.b.c', | ||
['a', 'b', 'd'], | ||
'a2.a2b2', | ||
['a2', 'a2b1'] | ||
) | ||
).toEqual({ | ||
a: { b: { c: 1, d: 3 } }, | ||
a2: { a2b1: 5, a2b2: 6 }, | ||
}); | ||
}); | ||
|
||
test('pick whole object with partial paths', () => { | ||
const obj = { a: { b: { c: 3, d: 4, e: 5 } } }; | ||
expect(pick(obj, 'a', 'a.b.c', ['a', 'b', 'e'])).toEqual(obj); | ||
}); | ||
}); |
Oops, something went wrong.