Skip to main content
Module

x/rambda/src/assocPath.js

Faster and smaller alternative to Ramda
Go to Latest
File
import {_isArray} from './_internals/_isArray'import {_isInteger} from './_internals/_isInteger'import {assoc} from './assoc'import {curry} from './curry'import {cloneList} from './_internals/cloneList'
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)