UNPKG

object-hierarchy-access

Version:

Get/Set value from/to JS object hierarchy properties

78 lines (77 loc) 2.73 kB
import { setupIfUndef } from './setup'; function _parseArgs(others) { var value = others[others.length - 1]; var rest = Array.prototype.concat.apply([], others.slice(0, -1)); // exclude `value` var hierarchies = rest.slice(0, -1); var prop = rest[rest.length - 1]; return { hierarchies: hierarchies, prop: prop, value: value }; } function set(optionalTarget) { var others = []; for (var _i = 1; _i < arguments.length; _i++) { others[_i - 1] = arguments[_i]; } var _a = _parseArgs(others), hierarchies = _a.hierarchies, prop = _a.prop, value = _a.value; var target = optionalTarget || {}; var current = setupIfUndef(target, hierarchies); current[prop] = value; return target; } function assign(target) { var others = []; for (var _i = 1; _i < arguments.length; _i++) { others[_i - 1] = arguments[_i]; } var _a = _parseArgs(others), hierarchies = _a.hierarchies, prop = _a.prop, value = _a.value; var current = setupIfUndef(target, hierarchies); current[prop] = value; return current; } function put(target) { var others = []; for (var _i = 1; _i < arguments.length; _i++) { others[_i - 1] = arguments[_i]; } var _a = _parseArgs(others), hierarchies = _a.hierarchies, prop = _a.prop, value = _a.value; var current = setupIfUndef(target, hierarchies); current[prop] = value; return value; } function setIfUndef(optionalTarget) { var others = []; for (var _i = 1; _i < arguments.length; _i++) { others[_i - 1] = arguments[_i]; } var _a = _parseArgs(others), hierarchies = _a.hierarchies, prop = _a.prop, value = _a.value; var target = optionalTarget || {}; var current = setupIfUndef(target, hierarchies); if (current[prop] === undefined) { current[prop] = value; } return target; } function assignIfUndef(target) { var others = []; for (var _i = 1; _i < arguments.length; _i++) { others[_i - 1] = arguments[_i]; } var _a = _parseArgs(others), hierarchies = _a.hierarchies, prop = _a.prop, value = _a.value; var current = setupIfUndef(target, hierarchies); if (current[prop] === undefined) { current[prop] = value; } return current; } function putIfUndef(target) { var others = []; for (var _i = 1; _i < arguments.length; _i++) { others[_i - 1] = arguments[_i]; } var _a = _parseArgs(others), hierarchies = _a.hierarchies, prop = _a.prop, value = _a.value; var current = setupIfUndef(target, hierarchies); if (current[prop] === undefined) { current[prop] = value; } return current[prop]; } export { set, assign, put, setIfUndef, assignIfUndef, putIfUndef };