verificator
Version:
Client and server-side validation JavaScript library
22 lines (21 loc) • 686 B
JavaScript
import { REGEXP_IS_UINT } from '../constants';
import isObject from './isObject';
const set = (object, keyPath, value) => {
const path = keyPath.split('.');
const length = path.length;
const lastIndex = length - 1;
let index = -1;
let nested = object;
while (nested != null && ++index < length) {
const key = path[index];
let newValue = value;
if (index !== lastIndex) {
const objValue = nested[key];
newValue = isObject(objValue) ? objValue : (REGEXP_IS_UINT.test(path[index + 1]) ? [] : {});
}
nested[key] = newValue;
nested = nested[key];
}
return object;
};
export default set;