blob: 68a8ba4b2ff027ddd229eb35b8e333783b33e7bf [file] [log] [blame] [edit]
import { cloneList } from './_internals/cloneList.js'
import { isArray } from './_internals/isArray.js'
import { isInteger } from './_internals/isInteger.js'
import { assoc } from './assoc.js'
import { curry } from './curry.js'
function assocPathFn(
path, newValue, input
){
const pathArrValue =
typeof path === 'string' ?
path.split('.').map(x => isInteger(Number(x)) ? Number(x) : x) :
path
if (pathArrValue.length === 0){
return newValue
}
const index = pathArrValue[ 0 ]
if (pathArrValue.length > 1){
const condition =
typeof input !== 'object' ||
input === null ||
!input.hasOwnProperty(index)
const nextInput = condition ?
isInteger(pathArrValue[ 1 ]) ?
[] :
{} :
input[ index ]
newValue = assocPathFn(
Array.prototype.slice.call(pathArrValue, 1),
newValue,
nextInput
)
}
if (isInteger(index) && isArray(input)){
const arr = cloneList(input)
arr[ index ] = newValue
return arr
}
return assoc(
index, newValue, input
)
}
export const assocPath = curry(assocPathFn)