slice-foo
Slices foo any way you'd like
Install
$ npm install --save slice-foo
Written in ES6 and uses generators so you'll need Node 6 or a transpiler to use it.
Usage
const sliceFoo = sliceFoo//=> ['4040', '4141', '4242', '4343'] // It's curried so you can do compose functions likeconst parseCardNumber = sliceFoo //=> ['4040', '4141', '4242', '4343'] //=> ['4040', '4141', '42'] //=> ['4040', '4141'] // easily join into the format you'd like//=> '4040 4141 4242 4343'
- sliceWith functions to specify chunk sizes dynamically, see API.
Read the tests to see how it behaves.
API
Note:
Sliceable: A type that implements .slice (eg. Array and String)
.slice
slice :: [Number] -> Sliceable -> [a]
Slices a sliceable into chunks specified by an array of numbers
const sliceFoo = sliceFoo//=> ['4040', '4141', '4242', '4343']
.sliceWith
sliceWith :: (() -> Number) -> Sliceable -> [a]
Slices a sliceable into chunks using a function
const sliceFoo = let i = 0const getChuckSize = { if i++ % 2 === 0 return 4 else return 2} sliceFoo//=> ['4040', '41', '4142', '42', '4343']
.sliceWithGenerator
sliceWithGenerator :: (function* -> Number) -> Sliceable -> [a]
Slices a sliceable into chunks using a generator
const sliceFoo = { let n = 0 while n++ < 2 4} sliceFoo//=> ['4040', '4141']
Why?
I needed a module to format strings and found most modules do too many things. It's built to be useful on it's own and composed into more specific functions. Aaand I wanted to publish my first open source project ☝️
License
MIT © Sindre Seppola