UNPKG

moltres-utils

Version:
84 lines (62 loc) 3.48 kB
"use strict"; require("core-js/modules/es6.object.define-property"); Object.defineProperty(exports, "__esModule", { value: true }); exports.baseSet = exports.default = void 0; require("core-js/modules/es7.symbol.async-iterator"); require("core-js/modules/es6.symbol"); require("core-js/modules/web.dom.iterable"); require("core-js/modules/es6.array.is-array"); var _assoc = require("./assoc"); var _allWith = _interopRequireDefault(require("../common/allWith")); var _curry = _interopRequireDefault(require("../common/curry")); var _isFunction = _interopRequireDefault(require("../lang/isFunction")); var _isMap = _interopRequireDefault(require("../lang/isMap")); var _isWeakMap = _interopRequireDefault(require("../lang/isWeakMap")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var baseSet = function baseSet(selector, value, collection) { if (collection != null && (0, _isFunction.default)(collection.set) && !(0, _isMap.default)(collection) && !(0, _isWeakMap.default)(collection)) { return collection.set(selector, value); } return (0, _assoc.baseAssoc)(selector, value, collection); }; /** * This method is an alias for `assoc` * * Makes a shallow clone of an object, setting or overriding the specified property with the given value. Note that this copies and flattens prototype properties onto the new object as well. All non-primitive properties are copied by reference. * * Supports path based property selectors 'foo.bar' and functional selectors which performs an 'over' on the entire collection and sets each matching selector to the given value. * * dispatches to the `set` method of the 3rd argument if available * * @function * @since v0.0.3 * @category data * @sig String -> a -> {k: v} -> {k: v} * @param {Array | String | Function} selector The property path to set or functional selector * @param {*} value The new value * @param {*} collection The collection to clone and assign the new value * @returns {*} A new collection equivalent to the original except for the changed selector path. * @example * * set('c', 3, {a: 1, b: 2}) //=> {a: 1, b: 2, c: 3} * set('c.d', 3, {a: 1, b: 2}) //=> {a: 1, b: 2, c: { d: 3 }} * set([ 'c', 'd' ], 3, {a: 1, b: 2}) //=> {a: 1, b: 2, c: { d: 3 }} */ exports.baseSet = baseSet; var set = (0, _curry.default)(function (selector, value, collection) { return (0, _allWith.default)(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), resolvedSelector = _ref2[0], resolvedCollection = _ref2[1]; return baseSet(resolvedSelector, value, resolvedCollection); }, [selector, collection]); }); var _default = set; exports.default = _default; //# sourceMappingURL=set.js.map