-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathomit.js
83 lines (80 loc) · 2.08 KB
/
omit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const isPromise = require('./_internal/isPromise')
const deleteByPath = require('./_internal/deleteByPath')
const copyDeep = require('./_internal/copyDeep')
const curry2 = require('./_internal/curry2')
const __ = require('./_internal/placeholder')
// _omit(source Object, paths Array<string>) -> result Object
const _omit = function (source, paths) {
const pathsLength = paths.length,
result = copyDeep(source)
let pathsIndex = -1
while (++pathsIndex < pathsLength) {
deleteByPath(result, paths[pathsIndex])
}
return result
}
/**
* @name omit
*
* @synopsis
* ```coffeescript [specscript]
* omit(paths Array<string>)(source Object) -> omitted Object
*
* omit(source Object, paths Array<string>) -> omitted Object
* ```
*
* @description
* Create a new object by excluding provided paths on a source object.
*
* ```javascript [playground]
* console.log(
* omit({ _id: '1', name: 'George' }, ['_id']),
* ) // { name: 'George' }
* ```
*
* `omit` supports three types of path patterns for nested property access
*
* * dot delimited - `'a.b.c'`
* * bracket notation - `'a[0].value'`
* * an array of keys or indices - `['a', 0, 'value']`
*
* ```javascript [playground]
* console.log(
* omit(['a.b.d'])({
* a: {
* b: {
* c: 'hello',
* d: 'goodbye',
* },
* },
* }),
* ) // { a: { b: { c: 'hello' } } }
* ```
*
* Compose `omit` inside a `pipe` with its lazy API
*
* ```javascript [playground]
* pipe({ a: 1, b: 2, c: 3 }, [
* map(number => number ** 2),
* omit(['a', 'b']),
* console.log, // { c: 9 }
* ])
* ```
*
* Any promises passed in argument position are resolved for their values before further execution. This only applies to the eager version of the API.
*
* ```javascript [playground]
* omit(Promise.resolve({ a: 1, b: 2, c: 3 }), ['a', 'b']).then(console.log)
* // { c: 3 }
* ```
*/
const omit = function (arg0, arg1) {
if (arg1 == null) {
return curry2(_omit, __, arg0)
}
if (isPromise(arg0)) {
return arg0.then(curry2(_omit, __, arg1))
}
return _omit(arg0, arg1)
}
module.exports = omit