slice-foo
Version:
Slices foo any way you'd like
47 lines (33 loc) • 1.13 kB
JavaScript
var _require = require('./utils.js');
var curry = _require.curry;
var singles = _require.singles;
var singlesGenerator = _require.singlesGenerator;
var slicer = function slicer(i, nextFn, str) {
var n = nextFn();
if (!n || str.length === i) return [];
if (str.length < i + n) return [str.slice(i, i + n)];
return [str.slice(i, i + n)].concat(slicer(i + n, nextFn, str));
};
var generatorSlicer = function generatorSlicer(i, nextGen, str) {
var _nextGen$next = nextGen.next();
var n = _nextGen$next.value;
var done = _nextGen$next.done;
if (done || str.length === i) return [];
if (str.length < i + n) return [str.slice(i, i + n)];
return [str.slice(i, i + n)].concat(generatorSlicer(i + n, nextGen, str));
};
var slice = curry(function (ns, str) {
return slicer(0, singles(ns), str);
});
var sliceWith = curry(function (fn, str) {
return slicer(0, fn, str);
});
var sliceWithGenerator = curry(function (generator, str) {
return generatorSlicer(0, generator(), str);
});
module.exports = {
slice: slice,
sliceWith: sliceWith,
sliceWithGenerator: sliceWithGenerator
};
;