-
Notifications
You must be signed in to change notification settings - Fork 86
/
dissocPath.js
47 lines (41 loc) · 1.47 KB
/
dissocPath.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
import { createPath } from '../src/_internals/createPath.js'
import { isArray } from './_internals/isArray.js'
import { isIndexInteger } from './_internals/isInteger.js'
import { omit } from './omit.js'
import { path } from './path.js'
import { removeIndex } from './removeIndex.js'
import { update } from './update.js'
export function dissocPath(pathInput, input){
if (arguments.length === 1) return _obj => dissocPath(pathInput, _obj)
const pathArrValue = createPath(pathInput)
// this {...input} spread could be done to satisfy ramda specs, but this is done on so many places
// TODO: add warning that Rambda simply returns input if path is empty
if (pathArrValue.length === 0) return input
const pathResult = path(pathArrValue, input)
if (pathResult === undefined) return input
const index = pathArrValue[ 0 ]
const condition =
typeof input !== 'object' ||
input === null ||
!input.hasOwnProperty(index)
if (pathArrValue.length > 1){
const nextInput = condition ?
isIndexInteger(pathArrValue[ 1 ]) ?
[] :
{} :
input[ index ]
const nextPathInput = Array.prototype.slice.call(pathArrValue, 1)
const intermediateResult = dissocPath(
nextPathInput, nextInput, input
)
if (isArray(input)) return update(
index, intermediateResult, input
)
return {
...input,
[ index ] : intermediateResult,
}
}
if (isArray(input)) return removeIndex(index, input)
return omit([ index ], input)
}