fandy 👋
A set of JavaScript utility functions I find myself needing frequently.
None of these are functions you can't find elsewhere, but I often find myself needing some of these, and not wanting to add a large dependency like lodash to my project.
This project is also a chance for me to practice setting up CI, full test coverage and a real README for the first time.
Feel free to make pull requests.
Install
npm install fandy --save
Install dependencies
npm install
Run tests
npm test
Functions
pipe(...fns)
Returns a function which returns the result of its arguments being passed one by one through fns
const first = x + 1const second = x * 2 const result = 1 // result = 4
split(string, token)
Returns an array consisting of string
split on token
const string = 'I-am-a-string'const result = // result = ['I', 'am', 'a', 'string']
uniq(array)
Returns a new array consisting of array
with all duplicates removed.
const array = 'Bob' 'James' 'Bob'const result = // result = ['Bob', 'James']
zip(keys, ...arrays)
Returns an array objects created by zipping each array
with keys
const firstNames = 'Bob' 'James' 'Tim'const lastNames = 'Builder' 'Rocket' 'Tiny'const cities = 'London' 'Paris'const keys = 'firstName' 'lastName' 'currentCity'const result = // result = [// { firstName: 'Bob', lastName: 'Builder', currentCity: 'London' },// { firstName: 'James', lastName: 'Rocket', currentCity: 'Paris' },// { firstName: 'Tim', lastName: 'Tiny', currentCity: undefined }// ]
last(array, index = 1)
Returns the index
-th item from the end of array
const array = 'Bob' 'James' 'Robert'const result = // result = 'Robert' const array = 'Bob' 'James' 'Robert'const result = // result = 'James'
removeAt(array, index)
Returns a new array consisting of array
with element at index
removed
const array = 'Bob' 'James' 'Robert'const result = // result = ['Bob', 'Robert']
switcher(cases, otherwise, value)
Returns the case
which has a key that matches value
. If none match, returns otherwise
const cases = true: "It's true" false: "It's false"const result = // result = "It's true" const cases = 'test': "It's a test" 'other': "It's something else"const result = // result = "It's something else" const cases = true: "It's true" false: "It's false"const result = // result = 'otherwise' const cases = "It's true" false: "It's false"const result = // result = "It's true"