deno.land / x / rambda@v9.1.1 / files / waiting / deletePath.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
/*Method: deletePath
Explanation: It removes property path from an object.
Example:
```const input = {a: 1, b:{c:2, d:3}}const expected = {a: 1, b:{c:2}}const result = R.deletePath('a.d', input)// => result === expected```
Categories: Object
Notes:
export function deletePath<Output>(path: Path, obj: unknown): Outputexport function deletePath<Output>( path: Path,): (obj: unknown) => Output*/
import { createPath } from '../../source/_internals/createPath.js'import { assocPath } from '../../source/assocPath.js'import { path as pathModule } from '../../source/path.js'
function removeProperty(prop, obj){ const toReturn = {}
Object.keys(obj).forEach(key => { if (key === prop) return toReturn[ key ] = obj[ key ] })
return toReturn}
export function deletePath(pathInput, obj){ if (arguments.length === 1){ return _obj => deletePath(pathInput, _obj) } const path = createPath(pathInput) if (path.length === 0){ return obj } if (path.length === 1){ return removeProperty(path[ 0 ], obj) } const lastIndex = path.length - 1 const newPath = path.filter((item, i) => i !== lastIndex) const found = pathModule(newPath, obj) if (!found) return obj
const newValue = deletePath(path[ lastIndex ], found)
return assocPath( newPath, newValue, obj )}
rambda

Version Info

Tagged at
a year ago