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.
npm install fandy --save
npm install
npm test
Returns a function which returns the result of its arguments being passed one by one through fns
const first = x => x + 1
const second = x => x * 2
const result = pipe(first, second)(1)
// result = 4
Returns an array consisting of string
split on token
const string = 'I-am-a-string'
const result = split(string, '-')
// result = ['I', 'am', 'a', 'string']
Returns a new array consisting of array
with all duplicates removed.
const array = ['Bob', 'James', 'Bob']
const result = uniq(array)
// result = ['Bob', 'James']
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 = zip(keys, firstNames, lastNames, cities)
// result = [
// { firstName: 'Bob', lastName: 'Builder', currentCity: 'London' },
// { firstName: 'James', lastName: 'Rocket', currentCity: 'Paris' },
// { firstName: 'Tim', lastName: 'Tiny', currentCity: undefined }
// ]
Returns the index
-th item from the end of array
const array = ['Bob', 'James', 'Robert']
const result = last(array)
// result = 'Robert'
const array = ['Bob', 'James', 'Robert']
const result = last(array, 2)
// result = 'James'
Returns a new array consisting of array
with element at index
removed
const array = ['Bob', 'James', 'Robert']
const result = removeAt(array, 1)
// result = ['Bob', 'Robert']
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 = switcher(cases, 'otherwise', true)
// result = "It's true"
const cases = {
'test': "It's a test",
'other': "It's something else"
}
const result = switcher(cases, 'otherwise', 'other')
// result = "It's something else"
const cases = {
true: "It's true",
false: "It's false"
}
const result = switcher(cases, 'otherwise', 'none of the above')
// result = 'otherwise'
const cases = {
true: () => "It's true",
false: "It's false"
}
const result = switcher(cases, 'default', true)
// result = "It's true"