moltres-utils
Version:
Utils for Moltres apps
87 lines (72 loc) • 2.6 kB
JavaScript
require("core-js/modules/es6.object.define-property");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _append = _interopRequireDefault(require("./append"));
var _castPath = _interopRequireDefault(require("./castPath"));
var _curry = _interopRequireDefault(require("../common/curry"));
var _isObject = _interopRequireDefault(require("../lang/isObject"));
var _pipe = _interopRequireDefault(require("../common/pipe"));
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(pathParts, accum, value, keys, iteratee, recur) {
return (0, _pipe.default)(function (result) {
return iteratee(result, value, keys);
}, function (result) {
var resolvedValue = (0, _resolve.default)(value);
if (pathParts.length > keys.length && (0, _isObject.default)(resolvedValue)) {
var nextKey = pathParts[keys.length];
var newKeys = (0, _append.default)(nextKey, keys);
value = (0, _resolve.default)(value);
return recur(pathParts, result, resolvedValue[nextKey], newKeys, iteratee);
}
return result;
})(accum);
};
/**
* Walk reduce the specific path using the given reducer function
*
* NOTE: This method will resolve values during the walk before walking them. However, the unresolved value will be delivered to the iteratee.
*
* @function
* @since v0.0.6
* @category data
* @param {*} path The specific path to walk
* @param {Function} fn 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
*
* walkReducePath(
* (accum, value, keys) => {
* return accum.push(keys)
* },
* 'a.c.d'
* [],
* {
* a: {
* b: 'b',
* c: {
* d: 'd'
* }
* },
* e: [ 'e', 'f' ]
* }
* )
* //=> [
* // [],
* // ['a'],
* // ['a', 'c'],
* // ['a', 'c', 'd']
* // ]
*/
var walkReducePath = (0, _curry.default)(function (iteratee, path, accum, collection) {
return (0, _walk.default)(reduceWalkee, iteratee, (0, _castPath.default)(path), accum, collection, []);
});
var _default = walkReducePath;
exports.default = _default;
//# sourceMappingURL=walkReducePath.js.map
;