UNPKG

zippa

Version:
76 lines (64 loc) 2.56 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.walk = exports.preWalk = exports.postWalk = undefined; var _curry = require('ramda/src/curry'); var _curry2 = _interopRequireDefault(_curry); var _visit = require('./visit'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // @flow var makeStatelessVisitor = function makeStatelessVisitor(fn) { return function (item, _) { return { item: fn(item) }; }; }; /** * Walks the data structure in depth-first order, applying * the function after the item's subtree has been walked. * * Returns a new data structure of modified items, or the original * zipper if the structure wasn't modified. * * @param {Function} fn - function applied to each item after it's subtree was walked * @param {Zipper} zipper - A Zipper value to walk * @return {Zipper} */ var postWalk = exports.postWalk = (0, _curry2.default)(function (fn, zipper) { return (0, _visit.visit)([(0, _visit.onPost)(makeStatelessVisitor(fn))], undefined, zipper).zipper; }); /** * Walks the data structure in depth-first order, applying * the function before the item's subtree has been walked. * * Returns a new data structure of modified items, or the original * zipper if the structure wasn't modified. * * @param {Function} fn - function applied to each item before it's subtree is walked * @param {Zipper} zipper - A Zipper value to walk * @return {Zipper} */ var preWalk = exports.preWalk = (0, _curry2.default)(function (fn, zipper) { return (0, _visit.visit)([(0, _visit.onPre)(makeStatelessVisitor(fn))], undefined, zipper).zipper; }); /** * Walks the data structure in depth-first order, * applying inner and outer functions before and after (respectively) each * item's subtree is walked. * * Returns a new data structure from modified items, or the original * zipper if the structure wasn't modified. * * @param {Function} inner - function applied to each item before it's subtree is walked * @param {Function} outer function applied to each item after it's subtree was walked * @param {Zipper} zipper - A Zipper value to walk * @return {Zipper} */ var walk = exports.walk = (0, _curry2.default)(function (innerFn, outerFn, zipper) { return (0, _visit.visit)([(0, _visit.onPre)(makeStatelessVisitor(innerFn)), (0, _visit.onPost)(makeStatelessVisitor(outerFn))], undefined, zipper).zipper; }); exports.default = { walk: walk, postWalk: postWalk, preWalk: preWalk };