- always
- and
- assign
- average
- between
- bind
- bitAnd
- bitOr
- butLast
- by
- charCodeAt
- codePointAt
- compose
- composeAll
- concat
- converge
- curry
- curryRight
- dec
- drop
- endsWith
- endsWithAt
- equal
- every
- exec
- explode
- filter
- flatMap
- flip
- fold
- foldRight
- forEach
- greaterOrEqual
- greaterThan
- hasOwnProperty
- head
- identity
- ifThen
- ifThenElse
- implode
- inc
- indexOf
- isBetween
- isBoolean
- isFalse
- isFalsy
- isFunction
- isNull
- isNumber
- isObject
- isPlainObject
- isString
- isTrue
- isTruthy
- isTypeOf
- isUndefined
- isUnknown
- join
- keys
- last
- lastIndexOf
- length
- lessOrEqual
- lessThan
- looseEqual
- map
- match
- max
- method
- min
- minus
- nand
- noop
- nor
- not
- nth
- omit
- or
- pick
- pipe
- pipeAll
- plus
- product
- property
- push
- put
- reduce
- reduceRight
- replace
- shallowClone
- shave
- signum
- slice
- some
- split
- startsWith
- startsWithAt
- sum
- tail
- take
- takeUntil
- takeWhile
- test
- times
- toLowerCase
- toUpperCase
- uncurry
- uncurry3
- unfold
- values
- xor
Creates a function that always returns a given value
const always = require('1-liners/always');
const T = always(true);
T(); // => true
T(); // => true
const fortyTwo = always(42);
fortyTwo(); // => 42
fortyTwo(); // => 42
Same as a && b
.
const and = require('1-liners/and');
and(true, true); // => true
and(false, true); // => false
Returns a new object and assigns assign
to object
.
const assign = require('1-liners/assign');
const yedi = { id: 1, age: 100 };
assign({ name: 'Yoda', age: 900 }, yedi); // => { id: 1, name: 'Yoda', 900 }
Returns the average of all items of an array
.
const average = require('1-liners/average');
average([2, 3, 4]); // => 3
average([]); // => NaN
Return number
if it’s greater than min
and lower than max
. Else return min
or max
respectively.
const between = require('1-liners/between');
between(1, 10, 2.5); // => 2.5
between(1, 10, -5); // => 1
between(1, 10, 25); // => 10
Binds a context to a function. Same as fun.bind(thisArg[, arg1[, arg2[, ...]]])
const bind = require('1-liners/bind');
setTimeout(bind(console, ['Hello'], console.log), 2000); // => 'Hello' (after 2s)
Same as a & b
.
const bitAnd = require('1-liners/bitAnd');
bitAnd(1, 2); // => 0
bitAnd(2, 2); // => 2
Same as a | b
.
const bitOr = require('1-liners/bitOr');
bitOr(0, 1); // => 1
bitOr(1, 1); // => 1
Return a copy of array
, without the last item.
import butLast from '1-liners/butLast';
const array = [1, 2, 3];
butLast(array); // => [1, 2]
array; // => [1, 2, 3]
Same as a / b
const by = require('1-liners/by');
by(6, 2); // => 3
Same as 'STR'.charCodeAt(0)
.
const charCodeAt = require('1-liners/charCodeAt');
charCodeAt(0, 'super') // => 115
Same as 'STR'.codePointAt(0)
.
const codePointAt = require('1-liners/codePointAt');
codePointAt(0, 'super') // => 115
Compose a new function from two given functions.
const compose = require('1-liners/compose');
compose(f, g)(1, 2) === f(g(1, 2));
Compose a new function with a given array of functions.
const composeAll = require('1-liners/composeAll');
composeAll([f, g, h])(1, 2) === f(g(h(1, 2)));
Returns a copy of array
with values
or value
appended at the end. Same as array.concat(values)
or array.concat(value)
.
const concat = require('1-liners/concat');
concat(['c', 'd'], ['a', 'b']); // => ['a', 'b', 'c', 'd']
concat(['c'], ['a', 'b']); // => ['a', 'b', 'c']
concat('c', ['a', 'b']); // => ['a', 'b', 'c']
Converge two functions into one.
const converge = require('1-liners/converge');
converge(f, g, h)(1, 2) === f(g(1, 2), h(1, 2));
Curry a function – split its list of parameters into 2 lists.
import curry from '1-liners/curry';
import reduce from '1-liners/reduce';
import compose from '1-liners/compose';
// You can use reduce and compose to create curry3,4 and so on.
const curry3 = compose(curry, curry);
const curry4 = reduce(compose, [curry, curry, curry]);
const f = (a, b, c, d) => a * b * c * d;
const fβ = curry(f); // ~= curry2
const fγ = curry3(f); // ~= curry3
const fδ = curry4(f); // ~= curry4
f(1, 2, 3, 4) === 24
fβ(1)(2, 3, 4) === 24
fβ(1, 2)(3, 4) === 24
fβ(1, 2, 3)(4) === 24
fγ(1)(2)(3, 4) === 24
fγ(1)(2, 3)(4) === 24
fδ(1)(2)(3)(4) === 24
Curry a function from the right – split its parameters into 2 lists. Apply the second list of parameters first, without changing the order within the lists.
import curryRight from '1-liners/curryRight';
const g = (a, b, c, d) => a + b * c - d;
g(1, 2, 3, 4); // => 3
const gλ = curryRight(g);
gλ(4)(1, 2, 3); // => 3
gλ(3, 4)(1, 2); // => 3
gλ(2, 3, 4)(1); // => 3
Same as a - 1
const dec = require('1-liners/dec');
dec(1); // => 0
Returns the tail of array
after dropping the first n
elements.
Use this in place of String's .substr(startIndex)
and .substring(startIndex)
const drop = require('1-liners/drop');
const array = [1, 2, 3, 4, 5];
const string = 'Hello World';
drop(2, array); // => [3, 4, 5]
drop(6, string); // => 'World'
Same as str.endsWith(searchString)
.
const endsWith = require('1-liners/endsWith');
endsWith('liners', '1-liners'); // => true
endsWith('stoeffel', 'nope'); // => false
Same as str.endsWith(searchString, position)
.
const endsWithAt = require('1-liners/endsWithAt');
endsWithAt(8, 'liners', '1-liners/endsWithAt'); // => true
endsWithAt(2, 'stoeffel', 'nope'); // => false
Same as a === b
.
const equal = require('1-liners/equal');
equal(true, true); // => true
equal(false, true); // => false
equal(1, true); // => false
Same as [1,2,3].every(GreaterThan16)
.
const every = require('1-liners/every');
every(elem => elem > 16, [16,17,18]); // => false
Same as regexObj.exec(str)
.
const exec = require('1-liners/exec');
const haystack = 'hAyHAYhayneEdLEHayHAy';
exec(haystack, /needle/i); // => ['neEdLE']
exec(haystack, /n(.+)e/i); // => ['neEdLE', 'eEdL']
exec(haystack, /needle/); // => null
The opposite of implode.
const explode = require('1-liners/explode');
const sum = (numbers) => numbers.reduce((a, b) => a + b);
explode(sum)(1, 2, 3, 4); // => 10
Same as [1, 2, 3].filter(isOdd)
.
const filter = require('1-liners/filter');
filter(isOdd, [1, 2, 3]); // => [1, 3]
Map a function over a collection and flatten the result by one-level.
const flatMap = require('1-liners/flatMap');
flatMap((x) => [x, x], [1, 2, 3]); // => [1, 1, 2, 2, 3, 3]
Flip a function’s arguments.
const flip = require('1-liners/flip');
const f = (a, b) => a / b;
flip(f)(2, 6); // => 3
flip(flip(f))(6, 2); // => 3
Same as array.reduce
.
const fold = require('1-liners/fold');
fold(sum, 8, [1, 2, 3]); // => 2
Same as array.reduceRight
.
const foldRight = require('1-liners/foldRight');
foldRight(sub, 1, [1, 2, 3]); // => -5
Same as [1, 2, 3].forEach(Math.sqrt)
.
const forEach = require('1-liners/forEach');
forEach(i => console.log('Item: ' + i), [9, 25]); // => logs "Item: 9" and "Item: 25"
Same as a >= b
.
const greaterOrEqual = require('1-liners/greaterOrEqual');
greaterOrEqual(2, 1); // => true
greaterOrEqual(2, 2); // => true
greaterOrEqual(1, 2); // => false
Same as a > b
.
const greaterThan = require('1-liners/greaterThan');
greaterThan(2, 1); // => true
greaterThan(2, 2); // => false
greaterThan(1, 2); // => false
Same as obj.hasOwnProperty(prop)
.
const hasOwnProperty = require('1-liners/hasOwnProperty');
hasOwnProperty('a', {a: 1, b: 2}); // => true
hasOwnProperty('c', {a: 1, b: 2}); // => false
Returns the first item of an array.
const head = require('1-liners/head');
head([1, 2, 3]); // => 1
Returns the value you pass to the function
const identity = require('1-liners/identity');
identity(true); // => true
identity(1); // => 1
identity({ foo: 1 }); // => { foo: 1 }
identity("1-liners"); // => "1-liners"
Creates a function which calls then
if the predicate
is true
and returns undefined
if the predicate
is false.
const ifThen = require('1-liners/ifThen');
const eq = (a, b) => a === b;
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const words = ifThen((str) => typeof str === 'string', (str) => str.split(' '));
words('Hello ES2015'); // => ['Hello', 'ES2015']
Creates a function which calls then
if the predicate
is true
and otherwise
if the predicate
is false.
const ifThenElse = require('1-liners/ifThenElse');
const eq = (a, b) => a === b;
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const addIfEq = ifThenElse(eq, add, sub);
addIfEq(1, 1); // => 2
addIfEq(2, 1); // => 1
(predicate, then, otherwise) => (...args) => predicate(...args) ? then(...args) : otherwise(...args);
Collapse a list of arguments into an array of arguments.
const implode = require('1-liners/implode');
const f = (a, b) => a + b;
[
[1, 2],
[3, 4],
[5, 6],
].map(implode(f)); // => [3, 7, 11]
Same as a + 1
const inc = require('1-liners/inc');
inc(1); // => 2
Same as 'str'.indexOf('t')
.
const indexOf = require('1-liners/indexOf');
indexOf('a', 'hallo') // => 1
Check if the number
lies between min
and max
, inclusive.
const isBetween = require('1-liners/isBetween');
isBetween(1, 10, 2.5); // => true
isBetween(1, 10, -5); // => false
isBetween(1, 10, 25); // => false
Same as typeof value === 'boolean'
.
const isBoolean = require('1-liners/isBoolean');
isBoolean(false); // => true
isBoolean(true); // => true
isBoolean(null); // => false
isBoolean(/anything else/); // => false
Same as x === false
.
const isFalse = require('1-liners/isFalse');
isFalse(false); // => true
isFalse('yes'); // => false
isFalse(true); // => false
isFalse([]); // => false
isFalse(''); // => false
isFalse(0); // => false
Same as !
.
const isFalsy = require('1-liners/isFalsy');
isFalsy('yes'); // => false
isFalsy(true); // => false
isFalsy([]); // => false
isFalsy(''); // => true
isFalsy(0); // => true
isFalsy(false); // => true
Same as typeof value === 'function'
.
const isFunction = require('1-liners/isFunction');
isFunction(function() {}); // => true
isFunction(function named() {}); // => true
isFunction('any other value'); // => false
Same as === null
.
const isNull = require('1-liners/isNull');
isNull(null); // => true
isNull(undefined); // => false
isNull(NaN); // => false
isNull('anything else'); // => false
Same as typeof value === 'number'
. Use Number.isFinite
instead if you want to filter out NaN
and Infinity
.
const isNumber = require('1-liners/isNumber');
isNumber(1); // => true
isNumber(3.14); // => true
isNumber(NaN); // => true
isNumber(Infinity); // => true
isNumber('3.14'); // => false
isNumber(/anything else/); // => false
Same as value !== null && typeof value === 'object'
.
const isObject = require('1-liners/isObject');
isObject({}); // => true
isObject([]); // => true
isObject(/anything/); // => true
isObject(null); // => false
isObject('anything else'); // => false
Checks if an object inherits directly from null
or Object.prototype
– like an object literal ({...}
) does.
Heads up! This function is not supported on IE 10 and below.
const isPlainObject = require('1-liners/isPlainObject');
isPlainObject({}); // => true
isPlainObject(Object.create(null)); // => true
isPlainObject(null); // => false
isPlainObject([]); // => false
isPlainObject(/anything else/); // => false
(value) => (value && typeof value === 'object' && (value.__proto__ == null || value.__proto__ === Object.prototype));
Same as typeof value === 'string'
.
const isString = require('1-liners/isString');
isString(''); // => true
isString('anything'); // => true
isString(/anything else/); // => false
Same as x === true
.
const isTrue = require('1-liners/isTrue');
isTrue(true); // => true
isTrue('yes'); // => false
isTrue([]); // => false
isTrue(''); // => false
isTrue(0); // => false
isTrue(false); // => false
Same as !!
.
const isTruthy = require('1-liners/isTruthy');
isTruthy('yes'); // => true
isTruthy(true); // => true
isTruthy([]); // => true
isTruthy(''); // => false
isTruthy(0); // => false
isTruthy(false); // => false
Same as typeof value === TYPE
.
const isTypeOf = require('1-liners/isTypeOf');
isTypeOf('boolean', false); // => true
isTypeOf('boolean', true); // => true
isTypeOf('boolean', null); // => false
isTypeOf('boolean', /anything else/); // => false
Returns true
if a value or reference is undefined
.
const isUndefined = require('1-liners/isUndefined');
isUndefined(undefined); // => true
isUndefined(null); // => false
isUndefined(false); // => false
isUndefined(NaN); // => false
isUndefined('anything else'); // => false
Same as == null
.
const isUnknown = require('1-liners/isUnknown');
isUnknown(null); // => true
isUnknown(undefined); // => true
isUnknown(false); // => false
isUnknown(''); // => false
isUnknown(NaN); // => false
isUnknown(/anything else/); // => false
Same as [1, 'liners'].join('-')
const join = require('1-liners/join');
join('-', [1, 'liners']); // => '1-liners'
Same as Object.keys(obj)
.
const keys = require('1-liners/keys');
keys({ 100: 'a', 2: 'b', 7: 'c' }); // => ['2', '7', '100']
keys([1, 2, 3]); // => [0, 1, 2]
Returns the last item of array
.
const last = require('1-liners/last');
last([1, 2, 3]); // => 3
Same as 'wow'.lastIndexOf('w')
.
const lastIndexOf = require('1-liners/lastIndexOf');
lastIndexOf('f', 'waffle') // => 3
Returns the length of an array.
const length = require('1-liners/length');
length([0, 1, 2]); // => 3
Same as a <= b
.
const lessOrEqual = require('1-liners/lessOrEqual');
lessOrEqual(1, 2); // => true
lessOrEqual(1, 1); // => true
lessOrEqual(2, 1); // => false
Same as a < b
.
const lessThan = require('1-liners/lessThan');
lessThan(1, 2); // => true
lessThan(1, 1); // => false
lessThan(2, 1); // => false
Same as a == b
.
const looseEqual = require('1-liners/looseEqual');
looseEqual(true, true); // => true
looseEqual(false, true); // => false
looseEqual(1, true); // => true
Same as [1, 2, 3].map(Math.sqrt)
.
const map = require('1-liners/map');
map(Math.sqrt, [9, 25]); // => [3, 5]
Same as haystack.match(needle)
.
const match = require('1-liners/match');
match(/\d+/g, 'Items: 3,2'); // => ["3", "2"]
Same as Math.max
– but with a stable number of arguments.
const max = require('1-liners/max');
max(3, 6); // => 6
[3, 6, 9].reduce(max); // => 9
[3, 6, 9].reduce(Math.max); // => NaN
Same as object[method](...args)
const method = require('1-liners/method');
const object = {
base: 1,
add(number) { return this.base + number; },
};
method('add', object)(5); // => 6
Same as Math.min
– but with a stable number of arguments.
const min = require('1-liners/min');
min(3, 6); // => 3
[3, 6, 1].reduce(min); // => 1
[3, 6, 1].reduce(Math.min); // => NaN
Same as a - b
const minus = require('1-liners/minus');
minus(3, 2); // => 1
Same as !(a && b)
.
const nand = require('1-liners/nand');
nand(0, 0); // => true
nand(1, 1); // => false
Same as function(){}
.
const noop = require('1-liners/noop');
window.console = {
log: noop,
error: noop,
warn: noop,
table: noop
};
Same as !(a || b)
.
const nor = require('1-liners/nor');
nor(0, 0); // => true
nor(1, 0); // => false
Same as !a
.
const not = require('1-liners/not');
not(true); // => false
not(false); // => true
Returns the nth item of an array.
const nth = require('1-liners/nth');
nth(1, [1, 2, 3]); // => 2
Creates a copy of the object
without the given props
.
const omit = require('1-liners/omit');
const object = {foo: 1, bar: 2, baz: 3};
omit(['foo', 'baz'], object); // => {bar: 2}
(props, obj) => props.reduce((newObj, val) => (({ [val]: dropped, ...rest }) => rest)(newObj), obj);
Same as a || b
.
const or = require('1-liners/or');
or(true, true); // => true
or(false, true); // => true
or(false, false); // => false
Copies only specified properties
from an object
into a new object.
const pick = require('1-liners/pick');
const object = {foo: 1, bar: 2, baz: 3};
pick(['foo', 'baz'], object); // => {foo: 1, baz: 3}
(properties, object) => Object.assign({}, ...properties.map(key => ({[key]: object[key]})));
Pipe arguments through functions.
const pipe = require('1-liners/pipe');
pipe(f, g)(1, 2) === g(f(1, 2));
Pipe arguments through an array of functions.
const pipeAll = require('1-liners/pipeAll');
pipeAll([f, g, h])(1, 2) === h(g(f(1, 2)));
Same as a + b
.
const plus = require('1-liners/plus');
plus(2, 8); // => 10
plus('a', 'b'); // => 'ab'
Returns the product of all items of an array
.
const product = require('1-liners/product');
product([2, 3, 4]); // => 24
product([]); // => 1
Same as object[property]
const property = require('1-liners/property');
const object = {foo: 1};
property('foo', object); // => 1
Same as push but immutable.
const push = require('1-liners/push');
push(4, [1, 2, 3]); // => [1, 2, 3, 4]
Same as Object.assign({}, obj, {[key]: val})
const put = require('1-liners/put');
const object = {id: 1};
put('name', 'stoeffel', object); // => { id: 1, name: 'stoeffel' }
Same as [1, 2, 3].reduce(sum)
.
const reduce = require('1-liners/reduce');
reduce(sum, [1, 2, 3]); // => 6
Same as [1, 2, 3].reduceRight(sub)
.
const reduceRight = require('1-liners/reduceRight');
reduceRight(sub, [1, 2, 3]); // => -4
Same as haystack.replace(needle, replace)
.
const replace = require('1-liners/replace');
replace(/\d+/g, sub => `"${sub}"`, 'Items: 3,2'); // => Items: "3","2"
replace(':', '=', 'Items: 3,2'); // => Items= 3,2
Copy all properties of an object into a new plain object.
import shallowClone from '1-liners/shallowClone';
const source = {
value: 'value',
reference: /reference/,
};
const target = shallowClone(source);
target === source // => false
target.value === source.value // => true
target.reference === source.reference // => true
Shave ensures that a function is called with n arguments.
const shave = require('1-liners/shave');
map(parseInt, [0, 1.1, 2.2]); // => [0, NaN, NaN]
map(shave(1, parseInt), [0, 1.1, 2.2]); // => [0, 1, 2]
Returns the sign of a number. 1
if n
is positive, -1
if n
is negative and 0
if n
is 0
. Otherwise returns NaN
.
const signum = require('1-liners/signum');
signum(-5); // => -1
signum(-Infinity); // => -1
signum(10); // => 1
signum(Infinity); // => 1
signum(0); // => 0
signum(-0); // => 0
Same as '1-liners'.slice(2,4)
or [1,2,3,4].slice(1,3)
Use in place of '1-liners'.substring(2,6)
const slice = require('1-liners/slice');
slice(2, 6, '1-liners'); // => 'line'
slice(1, 3, [1,2,3,4]); // => [2,3]
Same as [1,2,3].some(GreaterThan16)
const some = require('1-liners/some');
some(elem => elem > 16, [16,17,18]); // => true
Same as '1-liners'.split('-')
const split = require('1-liners/split');
split('-', '1-liners'); // => [1, 'liners']
Same as str.startsWith(searchString)
.
const startsWith = require('1-liners/startsWith');
startsWith('1', '1-liners'); // => true
startsWith('stoeffel', 'nope'); // => false
Same as str.startsWith(searchString, position)
.
const startsWithAt = require('1-liners/startsWithAt');
startsWithAt(2, 'liners', '1-liners/startsWithAt'); // => true
startsWithAt(2, 'stoeffel', 'nope'); // => false
Sums all items of an array
.
const sum = require('1-liners/sum');
sum([1, 2, 3]); // => 6
sum([]); // => 0
Returns the tail of an array
const tail = require('1-liners/tail');
tail([1, 2, 3]); // => [2, 3]
Take n items of an array. Same as arr.slice(0, n)
.
const take = require('1-liners/take');
take(2, [1, 2, 3]); // => [1, 2]
Take items of an array until they fulfill a predicate.
const takeUntil = require('1-liners/takeUntil');
takeUntil(i => i % 2 === 1, [2, 4, 6, 8, 7, 8, 8]); // => [2, 4, 6, 8]
(pred, arr) => arr.reduce((newArr, i) => { if (pred(i)) arr.length = 0; else newArr.push(i); return newArr; }, []);
Take items of an array while they fulfill a predicate.
const takeWhile = require('1-liners/takeWhile');
takeWhile(i => i % 2 === 0, [2, 4, 6, 8, 7, 8, 8]); // => [2, 4, 6, 8]
(pred, arr) => arr.reduce((newArr, i) => { if (!pred(i)) arr.length = 0; else newArr.push(i); return newArr; }, []);
Same as regexObj.test(str)
.
const test = require('1-liners/test');
const haystack = 'hAyHAYhayneEdLEHayHAy';
test(haystack, /needle/); // => false
test(haystack, /needle/i); // => true
Same as a * b
const times = require('1-liners/times');
times(3, 2); // => 6
Same as 'STR'.toLowerCase()
.
const toLowerCase = require('1-liners/toLowerCase');
toLowerCase('HALLO') // => 'hallo'
Same as 'str'.toUpperCase()
.
const toUpperCase = require('1-liners/toUpperCase');
toUpperCase('hallo') // => 'HALLO'
Uncurry a function – collapse 2 lists of parameters into one.
import uncurry from '1-liners/uncurry';
const f = (a) => (b) => a + b;
const fβ = uncurry(f);
fβ(1, 2); // => 3
const g = (a) => (b, c) => a + b + c
const gβ = uncurry(g);
gβ(1, 2, 3); // => 6
Uncurry a function – collapse 3 lists of parameters into one.
import uncurry3 from '1-liners/uncurry3';
const f = (a) => (b) => (c) => a + b + c;
const fβ = uncurry3(f);
fβ(1, 2, 3); // => 6
const g = (a) => (b) => (c, d) => a + b + c + d;
const gβ = uncurry3(g);
gβ(1, 2, 3, 4); // => 10
Builds a list from a seed value.
const unfold = require('1-liners/unfold');
const fn = n => n < 20 ? [n, n + 1] : false;
unfold(fn, 10); // => [10,11,12,13,14,15,16,17,18,19]
// range in terms of unfold
const range = (from, to) => unfold((seed) => seed < to ? [seed, seed + 1] : false, from);
range(1, 10); // => [1,2,3,4,5,6,7,8,9]
// unnest in terms of unfold
const unnest = xs => unfold((seed) => seed < xs.length ? [xs[seed], seed + 1] : false , 0);
unnest([[1, 2], [3, 4], [5, 6]]); // => [1,2,3,4,5,6]
function unfold (fn, seed, acc = []) { return fn(seed) ? unfold(fn, fn(seed)[1], acc.concat.apply(acc, [fn(seed)[0]])) : acc }
Get all values of an object
Same as Object.keys(obj).map(i => obj[i])
.
const values = require('1-liners/values');
values({ 100: 'a', 2: 'b', 7: 'c' }); // => ['a', 'b', 'c']
values(['a', 'b', 'c']); // => ['a', 'b', 'c']
Same as (x && !y) || (!x && y)
const xor = require('1-liners/xor');
xor(0, 1); // => 1
xor(1, 1); // => 0