UNPKG

moltres-utils

Version:
101 lines (83 loc) 2.85 kB
"use strict"; require("core-js/modules/es6.object.define-property"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/web.dom.iterable"); require("core-js/modules/es6.array.iterator"); require("core-js/modules/es6.string.iterator"); require("core-js/modules/es6.set"); var _concat = _interopRequireDefault(require("./concat")); var _curry = _interopRequireDefault(require("../common/curry")); var _isObjectLike = _interopRequireDefault(require("../lang/isObjectLike")); var _pipe = _interopRequireDefault(require("../common/pipe")); var _reduce = _interopRequireDefault(require("./reduce")); var _resolve = _interopRequireDefault(require("../common/resolve")); var _walk = _interopRequireDefault(require("./walk")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var reduceWalkee = function reduceWalkee() { var visited = new Set(); return function (accum, value, keys, iteratee, recur) { var resolvedValue = (0, _resolve.default)(value); return (0, _pipe.default)(function (result) { if ((0, _isObjectLike.default)(resolvedValue) && !visited.has(resolvedValue)) { visited.add(resolvedValue); return (0, _reduce.default)(function (accumResult, child, childKdx) { var newKeys = (0, _concat.default)(keys, [childKdx]); return recur(accumResult, child, newKeys, iteratee); }, result, resolvedValue); } return result; }, function (result) { return iteratee(result, value, keys); })(accum); }; }; /** * Walk depth first and reduce using the given reducer function * * NOTE: This method will resolve values during the walk before iterating and walking them. * * @function * @since v0.0.4 * @category data * @param {Function} iteratee The iterator function. Receives three values, the accumulator and the current element from the walk and the current set of keys from the entire depth of the walk. * @param {*} accum The accumulator value. * @param {*} collection The collection to walk. * @returns {*} The final, accumulated value. * @example * * walkReduceDepthFirst( * (accum, value, keys) => { * accum.push(keys) * return accum * }, * [], * { * a: { * b: { * c: 'c' * }, * d: 'd', * }, * e: [ 'e', 'f' ] * } * ) * //=> [ * [ 'a', 'b', 'c' ], * [ 'a', 'b' ], * [ 'a', 'd' ], * [ 'a' ], * [ 'e', 0 ], * [ 'e', 1 ], * [ 'e' ], * [] * ] */ var walkReduceDepthFirst = (0, _curry.default)(function (iteratee, accum, collection) { return (0, _walk.default)(reduceWalkee(), iteratee, accum, collection, []); }); var _default = walkReduceDepthFirst; exports.default = _default; //# sourceMappingURL=walkReduceDepthFirst.js.map