├── .gitattributes ├── .coveralls.yml ├── .gitignore ├── tests ├── by.js ├── dec.js ├── inc.js ├── plus.js ├── minus.js ├── nth.js ├── times.js ├── length.js ├── nor.js ├── last.js ├── shave.js ├── join.js ├── not.js ├── split.js ├── match.js ├── bitOr.js ├── nand.js ├── lessThan.js ├── noop.js ├── endsWith.js ├── head.js ├── sum.js ├── tail.js ├── max.js ├── min.js ├── startsWith.js ├── takeUntil.js ├── takeWhile.js ├── lessOrEqual.js ├── map.js ├── greaterThan.js ├── some.js ├── always.js ├── endsWithAt.js ├── equal.js ├── every.js ├── or.js ├── product.js ├── xor.js ├── forEach.js ├── and.js ├── greaterOrEqual.js ├── startsWithAt.js ├── flatMap.js ├── fold.js ├── reduce.js ├── bitAnd.js ├── isTrue.js ├── hasOwnProperty.js ├── isFalsy.js ├── isFalse.js ├── average.js ├── foldRight.js ├── isTruthy.js ├── uncurry.js ├── converge.js ├── filter.js ├── method.js ├── reduceRight.js ├── test.js ├── bind.js ├── keys.js ├── looseEqual.js ├── between.js ├── butLast.js ├── put.js ├── pipe.js ├── values.js ├── ifThen.js ├── property.js ├── push.js ├── replace.js ├── uncurry3.js ├── extend.js ├── pipeAll.js ├── compose.js ├── isBetween.js ├── composeAll.js ├── flip.js ├── take.js ├── identity.js ├── ifThenElse.js ├── implode.js ├── curryRight.js ├── signum.js ├── explode.js ├── concat.js ├── shallowClone.js ├── drop.js ├── isNull.js ├── pick.js ├── isUnknown.js ├── exec.js ├── isUndefined.js ├── isString.js ├── isNumber.js ├── isFunction.js ├── isBoolean.js ├── isObject.js ├── curry.js ├── isPlainObject.js ├── isTypeOf.js └── oneliner.js ├── .travis.yml ├── .jshintrc ├── module ├── by.js ├── dec.js ├── inc.js ├── minus.js ├── times.js ├── head.js ├── length.js ├── not.js ├── tail.js ├── nth.js ├── bitOr.js ├── last.js ├── bitAnd.js ├── nor.js ├── plus.js ├── and.js ├── map.js ├── nand.js ├── pipe.js ├── xor.js ├── reduce.js ├── split.js ├── filter.js ├── join.js ├── some.js ├── take.js ├── match.js ├── every.js ├── or.js ├── compose.js ├── sum.js ├── converge.js ├── lessThan.js ├── reduceRight.js ├── equal.js ├── property.js ├── noop.js ├── greaterThan.js ├── lessOrEqual.js ├── forEach.js ├── pipeAll.js ├── flip.js ├── looseEqual.js ├── product.js ├── composeAll.js ├── greaterOrEqual.js ├── push.js ├── explode.js ├── flatMap.js ├── average.js ├── butLast.js ├── fold.js ├── put.js ├── max.js ├── min.js ├── identity.js ├── isString.js ├── extend.js ├── shave.js ├── foldRight.js ├── isNull.js ├── values.js ├── keys.js ├── always.js ├── implode.js ├── method.js ├── replace.js ├── isFalsy.js ├── isBetween.js ├── isFunction.js ├── isTrue.js ├── takeUntil.js ├── takeWhile.js ├── isFalse.js ├── isTruthy.js ├── pick.js ├── isBoolean.js ├── between.js ├── endsWith.js ├── test.js ├── bind.js ├── startsWith.js ├── uncurry.js ├── isTypeOf.js ├── hasOwnProperty.js ├── uncurry3.js ├── endsWithAt.js ├── isUnknown.js ├── drop.js ├── isObject.js ├── exec.js ├── isUndefined.js ├── startsWithAt.js ├── curryRight.js ├── shallowClone.js ├── concat.js ├── ifThenElse.js ├── signum.js ├── ifThen.js ├── isNumber.js ├── isPlainObject.js ├── curry.js └── index.js ├── .editorconfig ├── CONTRIBUTING.md ├── utils ├── updateFuncCount.js └── createDocs.js ├── license ├── package.json ├── README.md └── documentation └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-pro 2 | repo_token: HYzOUexk6Bz9k7OlWljTX8pMlj94Y3sCz 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | *.js 4 | !module/*.js 5 | !tests/*.js 6 | !utils/*.js 7 | coverage 8 | -------------------------------------------------------------------------------- /tests/by.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import by from '../by'; 3 | 4 | test('#by', () => equal(by(6, 2), 3)); 5 | -------------------------------------------------------------------------------- /tests/dec.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import dec from '../dec'; 3 | 4 | test('#dec', () => equal(dec(5), 4)); 5 | -------------------------------------------------------------------------------- /tests/inc.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import inc from '../inc'; 3 | 4 | test('#inc', () => equal(inc(0), 1)); 5 | -------------------------------------------------------------------------------- /tests/plus.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import plus from '../plus'; 3 | 4 | test('#plus', () => equal(plus(0, 2), 2)); 5 | -------------------------------------------------------------------------------- /tests/minus.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import minus from '../minus'; 3 | 4 | test('#minus', () => equal(minus(5, 2), 3)); 5 | -------------------------------------------------------------------------------- /tests/nth.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import nth from '../nth'; 3 | 4 | test('#nth', () => equal(nth(0, [0, 1, 2]), 0)); 5 | -------------------------------------------------------------------------------- /tests/times.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import times from '../times'; 3 | 4 | test('#times', () => equal(times(2, 3), 6)); 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 'iojs' 5 | - '0.12' 6 | - '0.10' 7 | after_script: "npm run coveralls" 8 | -------------------------------------------------------------------------------- /tests/length.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import length from '../length'; 3 | 4 | test('#length', () => equal(length([0, 1, 2]), 3)); 5 | -------------------------------------------------------------------------------- /tests/nor.js: -------------------------------------------------------------------------------- 1 | import { 2 | equal 3 | } 4 | from 'assert'; 5 | import nor from '../nor'; 6 | 7 | test('#nor', () => equal(nor(0, 0), true)); 8 | -------------------------------------------------------------------------------- /tests/last.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import last from '../last'; 3 | 4 | test('#last', () => { 5 | equal(last([1, 2, 3]), 3); 6 | }); 7 | -------------------------------------------------------------------------------- /tests/shave.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import shave from '../shave'; 3 | 4 | test('#shave', () => equal(shave(1, parseInt)(2.2, 2), 2)); 5 | -------------------------------------------------------------------------------- /tests/join.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import join from '../join'; 3 | 4 | test('#join', () => equal(join('-', [1, 'liners']), '1-liners')); 5 | -------------------------------------------------------------------------------- /tests/not.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import not from '../not'; 3 | 4 | test('#not', () => { 5 | ok(!not(true)); 6 | ok(not(false)); 7 | }); 8 | -------------------------------------------------------------------------------- /tests/split.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import split from '../split'; 3 | 4 | test('#split', () => deepEqual(split('-', '1-liners'), [1, 'liners'])); 5 | -------------------------------------------------------------------------------- /tests/match.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import match from '../match'; 3 | 4 | test('#match', () => deepEqual(match(/\d+/g, '1.2.3.4.5'), ["1","2","3","4","5"])); 5 | -------------------------------------------------------------------------------- /tests/bitOr.js: -------------------------------------------------------------------------------- 1 | import { 2 | equal 3 | } 4 | from 'assert'; 5 | import bitOr from '../bitOr'; 6 | 7 | test('#bitOr', () => { 8 | equal(bitOr(1, 0), 1); 9 | equal(bitOr(1, 1), 1); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/nand.js: -------------------------------------------------------------------------------- 1 | import { 2 | equal 3 | } 4 | from 'assert'; 5 | import nand from '../nand'; 6 | 7 | test('#nand', () => { 8 | equal(nand(0, 0), true); 9 | equal(nand(1, 1), false); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/lessThan.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import lessThan from '../lessThan'; 3 | 4 | test('#lessThan', () => { 5 | ok(lessThan(1, 2)); 6 | ok(!lessThan(1, 1)); 7 | ok(!lessThan(2, 1)); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/noop.js: -------------------------------------------------------------------------------- 1 | import { 2 | ok 3 | } 4 | from 'assert'; 5 | import noop from '../noop'; 6 | 7 | test('#noop', () => { 8 | ok(typeof noop === 'function'); 9 | ok(noop() === undefined); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/endsWith.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import endsWith from '../endsWith'; 3 | 4 | test('#endsWith', () => { 5 | ok(endsWith('liners', '1-liners')); 6 | ok(!endsWith('stoeffel', 'nope')); 7 | }); 8 | -------------------------------------------------------------------------------- /tests/head.js: -------------------------------------------------------------------------------- 1 | import { equal, deepEqual } from 'assert'; 2 | import head from '../head'; 3 | 4 | test('#head', () => { 5 | let arr = [0, 1, 2]; 6 | equal(head(arr), 0); 7 | deepEqual(arr, [0, 1, 2]); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/sum.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import sum from '../sum'; 3 | 4 | test('#sum', () => { 5 | equal(sum([1, 2, 3]), 6); 6 | equal(sum([]), 0); 7 | equal(sum([{valueOf: () => 10}]), 10); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/tail.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import tail from '../tail'; 3 | 4 | test('#tail', () => { 5 | let arr = [0, 1, 2]; 6 | deepEqual(tail(arr), [1, 2]); 7 | deepEqual(arr, [0, 1, 2]); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/max.js: -------------------------------------------------------------------------------- 1 | import {equal} from 'assert'; 2 | import max from '../max'; 3 | 4 | test('#max', () => { 5 | equal(max(3, 6), 6); 6 | equal(max(6, 3), 6); 7 | 8 | equal([3, 6, 9].reduce(max), 9); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/min.js: -------------------------------------------------------------------------------- 1 | import {equal} from 'assert'; 2 | import min from '../min'; 3 | 4 | test('#min', () => { 5 | equal(min(3, 6), 3); 6 | equal(min(6, 3), 3); 7 | 8 | equal([3, 6, 1].reduce(min), 1); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/startsWith.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import startsWith from '../startsWith'; 3 | 4 | test('#startsWith', () => { 5 | ok(startsWith('1', '1-liners')); 6 | ok(!startsWith('stoeffel', 'nope')); 7 | }); 8 | -------------------------------------------------------------------------------- /tests/takeUntil.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import takeUntil from '../takeUntil'; 3 | 4 | test('#takeUntil', () => { 5 | deepEqual(takeUntil(i => i % 2 === 1, [2, 4, 6, 5, 8, 2]), [2, 4, 6]); 6 | }); 7 | -------------------------------------------------------------------------------- /tests/takeWhile.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import takeWhile from '../takeWhile'; 3 | 4 | test('#takeWhile', () => { 5 | deepEqual(takeWhile(i => i % 2 === 0, [2, 4, 6, 5, 8, 2]), [2, 4, 6]); 6 | }); 7 | -------------------------------------------------------------------------------- /tests/lessOrEqual.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import lessOrEqual from '../lessOrEqual'; 3 | 4 | test('#lessOrEqual', () => { 5 | ok(lessOrEqual(1, 2)); 6 | ok(lessOrEqual(1, 1)); 7 | ok(!lessOrEqual(2, 1)); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/map.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import map from '../map'; 3 | 4 | test('#map', () => { 5 | let arr = [9, 16, 25]; 6 | deepEqual(map(Math.sqrt, arr), [3, 4, 5]); 7 | deepEqual(arr, [9, 16, 25]); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/greaterThan.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import greaterThan from '../greaterThan'; 3 | 4 | test('#greaterThan', () => { 5 | ok(greaterThan(2, 1)); 6 | ok(!greaterThan(1, 1)); 7 | ok(!greaterThan(1, 2)); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/some.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import some from '../some'; 3 | 4 | test('#some', () => { 5 | equal(some(elem => elem >= 16,[1,2,7,9,11,42]), true); 6 | equal(some(elem => elem >= 16,[0,1,2]), false); 7 | }); 8 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "curly": true, 6 | "immed": true, 7 | "newcap": true, 8 | "noarg": true, 9 | "undef": true, 10 | "unused": "vars", 11 | "eqnull": true 12 | } 13 | -------------------------------------------------------------------------------- /tests/always.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import always from '../always'; 3 | 4 | test('#always', () => { 5 | equal(always(true)(), true); 6 | equal(always(42)(), 42); 7 | equal(always('1-liners')(), '1-liners'); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/endsWithAt.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import endsWithAt from '../endsWithAt'; 3 | 4 | test('#endsWithAt', () => { 5 | ok(endsWithAt(8, 'liners', '1-liners/endsWithAt')); 6 | ok(!endsWithAt(2, 'stoeffel', 'nope')); 7 | }); 8 | -------------------------------------------------------------------------------- /tests/equal.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import equal from '../equal'; 3 | 4 | test('#equal', () => { 5 | ok(equal(true, true)); 6 | ok(!equal(false, true)); 7 | ok(equal(1, 1)); 8 | ok(!equal(1, true)); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/every.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import every from '../every'; 3 | 4 | test('#every', () => { 5 | equal(every(elem => elem >= 16,[1,2,7,9,11,42]), false); 6 | equal(every(elem => elem >= 16,[17,18,19,42]), true); 7 | }); 8 | -------------------------------------------------------------------------------- /tests/or.js: -------------------------------------------------------------------------------- 1 | import { 2 | ok 3 | } 4 | from 'assert'; 5 | import or from '../or'; 6 | 7 | test('#or', () => { 8 | ok(or(true, true)); 9 | ok(or(false, true)); 10 | ok(or(true, false)); 11 | ok(!or(false, false)); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/product.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import product from '../product'; 3 | 4 | test('#product', () => { 5 | equal(product([2, 3, 4]), 24); 6 | equal(product([]), 1); 7 | equal(product([{valueOf: () => 10}]), 10); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/xor.js: -------------------------------------------------------------------------------- 1 | import { 2 | equal 3 | } 4 | from 'assert'; 5 | import xor from '../xor'; 6 | 7 | test('#xor', () => { 8 | equal(xor(1, 0), 1); 9 | equal(xor(1, 1), 0); 10 | equal(xor(0, 1), 1); 11 | equal(xor(0, 0), 0); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/forEach.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import forEach from '../forEach'; 3 | 4 | test('#forEach', () => { 5 | let result = []; 6 | forEach(i => result.push(Math.sqrt(i)), [9, 16, 25]) 7 | deepEqual(result, [3, 4, 5]); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/and.js: -------------------------------------------------------------------------------- 1 | import { 2 | ok 3 | } 4 | from 'assert'; 5 | import and from '../and'; 6 | 7 | test('#and', () => { 8 | ok(and(true, true)); 9 | ok(!and(false, true)); 10 | ok(!and(true, false)); 11 | ok(!and(false, false)); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/greaterOrEqual.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import greaterOrEqual from '../greaterOrEqual'; 3 | 4 | test('#greaterOrEqual', () => { 5 | ok(greaterOrEqual(2, 1)); 6 | ok(greaterOrEqual(1, 1)); 7 | ok(!greaterOrEqual(1, 2)); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/startsWithAt.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import startsWithAt from '../startsWithAt'; 3 | 4 | test('#startsWithAt', () => { 5 | ok(startsWithAt(2, 'liners', '1-liners/startsWithAt')); 6 | ok(!startsWithAt(2, 'stoeffel', 'nope')); 7 | }); 8 | -------------------------------------------------------------------------------- /tests/flatMap.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import flatMap from '../flatMap'; 3 | 4 | test('#flatMap', () => { 5 | let arr = [1, 2, 3]; 6 | deepEqual(flatMap((x) => [x, x], arr), [1, 1, 2, 2, 3, 3]); 7 | deepEqual(arr, [1, 2, 3]); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/fold.js: -------------------------------------------------------------------------------- 1 | import { equal, deepEqual } from 'assert'; 2 | import fold from '../fold'; 3 | 4 | let sub = (x, y) => x - y; 5 | test('#fold', () => { 6 | let arr = [1, 2, 3, 4]; 7 | equal(fold(sub, 8, arr), -2); 8 | deepEqual(arr, [1, 2, 3, 4]); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/reduce.js: -------------------------------------------------------------------------------- 1 | import { equal, deepEqual } from 'assert'; 2 | import reduce from '../reduce'; 3 | 4 | let sub = (x, y) => x - y; 5 | test('#reduce', () => { 6 | let arr = [1, 2, 3, 4]; 7 | equal(reduce(sub, arr), -8); 8 | deepEqual(arr, [1, 2, 3, 4]); 9 | }); 10 | -------------------------------------------------------------------------------- /module/by.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/by 3 | * 4 | * @description 5 | * 6 | * Same as `a / b` 7 | * 8 | * @example 9 | * 10 | * var by = require('1-liners/by'); 11 | * 12 | * by(6, 2); // => 3 13 | * 14 | */ 15 | export default (a, b) => a / b; 16 | -------------------------------------------------------------------------------- /module/dec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/dec 3 | * 4 | * @description 5 | * 6 | * Same as `a - 1` 7 | * 8 | * @example 9 | * 10 | * var dec = require('1-liners/dec'); 11 | * 12 | * dec(1); // => 0 13 | * 14 | */ 15 | export default (val) => val - 1; 16 | -------------------------------------------------------------------------------- /module/inc.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/inc 3 | * 4 | * @description 5 | * 6 | * Same as `a + 1` 7 | * 8 | * @example 9 | * 10 | * var inc = require('1-liners/inc'); 11 | * 12 | * inc(1); // => 2 13 | * 14 | */ 15 | export default (val) => val + 1; 16 | -------------------------------------------------------------------------------- /tests/bitAnd.js: -------------------------------------------------------------------------------- 1 | import { 2 | ok 3 | } 4 | from 'assert'; 5 | import bitAnd from '../bitAnd'; 6 | 7 | test('#bitAnd', () => { 8 | ok(bitAnd(true, true)); 9 | ok(!bitAnd(false, true)); 10 | ok(!bitAnd(true, false)); 11 | ok(!bitAnd(false, false)); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/isTrue.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isTrue from '../isTrue'; 3 | 4 | test('#isTrue', () => { 5 | ok(isTrue(true)); 6 | 7 | ok(!isTrue('yes')); 8 | ok(!isTrue([])); 9 | ok(!isTrue('')); 10 | ok(!isTrue(0)); 11 | ok(!isTrue(false)); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/hasOwnProperty.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import hasOwnProperty from '../hasOwnProperty'; 3 | 4 | test('#hasOwnProperty', () => { 5 | let object = {a: 1, b: 2}; 6 | equal(hasOwnProperty('a', object), true); 7 | equal(hasOwnProperty('c', object), false); 8 | }); 9 | -------------------------------------------------------------------------------- /tests/isFalsy.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isFalsy from '../isFalsy'; 3 | 4 | test('#isFalsy', () => { 5 | ok(!isFalsy('yes')); 6 | ok(!isFalsy(true)); 7 | ok(!isFalsy([])); 8 | 9 | ok(isFalsy('')); 10 | ok(isFalsy(0)); 11 | ok(isFalsy(false)); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/isFalse.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isFalse from '../isFalse'; 3 | 4 | test('#isFalse', () => { 5 | ok(isFalse(false)); 6 | 7 | ok(!isFalse('yes')); 8 | ok(!isFalse(true)); 9 | ok(!isFalse([])); 10 | ok(!isFalse('')); 11 | ok(!isFalse(0)); 12 | }); 13 | -------------------------------------------------------------------------------- /module/minus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/minus 3 | * 4 | * @description 5 | * 6 | * Same as `a - b` 7 | * 8 | * @example 9 | * 10 | * var minus = require('1-liners/minus'); 11 | * 12 | * minus(3, 2); // => 1 13 | * 14 | */ 15 | export default (a, b) => a - b; 16 | -------------------------------------------------------------------------------- /module/times.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/times 3 | * 4 | * @description 5 | * 6 | * Same as `a * b` 7 | * 8 | * @example 9 | * 10 | * var times = require('1-liners/times'); 11 | * 12 | * times(3, 2); // => 6 13 | * 14 | */ 15 | export default (a, b) => a * b; 16 | -------------------------------------------------------------------------------- /tests/average.js: -------------------------------------------------------------------------------- 1 | import { equal, ok } from 'assert'; 2 | import average from '../average'; 3 | 4 | const {isNaN} = Number; 5 | 6 | test('#average', () => { 7 | equal(average([2, 3, 4]), 3); 8 | equal(average([{valueOf: () => 10}]), 10); 9 | 10 | ok(isNaN(average([]))); 11 | }); 12 | -------------------------------------------------------------------------------- /tests/foldRight.js: -------------------------------------------------------------------------------- 1 | import { equal, deepEqual } from 'assert'; 2 | import foldRight from '../foldRight'; 3 | 4 | let sub = (x, y) => x - y; 5 | test('#foldRight', () => { 6 | let arr = [1, 2, 3, 4]; 7 | equal(foldRight(sub, 10, arr), 0); 8 | deepEqual(arr, [1, 2, 3, 4]); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/isTruthy.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isTruthy from '../isTruthy'; 3 | 4 | test('#isTruthy', () => { 5 | ok(isTruthy('yes')); 6 | ok(isTruthy(true)); 7 | ok(isTruthy([])); 8 | 9 | ok(!isTruthy('')); 10 | ok(!isTruthy(0)); 11 | ok(!isTruthy(false)); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/uncurry.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import uncurry from '../uncurry'; 3 | 4 | test('#uncurry', () => { 5 | const f = (a) => (b) => a + b; 6 | equal(uncurry(f)(1, 2), 3); 7 | 8 | const g = (a) => (b, c) => a + b + c; 9 | equal(uncurry(g)(1, 2, 3), 6); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/converge.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import converge from '../converge'; 3 | 4 | test('#converge', () => { 5 | let g = (a, b) => a + b; 6 | let f = (a, b) => a * b; 7 | let h = (a, b) => a / b 8 | 9 | equal(converge(f, g, h)(1, 2), f(g(1, 2), h(1, 2))); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/filter.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import filter from '../filter'; 3 | 4 | let isOdd = (x) => x % 2 !== 0; 5 | 6 | test('#filter', () => { 7 | let arr = [1, 2, 3, 4, 5]; 8 | deepEqual(filter(isOdd, arr), [1, 3, 5]); 9 | deepEqual(arr, [1, 2, 3, 4, 5]); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/method.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import method from '../method'; 3 | 4 | test('#method', () => { 5 | const object = { 6 | base: 1, 7 | add(number) { return this.base + number; }, 8 | }; 9 | 10 | equal( 11 | method('add', object)(5), 12 | 6 13 | ); 14 | }); 15 | -------------------------------------------------------------------------------- /tests/reduceRight.js: -------------------------------------------------------------------------------- 1 | import { equal, deepEqual } from 'assert'; 2 | import reduceRight from '../reduceRight'; 3 | 4 | let sub = (x, y) => x - y; 5 | test('#reduceRight', () => { 6 | let arr = [1, 2, 3, 4]; 7 | equal(reduceRight(sub, arr), -2); 8 | deepEqual(arr, [1, 2, 3, 4]); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/test.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import ourTest from '../test'; 3 | 4 | test('#test', () => { 5 | ok(ourTest('hayhayhayneedlehayhay', /needle/)); 6 | ok(ourTest('hAyHAYhayneEdLEHayHAy', /needle/i)); 7 | 8 | ok(!ourTest('hayhayhayneedlehayhay', /not there/)); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/bind.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import bind from '../bind'; 3 | 4 | test('#bind', () => { 5 | const Obj = { 6 | foo: 'foo', 7 | fun: function(a, b, c) { 8 | return this.foo + a + b + c; 9 | } 10 | }; 11 | equal(bind(Obj, [1, 2, 3], Obj.fun)(), 'foo123'); 12 | }); 13 | -------------------------------------------------------------------------------- /module/head.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/head 3 | * 4 | * @description 5 | * 6 | * Returns the first item of an array. 7 | * 8 | * @example 9 | * 10 | * var head = require('1-liners/head'); 11 | * 12 | * head([1, 2, 3]); // => 1 13 | * 14 | */ 15 | export default ([head,]) => head; 16 | -------------------------------------------------------------------------------- /module/length.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module length 3 | * 4 | * @description 5 | * 6 | * Returns the length of an array. 7 | * 8 | * @example 9 | * 10 | * var length = require('1-liners/length'); 11 | * 12 | * length([0, 1, 2]); // => 3 13 | * 14 | */ 15 | export default (arr) => arr.length; 16 | -------------------------------------------------------------------------------- /module/not.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/not 3 | * 4 | * @description 5 | * 6 | * Same as `!a`. 7 | * 8 | * @example 9 | * 10 | * var not = require('1-liners/not'); 11 | * 12 | * not(true); // => false 13 | * not(false); // => true 14 | * 15 | */ 16 | export default (a) => !a; 17 | -------------------------------------------------------------------------------- /module/tail.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/tail 3 | * 4 | * @description 5 | * 6 | * Returns the tail of an array 7 | * 8 | * @example 9 | * 10 | * var tail = require('1-liners/tail'); 11 | * 12 | * tail([1, 2, 3]); // => [2, 3] 13 | * 14 | */ 15 | export default ([,...tail]) => tail; 16 | -------------------------------------------------------------------------------- /tests/keys.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import keys from '../keys'; 3 | 4 | test('#keys', () => { 5 | const obj = { 100: 'a', 2: 'b', 7: 'c' }; 6 | deepEqual(keys(obj), ['2', '7', '100']); 7 | deepEqual(keys({}), []); 8 | deepEqual(keys([1, 2, 3, 4]), [0, 1, 2, 3]); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/looseEqual.js: -------------------------------------------------------------------------------- 1 | import { ok } from 'assert'; 2 | import looseEqual from '../looseEqual'; 3 | 4 | test('#looseEqual', () => { 5 | ok(looseEqual(true, true)); 6 | ok(!looseEqual(false, true)); 7 | ok(looseEqual(1, 1)); 8 | ok(looseEqual(1, true)); 9 | ok(looseEqual(0, false)); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/between.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import between from '../between'; 3 | 4 | test('#between', () => { 5 | equal( between(1, 10, 3) , 3 ); 6 | equal( between(1, 10, 2.5) , 2.5 ); 7 | equal( between(1, 10, -5) , 1 ); 8 | equal( between(1, 10, 25) , 10 ); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/butLast.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import butLast from '../butLast'; 3 | 4 | test('#butLast', () => { 5 | const array = [1, 2, 3]; 6 | 7 | deepEqual(butLast(array) , [1, 2] ); 8 | deepEqual(array , [1, 2, 3] ); 9 | 10 | deepEqual(butLast([]) , [] ); 11 | }); 12 | -------------------------------------------------------------------------------- /tests/put.js: -------------------------------------------------------------------------------- 1 | import { deepEqual, notDeepEqual } from 'assert'; 2 | import put from '../put'; 3 | 4 | test('#put', () => { 5 | const object = {id: 1}; 6 | 7 | deepEqual(put('name', 'stoeffel', object), { id: 1, name: 'stoeffel' }); 8 | notDeepEqual(put('name', 'stoeffel', object), object); 9 | }); 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /module/nth.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/nth 3 | * 4 | * @description 5 | * 6 | * Returns the nth item of an array. 7 | * 8 | * @example 9 | * 10 | * var nth = require('1-liners/nth'); 11 | * 12 | * nth(1, [1, 2, 3]); // => 2 13 | * 14 | 15 | */ 16 | export default (n, arr) => arr[n]; 17 | -------------------------------------------------------------------------------- /tests/pipe.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import pipe from '../pipe'; 3 | 4 | test('#pipe', () => { 5 | let g = (a, b) => a + b; 6 | let f = (a) => a + a; 7 | let h = (a) => a * -1; 8 | 9 | equal(pipe(g, f)(1, 2), f(g(1, 2))); 10 | equal([g, f, h].reduce(pipe)(1, 2), h(f(g(1, 2)))); 11 | }); 12 | -------------------------------------------------------------------------------- /tests/values.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import values from '../values'; 3 | 4 | test('#values', () => { 5 | const obj = { 100: 'a', 2: 'b', 7: 'c' }; 6 | deepEqual(values(obj), ['b', 'c', 'a']); 7 | deepEqual(values({}), []); 8 | deepEqual(values([1, 'b', 2]), [1, 'b', 2]); 9 | }); 10 | -------------------------------------------------------------------------------- /module/bitOr.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/bitOr 3 | * 4 | * @description 5 | * 6 | * Same as `a | b`. 7 | * 8 | * @example 9 | * 10 | * var bitOr = require('1-liners/bitOr'); 11 | * 12 | * bitOr(0, 1); // => 1 13 | * bitOr(1, 1); // => 1 14 | * 15 | */ 16 | export default (x, y) => x | y; 17 | -------------------------------------------------------------------------------- /module/last.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/last 3 | * 4 | * @description 5 | * 6 | * Returns the last item of `array`. 7 | * 8 | * @example 9 | * 10 | * var last = require('1-liners/last'); 11 | * 12 | * last([1, 2, 3]); // => 3 13 | * 14 | */ 15 | export default (array) => array[array.length - 1]; 16 | -------------------------------------------------------------------------------- /module/bitAnd.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/bitAnd 3 | * 4 | * @description 5 | * 6 | * Same as `a & b`. 7 | * 8 | * @example 9 | * 10 | * var bitAnd = require('1-liners/bitAnd'); 11 | * 12 | * bitAnd(1, 2); // => 0 13 | * bitAnd(2, 2); // => 2 14 | * 15 | */ 16 | export default (x, y) => x & y; 17 | -------------------------------------------------------------------------------- /module/nor.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/nor 3 | * 4 | * @description 5 | * 6 | * Same as `!(a || b)`. 7 | * 8 | * @example 9 | * 10 | * var nor = require('1-liners/nor'); 11 | * 12 | * nor(0, 0); // => true 13 | * nor(1, 0); // => false 14 | * 15 | */ 16 | export default (x, y) => !(x || y); 17 | -------------------------------------------------------------------------------- /module/plus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/plus 3 | * 4 | * @description 5 | * 6 | * Same as `a + b`. 7 | * 8 | * @example 9 | * 10 | * var plus = require('1-liners/plus'); 11 | * 12 | * plus(2, 8); // => 10 13 | * plus('a', 'b'); // => 'ab' 14 | * 15 | */ 16 | export default (a, b) => a + b; 17 | -------------------------------------------------------------------------------- /tests/ifThen.js: -------------------------------------------------------------------------------- 1 | import { equal, deepEqual } from 'assert'; 2 | import ifThen from '../ifThen'; 3 | 4 | test('#ifThen', () => { 5 | let words = ifThen((str) => typeof str === 'string', (str) => str.split(' ')); 6 | 7 | deepEqual(words('Hello ES2015'), ['Hello', 'ES2015']); 8 | equal(words(1), undefined); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/property.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import property from '../property'; 3 | 4 | test('#property', () => { 5 | const object = {foo: 1, baz: [3]}; 6 | 7 | equal(property('foo', object), 1); 8 | equal(property('bar', object), undefined); 9 | equal(property('baz', object), object.baz); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/push.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import push from '../push'; 3 | 4 | test('#push', () => { 5 | let arr = [0, 1, 2]; 6 | let clone = [0, 1, 2]; 7 | deepEqual(push(3, arr), [0, 1, 2, 3]); 8 | 9 | clone.push(3); 10 | deepEqual(push(3, arr), clone); 11 | deepEqual(arr, [0, 1, 2]); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/replace.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import replace from '../replace'; 3 | 4 | test('#replace', () => { 5 | equal(replace(/\d+/g, sub => `"${sub}"`, 'Items: 3,2'), `Items: "3","2"`); 6 | let str = 'Items: 3,2'; 7 | equal(replace(':', '=', str), 'Items= 3,2'); 8 | equal(str, 'Items: 3,2'); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/uncurry3.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import uncurry3 from '../uncurry3'; 3 | 4 | test('#uncurry3', () => { 5 | const f = (a) => (b) => (c) => a + b + c; 6 | equal(uncurry3(f)(1, 2, 3), 6); 7 | 8 | const g = (a) => (b) => (c, d) => a + b + c + d; 9 | equal(uncurry3(g)(1, 2, 3, 4), 10); 10 | }); 11 | -------------------------------------------------------------------------------- /module/and.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/and 3 | * 4 | * @description 5 | * 6 | * Same as `a && b`. 7 | * 8 | * @example 9 | * 10 | * var and = require('1-liners/and'); 11 | * 12 | * and(true, true); // => true 13 | * and(false, true); // => false 14 | * 15 | */ 16 | export default (x, y) => x && y; 17 | -------------------------------------------------------------------------------- /module/map.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/map 3 | * 4 | * @description 5 | * 6 | * Same as `[1, 2, 3].map(Math.sqrt)`. 7 | * 8 | * @example 9 | * 10 | * var map = require('1-liners/map'); 11 | * 12 | * map(Math.sqrt, [9, 25]); // => [3, 5] 13 | * 14 | */ 15 | export default (map, arr) => arr.map(map); 16 | -------------------------------------------------------------------------------- /module/nand.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/nand 3 | * 4 | * @description 5 | * 6 | * Same as `!(a && b)`. 7 | * 8 | * @example 9 | * 10 | * var nand = require('1-liners/nand'); 11 | * 12 | * nand(0, 0); // => true 13 | * nand(1, 1); // => false 14 | * 15 | */ 16 | export default (x, y) => !(x && y); 17 | -------------------------------------------------------------------------------- /tests/extend.js: -------------------------------------------------------------------------------- 1 | import { deepEqual, notDeepEqual } from 'assert'; 2 | import extend from '../extend'; 3 | 4 | test('#extend', () => { 5 | const object = {id: 1}; 6 | 7 | deepEqual(extend({name: 'stoeffel'}, object), { id: 1, name: 'stoeffel' }); 8 | notDeepEqual(extend({name: 'stoeffel'}, object), object); 9 | }); 10 | -------------------------------------------------------------------------------- /tests/pipeAll.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import pipeAll from '../pipeAll'; 3 | 4 | test('#pipeAll', () => { 5 | let g = (a, b) => a + b; 6 | let f = (a) => a + a; 7 | let h = (a) => a * -1; 8 | 9 | equal(pipeAll([g, f])(1, 2), f(g(1, 2))); 10 | equal(pipeAll([g, f, h])(1, 2), h(f(g(1, 2)))); 11 | }); 12 | -------------------------------------------------------------------------------- /module/pipe.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/pipe 3 | * 4 | * @description 5 | * 6 | * Pipe arguments through functions. 7 | * 8 | * @example 9 | * 10 | * var pipe = require('1-liners/pipe'); 11 | * 12 | * pipe(f, g)(1, 2) === g(f(1, 2)); 13 | * 14 | */ 15 | export default (f, g) => (...args) => g(f(...args)); 16 | -------------------------------------------------------------------------------- /module/xor.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/xor 3 | * 4 | * @description 5 | * 6 | * Same as `(x && !y) || (!x && y)` 7 | * 8 | * @example 9 | * 10 | * var xor = require('1-liners/xor'); 11 | * 12 | * xor(0, 1); // => 1 13 | * xor(1, 1); // => 0 14 | * 15 | */ 16 | export default (x, y) => (x && !y) || (!x && y) 17 | -------------------------------------------------------------------------------- /tests/compose.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import compose from '../compose'; 3 | 4 | test('#compose', () => { 5 | let g = (a, b) => a + b; 6 | let f = (a) => a + a; 7 | let h = (a) => a * -1; 8 | 9 | equal(compose(f, g)(1, 2), f(g(1, 2))); 10 | equal([h, f, g].reduce(compose)(1, 2), h(f(g(1, 2)))); 11 | }); 12 | -------------------------------------------------------------------------------- /tests/isBetween.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import isBetween from '../isBetween'; 3 | 4 | test('#isBetween', () => { 5 | equal( isBetween(1, 10, 3) , true ); 6 | equal( isBetween(1, 10, 2.5) , true ); 7 | equal( isBetween(1, 10, -5) , false ); 8 | equal( isBetween(1, 10, 25) , false ); 9 | }); 10 | -------------------------------------------------------------------------------- /module/reduce.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/reduce 3 | * 4 | * @description 5 | * 6 | * Same as `[1, 2, 3].reduce(sum)`. 7 | * 8 | * @example 9 | * 10 | * var reduce = require('1-liners/reduce'); 11 | * 12 | * reduce(sum, [1, 2, 3]); // => 6 13 | * 14 | */ 15 | export default (reduce, arr) => arr.reduce(reduce); 16 | -------------------------------------------------------------------------------- /module/split.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/split 3 | * 4 | * @description 5 | * 6 | * Same as `'1-liners'.split('-')` 7 | * 8 | * @example 9 | * 10 | * var split = require('1-liners/split'); 11 | * 12 | * split('-', '1-liners'); // => [1, 'liners'] 13 | * 14 | */ 15 | export default (split, str) => str.split(split); 16 | -------------------------------------------------------------------------------- /tests/composeAll.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import composeAll from '../composeAll'; 3 | 4 | test('#composeAll', () => { 5 | let g = (a, b) => a + b; 6 | let f = (a) => a + a; 7 | let h = (a) => a * -1; 8 | 9 | equal(composeAll([f, g])(1, 2), f(g(1, 2))); 10 | equal(composeAll([h, f, g])(1, 2), h(f(g(1, 2)))); 11 | }); 12 | -------------------------------------------------------------------------------- /tests/flip.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import flip from '../flip'; 3 | 4 | test('#flip', () => { 5 | const f = (a, b) => a / b; 6 | equal(flip(f)(2, 6), 3); 7 | equal(flip(flip(f))(6, 2), 3); 8 | 9 | const g = (a, b, c) => a / (b + c); 10 | equal(flip(g)(1, 2, 3), 1); 11 | equal(flip(flip(g))(3, 2, 1), 1); 12 | }); 13 | -------------------------------------------------------------------------------- /module/filter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/filter 3 | * 4 | * @description 5 | * 6 | * Same as `[1, 2, 3].filter(isOdd)`. 7 | * 8 | * @example 9 | * 10 | * var filter = require('1-liners/filter'); 11 | * 12 | * filter(isOdd, [1, 2, 3]); // => [1, 3] 13 | * 14 | */ 15 | export default (filter, arr) => arr.filter(filter); 16 | -------------------------------------------------------------------------------- /module/join.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/join 3 | * 4 | * @description 5 | * 6 | * Same as `[1, 'liners'].join('-')` 7 | * 8 | * @example 9 | * 10 | * var join = require('1-liners/join'); 11 | * 12 | * join('-', [1, 'liners']); // => '1-liners' 13 | * 14 | */ 15 | export default (superglue, arr) => arr.join(superglue); 16 | -------------------------------------------------------------------------------- /module/some.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/some 3 | * 4 | * @description 5 | * 6 | * Same as `[1,2,3].some(GreaterThan16)` 7 | * 8 | * @example 9 | * 10 | * var some = require('1-liners/some'); 11 | * 12 | * some(elem => elem > 16, [16,17,18]); // => true 13 | * 14 | */ 15 | export default (some, arr) => arr.some(some); 16 | -------------------------------------------------------------------------------- /module/take.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/take 3 | * 4 | * @description 5 | * 6 | * Take n items of an array. Same as `arr.slice(0, n)`. 7 | * 8 | * @example 9 | * 10 | * var take = require('1-liners/take'); 11 | * 12 | * take(2, [1, 2, 3]); // => [1, 2] 13 | * 14 | */ 15 | export default (take, arr) => arr.slice(0, take); 16 | -------------------------------------------------------------------------------- /tests/take.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import take from '../take'; 3 | 4 | test('#take', () => { 5 | deepEqual(take(0, [0, 1, 2]), []); 6 | deepEqual(take(1, [0, 1, 2]), [0]); 7 | deepEqual(take(2, [0, 1, 2]), [0, 1]); 8 | deepEqual(take(3, [0, 1, 2]), [0, 1, 2]); 9 | deepEqual(take(4, [0, 1, 2]), [0, 1, 2]); 10 | }); 11 | -------------------------------------------------------------------------------- /module/match.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/match 3 | * 4 | * @description 5 | * 6 | * Same as `haystack.match(needle)`. 7 | * 8 | * @example 9 | * 10 | * var match = require('1-liners/match'); 11 | * 12 | * match(/\d+/g, 'Items: 3,2'); // => ["3", "2"] 13 | * 14 | */ 15 | export default (needle, haystack) => haystack.match(needle); 16 | -------------------------------------------------------------------------------- /tests/identity.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import identity from '../identity'; 3 | 4 | test('#identity', () => { 5 | deepEqual(identity(true), true); 6 | deepEqual(identity("string"), "string"); 7 | deepEqual(identity(1), 1); 8 | deepEqual(identity({ foo: 1 }), { foo: 1 }); 9 | deepEqual(identity([42]), [42]); 10 | }); 11 | -------------------------------------------------------------------------------- /tests/ifThenElse.js: -------------------------------------------------------------------------------- 1 | import { equal, deepEqual } from 'assert'; 2 | import ifThenElse from '../ifThenElse'; 3 | 4 | test('#ifThenElse', () => { 5 | let eq = (a, b) => a === b; 6 | let add = (a, b) => a + b; 7 | let sub = (a, b) => a - b; 8 | 9 | equal(ifThenElse(eq, add, sub)(1, 1), 2); 10 | equal(ifThenElse(eq, add, sub)(2, 1), 1); 11 | }); 12 | -------------------------------------------------------------------------------- /module/every.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/every 3 | * 4 | * @description 5 | * 6 | * Same as `[1,2,3].every(GreaterThan16)`. 7 | * 8 | * @example 9 | * 10 | * var every = require('1-liners/every'); 11 | * 12 | * every(elem => elem > 16, [16,17,18]); // => false 13 | * 14 | */ 15 | export default (every, arr) => arr.every(every); 16 | -------------------------------------------------------------------------------- /module/or.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/or 3 | * 4 | * @description 5 | * 6 | * Same as `a || b`. 7 | * 8 | * @example 9 | * 10 | * var or = require('1-liners/or'); 11 | * 12 | * or(true, true); // => true 13 | * or(false, true); // => true 14 | * or(false, false); // => false 15 | * 16 | */ 17 | export default (a, b) => a || b; 18 | -------------------------------------------------------------------------------- /module/compose.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/compose 3 | * 4 | * @description 5 | * 6 | * Compose a new function from two given functions. 7 | * 8 | * @example 9 | * 10 | * var compose = require('1-liners/compose'); 11 | * 12 | * compose(f, g)(1, 2) === f(g(1, 2)); 13 | * 14 | */ 15 | export default (f, g) => (...args) => f(g(...args)); 16 | -------------------------------------------------------------------------------- /tests/implode.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import implode from '../implode'; 3 | 4 | test('#implode', () => { 5 | let f = (a) => a + a; 6 | let g = (a, b, c) => a * b * c; 7 | let h = (a, b) => a * b; 8 | 9 | equal(implode(f)([1]), f(1)); 10 | equal(implode(g)([1, 2, 3]), g(1, 2, 3)); 11 | equal(implode(h)([1, 2, 3]), h(1, 2, 3)); 12 | }); 13 | -------------------------------------------------------------------------------- /module/sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/sum 3 | * 4 | * @description 5 | * 6 | * Sums all items of an `array`. 7 | * 8 | * @example 9 | * 10 | * const sum = require('1-liners/sum'); 11 | * 12 | * sum([1, 2, 3]); // => 6 13 | * sum([]); // => 0 14 | * 15 | */ 16 | export default (array) => array.reduce((a, b) => (a + b), 0); 17 | -------------------------------------------------------------------------------- /module/converge.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/converge 3 | * 4 | * @description 5 | * 6 | * Converge two functions into one. 7 | * 8 | * @example 9 | * 10 | * var converge = require('1-liners/converge'); 11 | * 12 | * converge(f, g, h)(1, 2) === f(g(1, 2), h(1, 2)); 13 | * 14 | */ 15 | export default (f, g, h) => (...args) => f(g(...args), h(...args)); 16 | -------------------------------------------------------------------------------- /module/lessThan.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/lessThan 3 | * 4 | * @description 5 | * 6 | * Same as `a < b`. 7 | * 8 | * @example 9 | * 10 | * var lessThan = require('1-liners/lessThan'); 11 | * 12 | * lessThan(1, 2); // => true 13 | * lessThan(1, 1); // => false 14 | * lessThan(2, 1); // => false 15 | * 16 | */ 17 | export default (x, y) => x < y; 18 | -------------------------------------------------------------------------------- /module/reduceRight.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/reduceRight 3 | * 4 | * @description 5 | * 6 | * Same as `[1, 2, 3].reduceRight(sub)`. 7 | * 8 | * @example 9 | * 10 | * var reduceRight = require('1-liners/reduceRight'); 11 | * 12 | * reduceRight(sub, [1, 2, 3]); // => -4 13 | * 14 | */ 15 | export default (reduce, arr) => arr.reduceRight(reduce); 16 | -------------------------------------------------------------------------------- /module/equal.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/equal 3 | * 4 | * @description 5 | * 6 | * Same as `a === b`. 7 | * 8 | * @example 9 | * 10 | * var equal = require('1-liners/equal'); 11 | * 12 | * equal(true, true); // => true 13 | * equal(false, true); // => false 14 | * equal(1, true); // => false 15 | * 16 | */ 17 | export default (x, y) => x === y; 18 | -------------------------------------------------------------------------------- /module/property.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/property 3 | * 4 | * @description 5 | * 6 | * Same as `object[property]` 7 | * 8 | * @example 9 | * 10 | * const property = require('1-liners/property'); 11 | * 12 | * const object = {foo: 1}; 13 | * 14 | * property('foo', object); // => 1 15 | * 16 | */ 17 | export default (property, object) => object[property]; 18 | -------------------------------------------------------------------------------- /module/noop.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/noop 3 | * 4 | * @description 5 | * 6 | * Same as `function(){}`. 7 | * 8 | * @example 9 | * 10 | * var noop = require('1-liners/noop'); 11 | * 12 | * window.console = { 13 | * log: noop, 14 | * error: noop, 15 | * warn: noop, 16 | * table: noop 17 | * }; 18 | * 19 | */ 20 | export default () => {}; 21 | -------------------------------------------------------------------------------- /tests/curryRight.js: -------------------------------------------------------------------------------- 1 | import {equal} from 'assert'; 2 | import curryRight from '../curryRight'; 3 | 4 | test('#curryRight', () => { 5 | const g = (a, b, c, d) => a + b * c - d; 6 | const gλ = curryRight(g); 7 | 8 | equal( 9 | gλ(4)(1, 2, 3), 10 | 3 11 | ); 12 | equal( 13 | gλ(3, 4)(1, 2), 14 | 3 15 | ); 16 | equal( 17 | gλ(2, 3, 4)(1), 18 | 3 19 | ); 20 | }); 21 | -------------------------------------------------------------------------------- /tests/signum.js: -------------------------------------------------------------------------------- 1 | import { equal, ok } from 'assert'; 2 | import signum from '../signum'; 3 | 4 | test('#signum', () => { 5 | equal(signum(-5), -1); 6 | equal(signum(-Infinity), -1); 7 | 8 | equal(signum(10), 1); 9 | equal(signum(Infinity), 1); 10 | 11 | equal(signum(0), 0); 12 | equal(signum(-0), 0); 13 | 14 | ok(Number.isNaN(signum(NaN))); 15 | }); 16 | -------------------------------------------------------------------------------- /module/greaterThan.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/greaterThan 3 | * 4 | * @description 5 | * 6 | * Same as `a > b`. 7 | * 8 | * @example 9 | * 10 | * var greaterThan = require('1-liners/greaterThan'); 11 | * 12 | * greaterThan(2, 1); // => true 13 | * greaterThan(2, 2); // => false 14 | * greaterThan(1, 2); // => false 15 | * 16 | */ 17 | export default (x, y) => x > y; 18 | -------------------------------------------------------------------------------- /module/lessOrEqual.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/lessOrEqual 3 | * 4 | * @description 5 | * 6 | * Same as `a <= b`. 7 | * 8 | * @example 9 | * 10 | * var lessOrEqual = require('1-liners/lessOrEqual'); 11 | * 12 | * lessOrEqual(1, 2); // => true 13 | * lessOrEqual(1, 1); // => true 14 | * lessOrEqual(2, 1); // => false 15 | * 16 | */ 17 | export default (x, y) => x <= y; 18 | -------------------------------------------------------------------------------- /module/forEach.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/forEach 3 | * 4 | * @description 5 | * 6 | * Same as `[1, 2, 3].forEach(Math.sqrt)`. 7 | * 8 | * @example 9 | * 10 | * var forEach = require('1-liners/forEach'); 11 | * 12 | * forEach(i => console.log('Item: ' + i), [9, 25]); // => logs "Item: 9" and "Item: 25" 13 | * 14 | */ 15 | export default (forEach, arr) => arr.forEach(forEach); 16 | -------------------------------------------------------------------------------- /module/pipeAll.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/pipeAll 3 | * 4 | * @description 5 | * 6 | * Pipe arguments through an array of functions. 7 | * 8 | * @example 9 | * 10 | * var pipeAll = require('1-liners/pipeAll'); 11 | * 12 | * pipeAll([f, g, h])(1, 2) === h(g(f(1, 2))); 13 | * 14 | */ 15 | export default (fns) => fns.reverse().reduce( (f, g) => (...args) => f(g(...args)) ); 16 | -------------------------------------------------------------------------------- /module/flip.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/flip 3 | * 4 | * @description 5 | * 6 | * Flip a function’s arguments. 7 | * 8 | * @example 9 | * 10 | * var flip = require('1-liners/flip'); 11 | * 12 | * var f = (a, b) => a / b; 13 | * 14 | * flip(f)(2, 6); // => 3 15 | * flip(flip(f))(6, 2); // => 3 16 | * 17 | */ 18 | export default (f) => (...args) => f(...args.reverse()); 19 | -------------------------------------------------------------------------------- /module/looseEqual.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/looseEqual 3 | * 4 | * @description 5 | * 6 | * Same as `a == b`. 7 | * 8 | * @example 9 | * 10 | * var looseEqual = require('1-liners/looseEqual'); 11 | * 12 | * looseEqual(true, true); // => true 13 | * looseEqual(false, true); // => false 14 | * looseEqual(1, true); // => true 15 | * 16 | */ 17 | export default (x, y) => x == y; 18 | -------------------------------------------------------------------------------- /module/product.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/product 3 | * 4 | * @description 5 | * 6 | * Returns the product of all items of an `array`. 7 | * 8 | * @example 9 | * 10 | * const product = require('1-liners/product'); 11 | * 12 | * product([2, 3, 4]); // => 24 13 | * product([]); // => 1 14 | * 15 | */ 16 | export default (array) => array.reduce((a, b) => (a * b), 1); 17 | -------------------------------------------------------------------------------- /module/composeAll.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/composeAll 3 | * 4 | * @description 5 | * 6 | * Compose a new function with a given array of functions. 7 | * 8 | * @example 9 | * 10 | * var composeAll = require('1-liners/composeAll'); 11 | * 12 | * composeAll([f, g, h])(1, 2) === f(g(h(1, 2))); 13 | * 14 | */ 15 | export default (fns) => fns.reduce( (f, g) => (...args) => f(g(...args)) ); 16 | -------------------------------------------------------------------------------- /module/greaterOrEqual.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/greaterOrEqual 3 | * 4 | * @description 5 | * 6 | * Same as `a >= b`. 7 | * 8 | * @example 9 | * 10 | * var greaterOrEqual = require('1-liners/greaterOrEqual'); 11 | * 12 | * greaterOrEqual(2, 1); // => true 13 | * greaterOrEqual(2, 2); // => true 14 | * greaterOrEqual(1, 2); // => false 15 | * 16 | */ 17 | export default (x, y) => x >= y; 18 | -------------------------------------------------------------------------------- /module/push.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/push 3 | * 4 | * @description 5 | * 6 | * Same as [push](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push) but immutable. 7 | * 8 | * @example 9 | * 10 | * var push = require('1-liners/push'); 11 | * 12 | * push(4, [1, 2, 3]); // => [1, 2, 3, 4] 13 | * 14 | */ 15 | export default (element, arr) => [...arr, element]; 16 | -------------------------------------------------------------------------------- /module/explode.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/explode 3 | * 4 | * @description 5 | * 6 | * The opposite of [implode](#implode). 7 | * 8 | * @example 9 | * 10 | * var explode = require('1-liners/explode'); 11 | * 12 | * const sum = (numbers) => numbers.reduce((a, b) => a + b); 13 | * 14 | * explode(sum)(1, 2, 3, 4); // => 10 15 | * 16 | */ 17 | export default (func) => (...args) => func(args); 18 | -------------------------------------------------------------------------------- /module/flatMap.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/flatMap 3 | * 4 | * @description 5 | * 6 | * Map a function over a collection and flatten the result by one-level. 7 | * 8 | * @example 9 | * 10 | * var flatMap = require('1-liners/flatMap'); 11 | * 12 | * flatMap((x) => [x, x], [1, 2, 3]); // => [1, 1, 2, 2, 3, 3] 13 | * 14 | */ 15 | export default (fn, array) => [].concat.apply([], array.map(fn)); 16 | -------------------------------------------------------------------------------- /module/average.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/average 3 | * 4 | * @description 5 | * 6 | * Returns the average of all items of an `array`. 7 | * 8 | * @example 9 | * 10 | * const average = require('1-liners/average'); 11 | * 12 | * average([2, 3, 4]); // => 3 13 | * average([]); // => NaN 14 | * 15 | */ 16 | export default (array) => array.reduce((a, b) => (a + b), 0) / array.length; 17 | -------------------------------------------------------------------------------- /module/butLast.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/butLast 3 | * 4 | * @description 5 | * 6 | * Return a copy of `array`, without the last item. 7 | * 8 | * @example 9 | * 10 | * import butLast from '1-liners/butLast'; 11 | * 12 | * const array = [1, 2, 3]; 13 | * 14 | * butLast(array); // => [1, 2] 15 | * array; // => [1, 2, 3] 16 | * 17 | */ 18 | export default (array) => array.slice(0, -1); 19 | -------------------------------------------------------------------------------- /module/fold.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/fold 3 | * 4 | * @description 5 | * 6 | * Same as [`array.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/array/reduce). 7 | * 8 | * @example 9 | * 10 | * var fold = require('1-liners/fold'); 11 | * 12 | * fold(sum, 8, [1, 2, 3]); // => 2 13 | * 14 | */ 15 | export default (fold, initial, arr) => arr.reduce(fold, initial); 16 | -------------------------------------------------------------------------------- /module/put.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/put 3 | * 4 | * @description 5 | * 6 | * Same as `Object.assign({}, obj, {[key]: val})` 7 | * 8 | * @example 9 | * 10 | * const put = require('1-liners/put'); 11 | * 12 | * const object = {id: 1}; 13 | * 14 | * put('name', 'stoeffel', object); // => { id: 1, name: 'stoeffel' } 15 | * 16 | */ 17 | export default (key, val, obj) => Object.assign({}, obj, {[key]: val}); 18 | -------------------------------------------------------------------------------- /tests/explode.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import implode from '../implode'; 3 | import explode from '../explode'; 4 | 5 | test('#explode', () => { 6 | let f = implode((a) => a + a); 7 | let g = implode((a, b, c) => a * b * c); 8 | let h = implode((a, b) => a * b); 9 | 10 | equal(explode(f)(1), f([1])); 11 | equal(explode(g)(1, 2, 3), g([1, 2, 3])); 12 | equal(explode(h)(1, 2, 3), h([1, 2, 3])); 13 | }); 14 | -------------------------------------------------------------------------------- /module/max.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/max 3 | * 4 | * @description 5 | * 6 | * Same as `Math.max` – but with a stable number of arguments. 7 | * 8 | * @example 9 | * 10 | * var max = require('1-liners/max'); 11 | * 12 | * max(3, 6); // => 6 13 | * 14 | * [3, 6, 9].reduce(max); // => 9 15 | * [3, 6, 9].reduce(Math.max); // => NaN 16 | * 17 | */ 18 | export default (a, b) => ((a > b) ? a : b); 19 | -------------------------------------------------------------------------------- /module/min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/min 3 | * 4 | * @description 5 | * 6 | * Same as `Math.min` – but with a stable number of arguments. 7 | * 8 | * @example 9 | * 10 | * var min = require('1-liners/min'); 11 | * 12 | * min(3, 6); // => 3 13 | * 14 | * [3, 6, 1].reduce(min); // => 1 15 | * [3, 6, 1].reduce(Math.min); // => NaN 16 | * 17 | */ 18 | export default (a, b) => ((a > b) ? b : a); 19 | -------------------------------------------------------------------------------- /tests/concat.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import concat from '../concat'; 3 | 4 | test('#concat', () => { 5 | deepEqual( 6 | concat('are', [1, 'liners']), 7 | [1, 'liners', 'are'] 8 | ); 9 | 10 | deepEqual( 11 | concat(['are'], [1, 'liners']), 12 | [1, 'liners', 'are'] 13 | ); 14 | 15 | deepEqual( 16 | concat(['are', 'great'], [1, 'liners']), 17 | [1, 'liners', 'are', 'great'] 18 | ); 19 | }); 20 | -------------------------------------------------------------------------------- /module/identity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/identity 3 | * 4 | * @description 5 | * 6 | * Returns the value you pass to the function 7 | * 8 | * @example 9 | * 10 | * var identity = require('1-liners/identity'); 11 | * 12 | * identity(true); // => true 13 | * identity(1); // => 1 14 | * identity({ foo: 1 }); // => { foo: 1 } 15 | * identity("1-liners"); // => "1-liners" 16 | * 17 | */ 18 | export default I => I; 19 | -------------------------------------------------------------------------------- /module/isString.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isString 3 | * 4 | * @description 5 | * 6 | * Same as `typeof value === 'string'`. 7 | * 8 | * @example 9 | * 10 | * var isString = require('1-liners/isString'); 11 | * 12 | * isString(''); // => true 13 | * isString('anything'); // => true 14 | * 15 | * isString(/anything else/); // => false 16 | * 17 | */ 18 | export default (value) => typeof value === 'string'; 19 | -------------------------------------------------------------------------------- /tests/shallowClone.js: -------------------------------------------------------------------------------- 1 | import { ok, equal, deepEqual } from 'assert'; 2 | import shallowClone from '../shallowClone'; 3 | 4 | test('#shallowClone', () => { 5 | const source = { 6 | value: 'value', 7 | reference: /reference/, 8 | }; 9 | 10 | const target = shallowClone(source); 11 | 12 | ok(source !== target); 13 | deepEqual(source, target); 14 | equal(source.value, target.value); 15 | equal(source.reference, target.reference); 16 | }); 17 | -------------------------------------------------------------------------------- /module/extend.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/extend 3 | * 4 | * @description 5 | * 6 | * Returns a copy of `object`, extended by `extension`. 7 | * 8 | * @example 9 | * 10 | * const extend = require('1-liners/extend'); 11 | * 12 | * const object = {id: 1}; 13 | * 14 | * extend({ name: 'stoeffel' }, object); // => { id: 1, name: 'stoeffel' } 15 | * 16 | */ 17 | export default (extension, object) => Object.assign({}, object, extension); 18 | -------------------------------------------------------------------------------- /module/shave.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/shave 3 | * 4 | * @description 5 | * 6 | * Shave ensures that a function is called with n arguments. 7 | * 8 | * @example 9 | * 10 | * var shave = require('1-liners/shave'); 11 | * 12 | * map(parseInt, [0, 1.1, 2.2]); // => [0, NaN, NaN] 13 | * map(shave(1, parseInt), [0, 1.1, 2.2]); // => [0, 1, 2] 14 | * 15 | */ 16 | export default (shave, f) => (...args) => f(...(args.slice(0, shave))); 17 | -------------------------------------------------------------------------------- /tests/drop.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import drop from '../drop'; 3 | 4 | test('#drop', () => { 5 | const object = {foo: 1, bar: 2, baz: 3}; 6 | 7 | deepEqual( 8 | drop(['foo', 'baz'], object), 9 | {bar: 2} 10 | ); 11 | deepEqual( 12 | drop([], object), 13 | object 14 | ); 15 | deepEqual( 16 | drop(['oof'], object), 17 | object 18 | ); 19 | deepEqual( 20 | object, 21 | {foo: 1, bar: 2, baz: 3} 22 | ); 23 | }); 24 | -------------------------------------------------------------------------------- /module/foldRight.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/foldRight 3 | * 4 | * @description 5 | * 6 | * Same as [`array.reduceRight`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/array/reduceRight). 7 | * 8 | * @example 9 | * 10 | * var foldRight = require('1-liners/foldRight'); 11 | * 12 | * foldRight(sub, 1, [1, 2, 3]); // => -5 13 | * 14 | */ 15 | export default (fold, initial, arr) => arr.reduceRight(fold, initial); 16 | -------------------------------------------------------------------------------- /module/isNull.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isNull 3 | * 4 | * @description 5 | * 6 | * Same as `=== null`. 7 | * 8 | * @example 9 | * 10 | * var isNull = require('1-liners/isNull'); 11 | * 12 | * isNull(null); // => true 13 | * 14 | * isNull(undefined); // => false 15 | * isNull(NaN); // => false 16 | * isNull('anything else'); // => false 17 | * 18 | */ 19 | export default (value) => (value === null); 20 | -------------------------------------------------------------------------------- /module/values.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/values 3 | * 4 | * @description 5 | * 6 | * Get all values of an object 7 | * Same as `Object.keys(obj).map(i => obj[i])`. 8 | * 9 | * @example 10 | * 11 | * const values = require('1-liners/values'); 12 | * 13 | * values({ 100: 'a', 2: 'b', 7: 'c' }); // => ['a', 'b', 'c'] 14 | * values(['a', 'b', 'c']); // => ['a', 'b', 'c'] 15 | */ 16 | export default (obj) => Object.keys(obj).map(i => obj[i]); 17 | -------------------------------------------------------------------------------- /tests/isNull.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isNull from '../isNull'; 3 | 4 | test('#isNull', () => { 5 | ok(isNull(null)); 6 | 7 | ok(!isNull(0)); 8 | ok(!isNull(1)); 9 | ok(!isNull(false)); 10 | ok(!isNull(true)); 11 | ok(!isNull('')); 12 | ok(!isNull('non-empty string')); 13 | ok(!isNull(undefined)); 14 | ok(!isNull(NaN)); 15 | ok(!isNull(Infinity)); 16 | ok(!isNull([])); 17 | ok(!isNull({})); 18 | ok(!isNull(new Date())); 19 | }); 20 | -------------------------------------------------------------------------------- /module/keys.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/keys 3 | * 4 | * @description 5 | * 6 | * Same as [`Object.keys(obj)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys). 7 | * 8 | * @example 9 | * 10 | * const keys = require('1-liners/keys'); 11 | * 12 | * keys({ 100: 'a', 2: 'b', 7: 'c' }); // => ['2', '7', '100'] 13 | * keys([1, 2, 3]); // => [0, 1, 2] 14 | */ 15 | export default (obj) => Object.keys(obj); 16 | -------------------------------------------------------------------------------- /module/always.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/always 3 | * 4 | * @description 5 | * 6 | * Creates a function that always returns a given value 7 | * 8 | * @example 9 | * 10 | * var always = require('1-liners/always'); 11 | * 12 | * var T = always(true); 13 | * T(); // => true 14 | * T(); // => true 15 | * 16 | * var fortyTwo = always(42); 17 | * fortyTwo(); // => 42 18 | * fortyTwo(); // => 42 19 | * 20 | */ 21 | export default val => () => val; 22 | -------------------------------------------------------------------------------- /module/implode.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/implode 3 | * 4 | * @description 5 | * 6 | * Collapse a list of arguments into an array of arguments. 7 | * 8 | * @example 9 | * 10 | * var implode = require('1-liners/implode'); 11 | * 12 | * const f = (a, b) => a + b; 13 | * 14 | * [ 15 | * [1, 2], 16 | * [3, 4], 17 | * [5, 6], 18 | * ].map(implode(f)); // => [3, 7, 11] 19 | * 20 | */ 21 | export default (func) => (args) => func(...args); 22 | -------------------------------------------------------------------------------- /module/method.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/method 3 | * 4 | * @description 5 | * 6 | * Same as `object[method](...args)` 7 | * 8 | * @example 9 | * 10 | * const method = require('1-liners/method'); 11 | * 12 | * const object = { 13 | * base: 1, 14 | * add(number) { return this.base + number; }, 15 | * }; 16 | * 17 | * method('add', object)(5); // => 6 18 | * 19 | */ 20 | export default (method, object) => (...args) => object[method](...args); 21 | -------------------------------------------------------------------------------- /module/replace.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/replace 3 | * 4 | * @description 5 | * 6 | * Same as `haystack.replace(needle, replace)`. 7 | * 8 | * @example 9 | * 10 | * var replace = require('1-liners/replace'); 11 | * 12 | * replace(/\d+/g, sub => `"${sub}"`, 'Items: 3,2'); // => Items: "3","2" 13 | * replace(':', '=', 'Items: 3,2'); // => Items= 3,2 14 | * 15 | */ 16 | export default (needle, replace, haystack) => haystack.replace(needle, replace); 17 | -------------------------------------------------------------------------------- /tests/pick.js: -------------------------------------------------------------------------------- 1 | import { deepEqual } from 'assert'; 2 | import pick from '../pick'; 3 | 4 | test('#pick', () => { 5 | const object = {foo: 1, bar: 2, baz: 3}; 6 | 7 | deepEqual( 8 | pick(['foo', 'baz'], object), 9 | {foo: 1, baz: 3} 10 | ); 11 | deepEqual( 12 | pick([], object), 13 | {} 14 | ); 15 | deepEqual( 16 | pick(['oof'], object), 17 | {oof: undefined} 18 | ); 19 | deepEqual( 20 | object, 21 | {foo: 1, bar: 2, baz: 3} 22 | ); 23 | }); 24 | -------------------------------------------------------------------------------- /module/isFalsy.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isFalsy 3 | * 4 | * @description 5 | * 6 | * Same as `!`. 7 | * 8 | * @example 9 | * 10 | * var isFalsy = require('1-liners/isFalsy'); 11 | * 12 | * isFalsy('yes'); // => false 13 | * isFalsy(true); // => false 14 | * isFalsy([]); // => false 15 | * 16 | * isFalsy(''); // => true 17 | * isFalsy(0); // => true 18 | * isFalsy(false); // => true 19 | * 20 | */ 21 | export default (x) => !x; 22 | -------------------------------------------------------------------------------- /module/isBetween.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isBetween 3 | * 4 | * @description 5 | * 6 | * Check if the `number` lies between `min` and `max`, inclusive. 7 | * 8 | * @example 9 | * 10 | * var isBetween = require('1-liners/isBetween'); 11 | * 12 | * isBetween(1, 10, 2.5); // => true 13 | * isBetween(1, 10, -5); // => false 14 | * isBetween(1, 10, 25); // => false 15 | * 16 | */ 17 | export default (min, max, number) => (min <= number && number <= max); 18 | -------------------------------------------------------------------------------- /module/isFunction.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isFunction 3 | * 4 | * @description 5 | * 6 | * Same as `typeof value === 'function'`. 7 | * 8 | * @example 9 | * 10 | * var isFunction = require('1-liners/isFunction'); 11 | * 12 | * isFunction(function() {}); // => true 13 | * isFunction(function named() {}); // => true 14 | * 15 | * isFunction('any other value'); // => false 16 | * 17 | */ 18 | export default (value) => typeof value === 'function'; 19 | -------------------------------------------------------------------------------- /module/isTrue.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isTrue 3 | * 4 | * @description 5 | * 6 | * Same as `x === true`. 7 | * 8 | * @example 9 | * 10 | * var isTrue = require('1-liners/isTrue'); 11 | * 12 | * isTrue(true); // => true 13 | * 14 | * isTrue('yes'); // => false 15 | * isTrue([]); // => false 16 | * isTrue(''); // => false 17 | * isTrue(0); // => false 18 | * isTrue(false); // => false 19 | * 20 | */ 21 | export default (x) => x === true; 22 | -------------------------------------------------------------------------------- /module/takeUntil.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/takeUntil 3 | * 4 | * @description 5 | * 6 | * Take items of an array until they fulfill a predicate. 7 | * 8 | * @example 9 | * 10 | * var takeUntil = require('1-liners/takeUntil'); 11 | * 12 | * takeUntil(i => i % 2 === 1, [2, 4, 6, 8, 7, 8, 8]); // => [2, 4, 6, 8] 13 | * 14 | */ 15 | export default (pred, arr) => arr.reduce((newArr, i) => { if (pred(i)) arr.length = 0; else newArr.push(i); return newArr; }, []); 16 | -------------------------------------------------------------------------------- /module/takeWhile.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/takeWhile 3 | * 4 | * @description 5 | * 6 | * Take items of an array while they fulfill a predicate. 7 | * 8 | * @example 9 | * 10 | * var takeWhile = require('1-liners/takeWhile'); 11 | * 12 | * takeWhile(i => i % 2 === 0, [2, 4, 6, 8, 7, 8, 8]); // => [2, 4, 6, 8] 13 | * 14 | */ 15 | export default (pred, arr) => arr.reduce((newArr, i) => { if (!pred(i)) arr.length = 0; else newArr.push(i); return newArr; }, []); 16 | -------------------------------------------------------------------------------- /module/isFalse.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isFalse 3 | * 4 | * @description 5 | * 6 | * Same as `x === false`. 7 | * 8 | * @example 9 | * 10 | * var isFalse = require('1-liners/isFalse'); 11 | * 12 | * isFalse(false); // => true 13 | * 14 | * isFalse('yes'); // => false 15 | * isFalse(true); // => false 16 | * isFalse([]); // => false 17 | * isFalse(''); // => false 18 | * isFalse(0); // => false 19 | * 20 | */ 21 | export default (x) => x === false; 22 | -------------------------------------------------------------------------------- /module/isTruthy.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isTruthy 3 | * 4 | * @description 5 | * 6 | * Same as `!!`. 7 | * 8 | * @example 9 | * 10 | * var isTruthy = require('1-liners/isTruthy'); 11 | * 12 | * isTruthy('yes'); // => true 13 | * isTruthy(true); // => true 14 | * isTruthy([]); // => true 15 | * 16 | * isTruthy(''); // => false 17 | * isTruthy(0); // => false 18 | * isTruthy(false); // => false 19 | * 20 | */ 21 | export default (x) => !!x; 22 | -------------------------------------------------------------------------------- /module/pick.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/pick 3 | * 4 | * @description 5 | * 6 | * Copies only specified `properties` from an `object` into a new object. 7 | * 8 | * @example 9 | * 10 | * const pick = require('1-liners/pick'); 11 | * 12 | * const object = {foo: 1, bar: 2, baz: 3}; 13 | * 14 | * pick(['foo', 'baz'], object); // => {foo: 1, baz: 3} 15 | * 16 | */ 17 | export default (properties, object) => Object.assign({}, ...properties.map(key => ({[key]: object[key]}))); 18 | -------------------------------------------------------------------------------- /module/isBoolean.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isBoolean 3 | * 4 | * @description 5 | * 6 | * Same as `typeof value === 'boolean'`. 7 | * 8 | * @example 9 | * 10 | * var isBoolean = require('1-liners/isBoolean'); 11 | * 12 | * isBoolean(false); // => true 13 | * isBoolean(true); // => true 14 | * 15 | * isBoolean(null); // => false 16 | * isBoolean(/anything else/); // => false 17 | * 18 | */ 19 | export default (value) => typeof value === 'boolean'; 20 | -------------------------------------------------------------------------------- /tests/isUnknown.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isUnknown from '../isUnknown'; 3 | 4 | test('#isUnknown', () => { 5 | ok(isUnknown(null)); 6 | ok(isUnknown(undefined)); 7 | 8 | ok(!isUnknown(0)); 9 | ok(!isUnknown(1)); 10 | ok(!isUnknown(false)); 11 | ok(!isUnknown(true)); 12 | ok(!isUnknown('')); 13 | ok(!isUnknown('non-empty string')); 14 | ok(!isUnknown(NaN)); 15 | ok(!isUnknown(Infinity)); 16 | ok(!isUnknown([])); 17 | ok(!isUnknown({})); 18 | ok(!isUnknown(new Date())); 19 | }); 20 | -------------------------------------------------------------------------------- /module/between.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/between 3 | * 4 | * @description 5 | * 6 | * Return `number` if it’s greater than `min` and lower than `max`. Else return `min` or `max` respectively. 7 | * 8 | * @example 9 | * 10 | * var between = require('1-liners/between'); 11 | * 12 | * between(1, 10, 2.5); // => 2.5 13 | * between(1, 10, -5); // => 1 14 | * between(1, 10, 25); // => 10 15 | * 16 | */ 17 | export default (min, max, number) => (number < min ? min : (number > max ? max : number)); 18 | -------------------------------------------------------------------------------- /module/endsWith.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/endsWith 3 | * 4 | * @description 5 | * 6 | * Same as [`str.endsWith(searchString)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). 7 | * 8 | * @example 9 | * 10 | * const endsWith = require('1-liners/endsWith'); 11 | * 12 | * endsWith('liners', '1-liners'); // => true 13 | * endsWith('stoeffel', 'nope'); // => false 14 | * 15 | */ 16 | export default (searchString, str) => str.endsWith(searchString); 17 | -------------------------------------------------------------------------------- /module/test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/test 3 | * 4 | * @description 5 | * 6 | * Same as [`regexObj.test(str)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test). 7 | * 8 | * @example 9 | * 10 | * const test = require('1-liners/test'); 11 | * const haystack = 'hAyHAYhayneEdLEHayHAy'; 12 | * 13 | * test(haystack, /needle/); // => false 14 | * test(haystack, /needle/i); // => true 15 | * 16 | */ 17 | export default (str, regexObj) => regexObj.test(str); 18 | -------------------------------------------------------------------------------- /tests/exec.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import exec from '../exec'; 3 | 4 | const stringify = require('../curry')(require('../join'))('\0'); 5 | 6 | test('#test', () => { 7 | const haystack = 'hAyHAYhayneEdLEHayHAy'; 8 | 9 | equal( 10 | stringify(exec(haystack, /needle/i)), 11 | stringify(['neEdLE']) 12 | ); 13 | 14 | equal( 15 | stringify(exec(haystack, /n(.+)e/i)), 16 | stringify(['neEdLE', 'eEdL']) 17 | ); 18 | 19 | equal( 20 | exec(haystack, /needle/), 21 | null 22 | ); 23 | }); 24 | -------------------------------------------------------------------------------- /module/bind.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/bind 3 | * 4 | * @description 5 | * 6 | * Binds a context to a function. Same as [`fun.bind(thisArg[, arg1[, arg2[, ...]]])`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) 7 | * 8 | * @example 9 | * 10 | * var bind = require('1-liners/bind'); 11 | * 12 | * setTimeout(bind(console, ['Hello'], console.log), 2000); // => 'Hello' (after 2s) 13 | * 14 | */ 15 | export default (thisArg, args, fun) => fun.bind(thisArg, ...args); 16 | -------------------------------------------------------------------------------- /module/startsWith.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/startsWith 3 | * 4 | * @description 5 | * 6 | * Same as [`str.startsWith(searchString)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith). 7 | * 8 | * @example 9 | * 10 | * const startsWith = require('1-liners/startsWith'); 11 | * 12 | * startsWith('1', '1-liners'); // => true 13 | * startsWith('stoeffel', 'nope'); // => false 14 | * 15 | */ 16 | export default (searchString, str) => str.startsWith(searchString); 17 | -------------------------------------------------------------------------------- /module/uncurry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/uncurry 3 | * 4 | * @description 5 | * 6 | * Uncurry a function – collapse 2 lists of parameters into one. 7 | * 8 | * @example 9 | * 10 | * import uncurry from '1-liners/uncurry'; 11 | * 12 | * const f = (a) => (b) => a + b; 13 | * const fβ = uncurry(f); 14 | * fβ(1, 2); // => 3 15 | * 16 | * const g = (a) => (b, c) => a + b + c 17 | * const gβ = uncurry(g); 18 | * gβ(1, 2, 3); // => 6 19 | * 20 | */ 21 | export default (f) => (a, ...rest) => f(a)(...rest); 22 | -------------------------------------------------------------------------------- /module/isTypeOf.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isTypeOf 3 | * 4 | * @description 5 | * 6 | * Same as `typeof value === TYPE`. 7 | * 8 | * @example 9 | * 10 | * var isTypeOf = require('1-liners/isTypeOf'); 11 | * 12 | * isTypeOf('boolean', false); // => true 13 | * isTypeOf('boolean', true); // => true 14 | * 15 | * isTypeOf('boolean', null); // => false 16 | * isTypeOf('boolean', /anything else/); // => false 17 | * 18 | */ 19 | export default (type, value) => typeof value === type; 20 | -------------------------------------------------------------------------------- /module/hasOwnProperty.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/hasOwnProperty 3 | * 4 | * @description 5 | * 6 | * Same as [`obj.hasOwnProperty(prop)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty). 7 | * 8 | * @example 9 | * 10 | * const hasOwnProperty = require('1-liners/hasOwnProperty'); 11 | * 12 | * hasOwnProperty('a', {a: 1, b: 2}); // => true 13 | * hasOwnProperty('c', {a: 1, b: 2}); // => false 14 | * 15 | */ 16 | export default (prop, obj) => obj.hasOwnProperty(prop); 17 | -------------------------------------------------------------------------------- /tests/isUndefined.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isUndefined from '../isUndefined'; 3 | 4 | test('#isUndefined', () => { 5 | ok(isUndefined(undefined)); 6 | 7 | ok(!isUndefined(null)); 8 | ok(!isUndefined(0)); 9 | ok(!isUndefined(1)); 10 | ok(!isUndefined(false)); 11 | ok(!isUndefined(true)); 12 | ok(!isUndefined('')); 13 | ok(!isUndefined('non-empty string')); 14 | ok(!isUndefined(NaN)); 15 | ok(!isUndefined(Infinity)); 16 | ok(!isUndefined([])); 17 | ok(!isUndefined({})); 18 | ok(!isUndefined(new Date())); 19 | }); 20 | -------------------------------------------------------------------------------- /tests/isString.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isString from '../isString'; 3 | 4 | test('#isString', () => { 5 | ok(isString('')); 6 | ok(isString('non-empty string')); 7 | 8 | ok(!isString(undefined)); 9 | ok(!isString(null)); 10 | ok(!isString(0)); 11 | ok(!isString(1)); 12 | ok(!isString(false)); 13 | ok(!isString(true)); 14 | ok(!isString(NaN)); 15 | ok(!isString(Infinity)); 16 | ok(!isString(() => {})); 17 | ok(!isString(function named() {})); 18 | ok(!isString([])); 19 | ok(!isString({})); 20 | ok(!isString(/anything else/)); 21 | }); 22 | -------------------------------------------------------------------------------- /module/uncurry3.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/uncurry3 3 | * 4 | * @description 5 | * 6 | * Uncurry a function – collapse 3 lists of parameters into one. 7 | * 8 | * @example 9 | * 10 | * import uncurry3 from '1-liners/uncurry3'; 11 | * 12 | * const f = (a) => (b) => (c) => a + b + c; 13 | * const fβ = uncurry3(f); 14 | * fβ(1, 2, 3); // => 6 15 | * 16 | * const g = (a) => (b) => (c, d) => a + b + c + d; 17 | * const gβ = uncurry3(g); 18 | * gβ(1, 2, 3, 4); // => 10 19 | * 20 | */ 21 | export default (f) => (a, b, ...rest) => f(a)(b)(...rest); 22 | -------------------------------------------------------------------------------- /module/endsWithAt.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/endsWithAt 3 | * 4 | * @description 5 | * 6 | * Same as [`str.endsWith(searchString, position)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). 7 | * 8 | * @example 9 | * 10 | * const endsWithAt = require('1-liners/endsWithAt'); 11 | * 12 | * endsWithAt(8, 'liners', '1-liners/endsWithAt'); // => true 13 | * endsWithAt(2, 'stoeffel', 'nope'); // => false 14 | * 15 | */ 16 | export default (position, searchString, str) => str.endsWith(searchString, position); 17 | -------------------------------------------------------------------------------- /module/isUnknown.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isUnknown 3 | * 4 | * @description 5 | * 6 | * Same as `== null`. 7 | * 8 | * @example 9 | * 10 | * var isUnknown = require('1-liners/isUnknown'); 11 | * 12 | * isUnknown(null); // => true 13 | * isUnknown(undefined); // => true 14 | * 15 | * isUnknown(false); // => false 16 | * isUnknown(''); // => false 17 | * isUnknown(NaN); // => false 18 | * isUnknown(/anything else/); // => false 19 | * 20 | */ 21 | export default (value) => (value == null); 22 | -------------------------------------------------------------------------------- /module/drop.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/drop 3 | * 4 | * @description 5 | * 6 | * Creates a copy of the `object` without the given `props`. 7 | * 8 | * @example 9 | * 10 | * const drop = require('1-liners/drop'); 11 | * 12 | * const object = {foo: 1, bar: 2, baz: 3}; 13 | * 14 | * drop(['foo', 'baz'], object); // => {bar: 2} 15 | * object; // => {foo: 1, bar: 2, baz: 3} 16 | * 17 | */ 18 | export default (props:Array, object) => Object.keys(object).reduce((res, k) => Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {}); 19 | -------------------------------------------------------------------------------- /module/isObject.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isObject 3 | * 4 | * @description 5 | * 6 | * Same as `value !== null && typeof value === 'object'`. 7 | * 8 | * @example 9 | * 10 | * var isObject = require('1-liners/isObject'); 11 | * 12 | * isObject({}); // => true 13 | * isObject([]); // => true 14 | * isObject(/anything/); // => true 15 | * 16 | * isObject(null); // => false 17 | * isObject('anything else'); // => false 18 | * 19 | */ 20 | export default (value) => (value !== null && typeof value === 'object'); 21 | -------------------------------------------------------------------------------- /module/exec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/exec 3 | * 4 | * @description 5 | * 6 | * Same as [`regexObj.exec(str)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec). 7 | * 8 | * @example 9 | * 10 | * const exec = require('1-liners/exec'); 11 | * const haystack = 'hAyHAYhayneEdLEHayHAy'; 12 | * 13 | * exec(haystack, /needle/i); // => ['neEdLE'] 14 | * exec(haystack, /n(.+)e/i); // => ['neEdLE', 'eEdL'] 15 | * exec(haystack, /needle/); // => null 16 | * 17 | */ 18 | export default (str, regexObj) => regexObj.exec(str); 19 | -------------------------------------------------------------------------------- /module/isUndefined.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isUndefined 3 | * 4 | * @description 5 | * 6 | * Returns `true` if a value or reference is `undefined`. 7 | * 8 | * @example 9 | * 10 | * var isUndefined = require('1-liners/isUndefined'); 11 | * 12 | * isUndefined(undefined); // => true 13 | * 14 | * isUndefined(null); // => false 15 | * isUndefined(false); // => false 16 | * isUndefined(NaN); // => false 17 | * isUndefined('anything else'); // => false 18 | * 19 | */ 20 | export default (value) => (value === void 0); 21 | -------------------------------------------------------------------------------- /tests/isNumber.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isNumber from '../isNumber'; 3 | 4 | test('#isNumber', () => { 5 | ok(isNumber(0)); 6 | ok(isNumber(1)); 7 | ok(isNumber(NaN)); 8 | ok(isNumber(Infinity)); 9 | 10 | ok(!isNumber(undefined)); 11 | ok(!isNumber(null)); 12 | ok(!isNumber(false)); 13 | ok(!isNumber(true)); 14 | ok(!isNumber('')); 15 | ok(!isNumber('1')); 16 | ok(!isNumber('non-empty string')); 17 | ok(!isNumber(() => {})); 18 | ok(!isNumber(function named() {})); 19 | ok(!isNumber([])); 20 | ok(!isNumber({})); 21 | ok(!isNumber(/anything else/)); 22 | }); 23 | -------------------------------------------------------------------------------- /module/startsWithAt.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/startsWithAt 3 | * 4 | * @description 5 | * 6 | * Same as [`str.startsWith(searchString, position)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith). 7 | * 8 | * @example 9 | * 10 | * const startsWithAt = require('1-liners/startsWithAt'); 11 | * 12 | * startsWithAt(2, 'liners', '1-liners/startsWithAt'); // => true 13 | * startsWithAt(2, 'stoeffel', 'nope'); // => false 14 | * 15 | */ 16 | export default (position, searchString, str) => str.startsWith(searchString, position); 17 | -------------------------------------------------------------------------------- /tests/isFunction.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isFunction from '../isFunction'; 3 | 4 | test('#isFunction', () => { 5 | ok(isFunction(() => {})); 6 | ok(isFunction(function named() {})); 7 | 8 | ok(!isFunction(undefined)); 9 | ok(!isFunction(null)); 10 | ok(!isFunction(0)); 11 | ok(!isFunction(1)); 12 | ok(!isFunction(false)); 13 | ok(!isFunction(true)); 14 | ok(!isFunction('')); 15 | ok(!isFunction('non-empty string')); 16 | ok(!isFunction(NaN)); 17 | ok(!isFunction(Infinity)); 18 | ok(!isFunction([])); 19 | ok(!isFunction({})); 20 | ok(!isFunction(new Date())); 21 | }); 22 | -------------------------------------------------------------------------------- /tests/isBoolean.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isBoolean from '../isBoolean'; 3 | 4 | test('#isBoolean', () => { 5 | ok(isBoolean(false)); 6 | ok(isBoolean(true)); 7 | 8 | ok(!isBoolean(undefined)); 9 | ok(!isBoolean(null)); 10 | ok(!isBoolean('')); 11 | ok(!isBoolean('1')); 12 | ok(!isBoolean(0)); 13 | ok(!isBoolean(1)); 14 | ok(!isBoolean(NaN)); 15 | ok(!isBoolean(Infinity)); 16 | ok(!isBoolean('non-empty string')); 17 | ok(!isBoolean(() => {})); 18 | ok(!isBoolean(function named() {})); 19 | ok(!isBoolean([])); 20 | ok(!isBoolean({})); 21 | ok(!isBoolean(/anything else/)); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/isObject.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isObject from '../isObject'; 3 | 4 | test('#isObject', () => { 5 | ok(isObject({})); 6 | ok(isObject([])); 7 | ok(isObject(/anything else/)); 8 | ok(isObject(Object.create(null))); 9 | 10 | ok(!isObject(undefined)); 11 | ok(!isObject(null)); 12 | ok(!isObject(false)); 13 | ok(!isObject(true)); 14 | ok(!isObject('')); 15 | ok(!isObject('1')); 16 | ok(!isObject(0)); 17 | ok(!isObject(1)); 18 | ok(!isObject(NaN)); 19 | ok(!isObject(Infinity)); 20 | ok(!isObject('non-empty string')); 21 | ok(!isObject(() => {})); 22 | ok(!isObject(function named() {})); 23 | }); 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | :heart: We love pull requests from everyone! 4 | 5 | Make sure to... 6 | 7 | * add tests 8 | * keep 100% coverage 9 | * only write ***ONE LINE OF CODE*** 10 | * and still keep it readable 11 | * update the README 12 | 13 | ## Getting started 14 | 15 | 1. `git clone https://github.com/1-liners/1-liners` 16 | 2. `npm install` 17 | 3. add your new awesome feature 18 | 4. `npm test` 19 | 5. `npm run toc` 20 | 6. push your commits (except compiled files in the root folder) 21 | 22 | During development you can run `npm run develop` to start [nodangel](https://github.com/tomekwi/nodangel). 23 | 24 | 25 | -------------------------------------------------------------------------------- /module/curryRight.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/curryRight 3 | * 4 | * @description 5 | * 6 | * 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. 7 | * 8 | * @example 9 | * 10 | * import curryRight from '1-liners/curryRight'; 11 | * 12 | * const g = (a, b, c, d) => a + b * c - d; 13 | * g(1, 2, 3, 4); // => 3 14 | * 15 | * const gλ = curryRight(g); 16 | * gλ(4)(1, 2, 3); // => 3 17 | * gλ(3, 4)(1, 2); // => 3 18 | * gλ(2, 3, 4)(1); // => 3 19 | * 20 | */ 21 | export default (f) => (...a) => (...b) => f(...b, ...a); 22 | -------------------------------------------------------------------------------- /module/shallowClone.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/shallowClone 3 | * 4 | * @description 5 | * 6 | * Copy all properties of an object into a new plain object. 7 | * 8 | * @example 9 | * 10 | * import shallowClone from '1-liners/shallowClone'; 11 | * 12 | * const source = { 13 | * value: 'value', 14 | * reference: /reference/, 15 | * }; 16 | * const target = shallowClone(source); 17 | * 18 | * target === source // => false 19 | * target.value === source.value // => true 20 | * target.reference === source.reference // => true 21 | * 22 | */ 23 | export default (object) => Object.assign({}, object); 24 | -------------------------------------------------------------------------------- /module/concat.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/concat 3 | * 4 | * @description 5 | * 6 | * Returns a copy of `array` with `values` or `value` appended at the end. Same as [`array.concat(values)` or `array.concat(value)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat). 7 | * 8 | * @example 9 | * 10 | * const concat = require('1-liners/concat'); 11 | * 12 | * concat(['c', 'd'], ['a', 'b']); // => ['a', 'b', 'c', 'd'] 13 | * concat(['c'], ['a', 'b']); // => ['a', 'b', 'c'] 14 | * concat('c', ['a', 'b']); // => ['a', 'b', 'c'] 15 | * 16 | */ 17 | export default (values, array) => array.concat(values); 18 | -------------------------------------------------------------------------------- /module/ifThenElse.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/ifThenElse 3 | * 4 | * @description 5 | * 6 | * Creates a function which calls `then` if the `predicate` is true 7 | * and `otherwise` if the `predicate` is false. 8 | * 9 | * @example 10 | * 11 | * let ifThenElse = require('1-liners/ifThenElse'); 12 | * 13 | * let eq = (a, b) => a === b; 14 | * let add = (a, b) => a + b; 15 | * let sub = (a, b) => a - b; 16 | * 17 | * let addIfEq = ifThenElse(eq, add, sub); 18 | * 19 | * addIfEq(1, 1); // => 2 20 | * addIfEq(2, 1); // => 1 21 | * 22 | */ 23 | export default (predicate, then, otherwise) => (...args) => predicate(...args) ? then(...args) : otherwise(...args); 24 | -------------------------------------------------------------------------------- /module/signum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/signum 3 | * 4 | * @description 5 | * 6 | * Returns the [sign of a number](https://en.wikipedia.org/wiki/Signum_function). `1` if `n` is positive, `-1` if `n` is negative and `0` if `n` is `0`. Otherwise returns `NaN`. 7 | * 8 | * @example 9 | * 10 | * const signum = require('1-liners/signum'); 11 | * 12 | * signum(-5); // => -1 13 | * signum(-Infinity); // => -1 14 | * 15 | * signum(10); // => 1 16 | * signum(Infinity); // => 1 17 | * 18 | * signum(0); // => 0 19 | * signum(-0); // => 0 20 | * 21 | */ 22 | export default (n) => (n === 0 ? 0 : (n > 0 ? 1 : (n < 0 ? -1 : NaN))); 23 | -------------------------------------------------------------------------------- /module/ifThen.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/ifThen 3 | * 4 | * @description 5 | * 6 | * Creates a function which calls `then` if the `predicate` is true 7 | * and returns `undefined` if the `predicate` is false. 8 | * 9 | * @example 10 | * 11 | * let ifThen = require('1-liners/ifThen'); 12 | * 13 | * let eq = (a, b) => a === b; 14 | * let add = (a, b) => a + b; 15 | * let sub = (a, b) => a - b; 16 | * 17 | * let words = ifThen((str) => typeof str === 'string', (str) => str.split(' ')); 18 | * 19 | * words('Hello ES2015'); // => ['Hello', 'ES2015'] 20 | * 21 | */ 22 | export default (perdicate, then) => (...args) => perdicate(...args) ? then(...args) : undefined; 23 | -------------------------------------------------------------------------------- /tests/curry.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import curry from '../curry'; 3 | import reduce from '../reduce'; 4 | import compose from '../compose'; 5 | 6 | test('#curry', () => { 7 | const f = (a, b) => a + b; 8 | equal(curry(f)(1)(2), 3); 9 | 10 | const g = (a, b, c) => a + b + c; 11 | equal( 12 | curry(g)(1)(2, 3), 13 | 6 14 | ); 15 | equal( 16 | curry(g)(1, 2)(3), 17 | 6 18 | ); 19 | 20 | const curry3 = compose(curry, curry); 21 | equal( 22 | curry3(g)(1)(2)(3), 23 | 6 24 | ); 25 | 26 | const h = (a, b, c, d) => a + b + c + d; 27 | const curry4 = reduce(compose, [curry, curry, curry]); 28 | equal( 29 | curry4(h)(1)(2)(3)(4), 30 | 10 31 | ); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/isPlainObject.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isPlainObject from '../isPlainObject'; 3 | 4 | test('#isPlainObject', () => { 5 | ok(isPlainObject({})); 6 | ok(isPlainObject(Object.create(null))); 7 | 8 | ok(!isPlainObject(undefined)); 9 | ok(!isPlainObject(null)); 10 | ok(!isPlainObject(false)); 11 | ok(!isPlainObject(true)); 12 | ok(!isPlainObject('')); 13 | ok(!isPlainObject('1')); 14 | ok(!isPlainObject(0)); 15 | ok(!isPlainObject(1)); 16 | ok(!isPlainObject(NaN)); 17 | ok(!isPlainObject(Infinity)); 18 | ok(!isPlainObject('non-empty string')); 19 | ok(!isPlainObject([])); 20 | ok(!isPlainObject(() => {})); 21 | ok(!isPlainObject(function named() {})); 22 | ok(!isPlainObject(/anything else/)); 23 | }); 24 | -------------------------------------------------------------------------------- /module/isNumber.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isNumber 3 | * 4 | * @description 5 | * 6 | * Same as `typeof value === 'number'`. Use [`Number.isFinite`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite) instead if you want to filter out `NaN` and `Infinity`. 7 | * 8 | * @example 9 | * 10 | * var isNumber = require('1-liners/isNumber'); 11 | * 12 | * isNumber(1); // => true 13 | * isNumber(3.14); // => true 14 | * isNumber(NaN); // => true 15 | * isNumber(Infinity); // => true 16 | * 17 | * isNumber('3.14'); // => false 18 | * isNumber(/anything else/); // => false 19 | * 20 | */ 21 | export default (value) => typeof value === 'number'; 22 | -------------------------------------------------------------------------------- /tests/isTypeOf.js: -------------------------------------------------------------------------------- 1 | import {ok} from 'assert'; 2 | import isTypeOf from '../isTypeOf'; 3 | 4 | test('#isTypeOf', () => { 5 | ok(isTypeOf('boolean', false)); 6 | ok(isTypeOf('boolean', true)); 7 | ok(isTypeOf('string', '1-liners')); 8 | ok(isTypeOf('number', 1)); 9 | ok(isTypeOf('object', {})); 10 | 11 | ok(!isTypeOf('boolean', undefined)); 12 | ok(!isTypeOf('boolean', null)); 13 | ok(!isTypeOf('boolean', '')); 14 | ok(!isTypeOf('boolean', '1')); 15 | ok(!isTypeOf('boolean', 0)); 16 | ok(!isTypeOf('boolean', 1)); 17 | ok(!isTypeOf('boolean', NaN)); 18 | ok(!isTypeOf('boolean', Infinity)); 19 | ok(!isTypeOf('boolean', 'non-empty string')); 20 | ok(!isTypeOf('boolean', () => {})); 21 | ok(!isTypeOf('boolean', function named() {})); 22 | ok(!isTypeOf('boolean', [])); 23 | ok(!isTypeOf('boolean', {})); 24 | ok(!isTypeOf('boolean', /anything else/)); 25 | }); 26 | -------------------------------------------------------------------------------- /utils/updateFuncCount.js: -------------------------------------------------------------------------------- 1 | import getModules from 'get-modules'; 2 | import { readFile, writeFile } from 'fs'; 3 | import { join } from 'path'; 4 | import curry from '../module/curry'; 5 | import replace from '../module/replace'; 6 | 7 | const replaceCount = curry(replace)(/\d+ one-liner functions/); 8 | 9 | getModules(join(__dirname, '..'), (err, modules) => { 10 | if (err) throw err; 11 | readFile(join(__dirname, '..', 'README.md'), { encoding: 'utf8' }, (err, data) => 12 | err? onError(err): 13 | writeFile('./README.md', replaceCount(modules.length + ' one-liner functions', data), onFileWritten)); 14 | }) 15 | 16 | function onError(err) { 17 | if (err) { 18 | console.log(err); 19 | process.exit(1); 20 | } 21 | } 22 | 23 | function onFileWritten(err) { 24 | if (err) onError(err); 25 | console.log('updated function count'); 26 | process.exit(0); 27 | } 28 | -------------------------------------------------------------------------------- /module/isPlainObject.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/isPlainObject 3 | * 4 | * @description 5 | * 6 | * Checks if an object inherits directly from `null` or `Object.prototype` – like an object literal (`{...}`) does. 7 | * 8 | * Heads up! This function is [not supported on IE 10 and below](https://babeljs.io/docs/usage/caveats/). 9 | * 10 | * @example 11 | * 12 | * var isPlainObject = require('1-liners/isPlainObject'); 13 | * 14 | * isPlainObject({}); // => true 15 | * isPlainObject(Object.create(null)); // => true 16 | * 17 | * isPlainObject(null); // => false 18 | * isPlainObject([]); // => false 19 | * isPlainObject(/anything else/); // => false 20 | * 21 | */ 22 | export default (value) => (value && typeof value === 'object' && (value.__proto__ == null || value.__proto__ === Object.prototype)); 23 | -------------------------------------------------------------------------------- /tests/oneliner.js: -------------------------------------------------------------------------------- 1 | import { equal } from 'assert'; 2 | import { join } from 'path'; 3 | import getModules from 'get-modules'; 4 | import { readdir, readFile } from 'fs'; 5 | 6 | test('Should be a oneliner', (done) => 7 | getModules(join(__dirname, '..'), (err, modules) => { 8 | if (err) throw err; 9 | let countTests = 0; 10 | 11 | modules.forEach((file) => { 12 | if (file === 'index.js') { return } 13 | 14 | readFile(join(__dirname, '..', 'module', file), { encoding: 'utf8', flag: 'r' }, (err, data) => { 15 | if (err) throw err; 16 | let count = 0; 17 | let lines = data.split(/\n/).forEach((l) => { 18 | if (l.startsWith(' *') || l.startsWith('/')) return; 19 | if (l.length > 0) count++; 20 | }); 21 | 22 | equal(count, 1, `${file} should be a oneliner`); 23 | 24 | countTests++; 25 | if (countTests >= modules.length -1 ) done(); 26 | }) 27 | }); 28 | }) 29 | ); 30 | -------------------------------------------------------------------------------- /module/curry.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @module 1-liners/curry 3 | * 4 | * @description 5 | * 6 | * Curry a function – split its list of parameters into 2 lists. 7 | * 8 | * @example 9 | * 10 | * import curry from '1-liners/curry'; 11 | * import reduce from '1-liners/reduce'; 12 | * import compose from '1-liners/compose'; 13 | * 14 | * // You can use reduce and compose to create curry3,4 and so on. 15 | * const curry3 = compose(curry, curry); 16 | * const curry4 = reduce(compose, [curry, curry, curry]); 17 | * 18 | * const f = (a, b, c, d) => a * b * c * d; 19 | * const fβ = curry(f); // ~= curry2 20 | * const fγ = curry3(f); // ~= curry3 21 | * const fδ = curry4(f); // ~= curry4 22 | * 23 | * f(1, 2, 3, 4) === 24 24 | * 25 | * fβ(1)(2, 3, 4) === 24 26 | * fβ(1, 2)(3, 4) === 24 27 | * fβ(1, 2, 3)(4) === 24 28 | * 29 | * fγ(1)(2)(3, 4) === 24 30 | * fγ(1)(2, 3)(4) === 24 31 | * 32 | * fδ(1)(2)(3)(4) === 24 33 | * 34 | */ 35 | export default (f) => (...a) => (...b) => f(...a, ...b); 36 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) stoeffel (schtoeffel.ch) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "1-liners", 3 | "version": "0.3.6", 4 | "description": "Useful oneliners and shorthand functions", 5 | "license": "MIT", 6 | "repository": "1-liners/1-liners", 7 | "author": { 8 | "name": "stoeffel", 9 | "email": "schtoeffel@gmail.com", 10 | "url": "schtoeffel.ch" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "precommit": "npm run docs; npm run create-index; npm run updateCount; git add documentation module/index.js README.md", 17 | "develop": "nodangel --watch module --watch tests --exec 'npm --silent test'", 18 | "istanbul": "istanbul cover ./node_modules/mocha/bin/_mocha -- --report lcov --compilers js:babel/register --ui qunit ./tests", 19 | "docs": "babel-node ./utils/createDocs.js; npm run toc; npm run updateCount", 20 | "updateCount": "babel-node ./utils/updateFuncCount.js;", 21 | "toc": "doctoc ./documentation/README.md", 22 | "mocha": "mocha --compilers js:babel/register --ui qunit ./tests", 23 | "compile": "cd ./module; babel --loose=all --plugins object-assign,array-includes --auxiliary-comment-before 'istanbul ignore next' ./*.js --out-dir ../", 24 | "clean": "rm -f *.js", 25 | "coverage": "npm run compile; npm run istanbul; npm run clean", 26 | "coveralls": "npm run compile && npm run istanbul && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && npm run clean", 27 | "test": "npm run coverage", 28 | "create-index": "module-indexer .", 29 | "prepublish": "npm test; npm run create-index; npm run compile", 30 | "patch-release": "npm version patch && npm publish && git push --follow-tags", 31 | "minor-release": "npm version minor && npm publish && git push --follow-tags", 32 | "major-release": "npm version major && npm publish && git push --follow-tags" 33 | }, 34 | "files": [ 35 | "/*.js", 36 | "/module/", 37 | "/license", 38 | "/readme.md" 39 | ], 40 | "keywords": [ 41 | "oneliners", 42 | "shorthands" 43 | ], 44 | "dependencies": {}, 45 | "devDependencies": { 46 | "babel": "^5.6.3", 47 | "babel-core": "^5.5.6", 48 | "babel-plugin-array-includes": "^1.1.0", 49 | "babel-plugin-object-assign": "^1.2.0", 50 | "coveralls": "^2.11.2", 51 | "doctoc": "^0.14.1", 52 | "dox": "^0.8.0", 53 | "get-modules": "^1.0.2", 54 | "husky": "^0.8.1", 55 | "istanbul": "^0.3.14", 56 | "mocha": "^2.2.5", 57 | "mocha-lcov-reporter": "0.0.2", 58 | "module-indexer": "^1.0.0", 59 | "nodangel": "^1.3.8" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /utils/createDocs.js: -------------------------------------------------------------------------------- 1 | import { parseComments } from 'dox'; 2 | import getModules from 'get-modules'; 3 | import { readFileSync, writeFile } from 'fs'; 4 | import path from 'path'; 5 | import curry from '../module/curry'; 6 | import map from '../module/map'; 7 | import join from '../module/join'; 8 | import head from '../module/head'; 9 | import nth from '../module/nth'; 10 | import property from '../module/property'; 11 | import compose from '../module/compose'; 12 | import reduce from '../module/reduce'; 13 | import filter from '../module/filter'; 14 | 15 | const composeAll = (...funcs) => reduce(compose, funcs); 16 | const mapλ = curry(map); 17 | const filterλ = curry(filter); 18 | const joinλ = curry(join); 19 | 20 | const filterIndex = filterλ(module => module !== 'index.js'); 21 | 22 | getModules(path.join(__dirname, '..'), (err, modules) => { 23 | if (err) throw err; 24 | 25 | const docs = composeAll( 26 | joinλ('\n'), 27 | mapλ(renderTpl), 28 | mapλ(parseComments), 29 | mapλ(fileName => readFileSync(fileName, { encoding: 'utf8' })), 30 | mapλ(fileName => `./module/${fileName}`), 31 | filterIndex 32 | )(modules); 33 | 34 | const README = ` 

  35 | # Documentation 36 | 37 | 38 | 39 | 40 | 41 | ${docs} 42 | `; 43 | writeFile('./documentation/README.md', README, onFileWritten); 44 | }) 45 | 46 | 47 | function onFileWritten(err) { 48 | if (err) { 49 | console.log(err); 50 | process.exit(1); 51 | } 52 | console.log('created docs'); 53 | process.exit(0); 54 | } 55 | 56 | function renderTpl(comments) { 57 | const comment = head(comments); 58 | const tags = property('tags', comment); 59 | const code = property('code', comment) 60 | .replace(/export default/g, '') 61 | .replace(/&/g, '&') 62 | .replace(//g, '>') 64 | ; 65 | if (tags.length <= 0) { 66 | console.warn(`NO DOCS FOR ${code} !!!`); 67 | return `\n\n:zap: NO DOCS FOR ${code} !!!\n\n`; 68 | } 69 | const name = property('string', head(tags)).replace('1-liners/', '').trim(); 70 | const desc = property('full', nth(1, tags)); 71 | const example = property('string', nth(2, tags)).replace(/\n\t/g, '\n'); 72 | 73 | const block = ` 74 | ### ${name} 75 | 76 | ${desc} 77 | 78 | \`\`\`js${example} 79 | \`\`\` 80 | 81 |
82 | Spec 83 | • 84 | Source: ${code} 85 |
86 | `; 87 | 88 | return block; 89 | } 90 | -------------------------------------------------------------------------------- /module/index.js: -------------------------------------------------------------------------------- 1 | import always from './always'; 2 | import and from './and'; 3 | import average from './average'; 4 | import between from './between'; 5 | import bind from './bind'; 6 | import bitAnd from './bitAnd'; 7 | import bitOr from './bitOr'; 8 | import butLast from './butLast'; 9 | import by from './by'; 10 | import compose from './compose'; 11 | import composeAll from './composeAll'; 12 | import concat from './concat'; 13 | import converge from './converge'; 14 | import curry from './curry'; 15 | import curryRight from './curryRight'; 16 | import dec from './dec'; 17 | import drop from './drop'; 18 | import endsWith from './endsWith'; 19 | import endsWithAt from './endsWithAt'; 20 | import equal from './equal'; 21 | import every from './every'; 22 | import exec from './exec'; 23 | import explode from './explode'; 24 | import extend from './extend'; 25 | import filter from './filter'; 26 | import flatMap from './flatMap'; 27 | import flip from './flip'; 28 | import fold from './fold'; 29 | import foldRight from './foldRight'; 30 | import forEach from './forEach'; 31 | import greaterOrEqual from './greaterOrEqual'; 32 | import greaterThan from './greaterThan'; 33 | import hasOwnProperty from './hasOwnProperty'; 34 | import head from './head'; 35 | import identity from './identity'; 36 | import ifThen from './ifThen'; 37 | import ifThenElse from './ifThenElse'; 38 | import implode from './implode'; 39 | import inc from './inc'; 40 | import isBetween from './isBetween'; 41 | import isBoolean from './isBoolean'; 42 | import isFalse from './isFalse'; 43 | import isFalsy from './isFalsy'; 44 | import isFunction from './isFunction'; 45 | import isNull from './isNull'; 46 | import isNumber from './isNumber'; 47 | import isObject from './isObject'; 48 | import isPlainObject from './isPlainObject'; 49 | import isString from './isString'; 50 | import isTrue from './isTrue'; 51 | import isTruthy from './isTruthy'; 52 | import isTypeOf from './isTypeOf'; 53 | import isUndefined from './isUndefined'; 54 | import isUnknown from './isUnknown'; 55 | import join from './join'; 56 | import keys from './keys'; 57 | import last from './last'; 58 | import length from './length'; 59 | import lessOrEqual from './lessOrEqual'; 60 | import lessThan from './lessThan'; 61 | import looseEqual from './looseEqual'; 62 | import map from './map'; 63 | import match from './match'; 64 | import max from './max'; 65 | import method from './method'; 66 | import min from './min'; 67 | import minus from './minus'; 68 | import nand from './nand'; 69 | import noop from './noop'; 70 | import nor from './nor'; 71 | import not from './not'; 72 | import nth from './nth'; 73 | import or from './or'; 74 | import pick from './pick'; 75 | import pipe from './pipe'; 76 | import pipeAll from './pipeAll'; 77 | import plus from './plus'; 78 | import product from './product'; 79 | import property from './property'; 80 | import push from './push'; 81 | import put from './put'; 82 | import reduce from './reduce'; 83 | import reduceRight from './reduceRight'; 84 | import replace from './replace'; 85 | import shallowClone from './shallowClone'; 86 | import shave from './shave'; 87 | import signum from './signum'; 88 | import some from './some'; 89 | import split from './split'; 90 | import startsWith from './startsWith'; 91 | import startsWithAt from './startsWithAt'; 92 | import sum from './sum'; 93 | import tail from './tail'; 94 | import take from './take'; 95 | import takeUntil from './takeUntil'; 96 | import takeWhile from './takeWhile'; 97 | import test from './test'; 98 | import times from './times'; 99 | import uncurry from './uncurry'; 100 | import uncurry3 from './uncurry3'; 101 | import values from './values'; 102 | import xor from './xor'; 103 | 104 | export { 105 | always, 106 | and, 107 | average, 108 | between, 109 | bind, 110 | bitAnd, 111 | bitOr, 112 | butLast, 113 | by, 114 | compose, 115 | composeAll, 116 | concat, 117 | converge, 118 | curry, 119 | curryRight, 120 | dec, 121 | drop, 122 | endsWith, 123 | endsWithAt, 124 | equal, 125 | every, 126 | exec, 127 | explode, 128 | extend, 129 | filter, 130 | flatMap, 131 | flip, 132 | fold, 133 | foldRight, 134 | forEach, 135 | greaterOrEqual, 136 | greaterThan, 137 | hasOwnProperty, 138 | head, 139 | identity, 140 | ifThen, 141 | ifThenElse, 142 | implode, 143 | inc, 144 | isBetween, 145 | isBoolean, 146 | isFalse, 147 | isFalsy, 148 | isFunction, 149 | isNull, 150 | isNumber, 151 | isObject, 152 | isPlainObject, 153 | isString, 154 | isTrue, 155 | isTruthy, 156 | isTypeOf, 157 | isUndefined, 158 | isUnknown, 159 | join, 160 | keys, 161 | last, 162 | length, 163 | lessOrEqual, 164 | lessThan, 165 | looseEqual, 166 | map, 167 | match, 168 | max, 169 | method, 170 | min, 171 | minus, 172 | nand, 173 | noop, 174 | nor, 175 | not, 176 | nth, 177 | or, 178 | pick, 179 | pipe, 180 | pipeAll, 181 | plus, 182 | product, 183 | property, 184 | push, 185 | put, 186 | reduce, 187 | reduceRight, 188 | replace, 189 | shallowClone, 190 | shave, 191 | signum, 192 | some, 193 | split, 194 | startsWith, 195 | startsWithAt, 196 | sum, 197 | tail, 198 | take, 199 | takeUntil, 200 | takeWhile, 201 | test, 202 | times, 203 | uncurry, 204 | uncurry3, 205 | values, 206 | xor 207 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MADE IN SWITZERLAND 2 | 3 | [![Coverage Status](https://coveralls.io/repos/1-liners/1-liners/badge.svg?branch=master)](https://coveralls.io/r/1-liners/1-liners?branch=master) 4 | [![Build Status](https://travis-ci.org/1-liners/1-liners.svg?branch=master)](https://travis-ci.org/1-liners/1-liners) 5 | [![Dependency Status](https://david-dm.org/1-liners/1-liners.svg)](https://david-dm.org/1-liners/1-liners) 6 | [![npm version](https://badge.fury.io/js/1-liners.svg)](http://badge.fury.io/js/1-liners) 7 | [![Stability: unstable](https://img.shields.io/badge/stability-unstable-yellowgreen.svg)](https://github.com/1-liners/1-liners/milestones/1.0) 8 | [![Join the chat at https://gitter.im/1-liners/1-liners](https://img.shields.io/badge/gitter-join%20chat-1dce73.svg)](https://gitter.im/1-liners/1-liners) 9 | 10 |

14 |
15 |
16 |
17 | 1-liners 23 |
24 |
25 |
26 |
27 |

28 | 29 | **Functional tools that couldn’t be simpler.** 30 | 31 | We’re proud to present *1-liners* – a dead simple functional utility belt. **[103 one-liner functions][docs]** (and counting). Each hand-crafted with love and attention. 32 | 33 | [docs]: ./documentation 34 | 35 |

36 |
37 |
38 |
39 | 44 |
45 |
46 |
47 |
48 |
49 |
50 |

51 | 52 | 53 | ## Our decalogue 54 | 55 | You get a product of top-quality functional programming craftmanship. Each function follows the *KISS* principle, which we’ve broken down into ten strict rules. 56 | 57 | We always follow them. You have our word. 58 | 59 | 60 |

64 |
65 |
66 | 1-liners 72 |
73 |
74 |
75 |

76 | 77 | 78 | 1. Each function shall **[fit in one readable line of code][]** 79 | – take a glimpse at the source and you know exactly what’s going on. 80 |   81 | 82 | 2. Each function shall **[have no side-effects][]** 83 | – you can use it with confidence. 84 |   85 | 86 | 3. Each function shall **[have a fixed number of arguments][]** 87 | – it’s dead easy to bind, curry and uncurry, apply partially, implode and explode. 88 |   89 | 90 | 4. Each function shall **[deal with data in an immutable way][]** 91 | – no more debugging nightmares. 92 |   93 | 94 | 5. Each function shall **[take data as the last argument][]** 95 | – this makes currying and composing new functions a breeze. 96 |   97 | 98 | 6. Each function shall **[be in a separate micro-module][]** 99 | – you only load/bundle the single 1-liner you need. 100 |   101 | 102 | 7. Each function shall **[be thoroughly tested][]** 103 | – 100% code coverage guaranteed. We even test if every function fits in one line! 104 |   105 | 106 | 8. Each function shall **[have great documentation][]** 107 | – the docs explain usage, present the source, and link to the specs. 108 |   109 | 110 | 9. We shall provide functional versions of **[native object methods][]** 111 | – like `reduce(callback, array)` for `array.reduce(callback)`. 112 |   113 | 114 | 10. We shall provide functional versions of **[language constructs][]** 115 | – like `plus(a, b)` for `a + b`. 116 |   117 | 118 | [fit in one readable line of code]: https://github.com/1-liners/1-liners/blob/28b02d0939d6bb4034693e48440f450141453ae9/module/flip.js#L18 119 | [have no side-effects]: ./documentation#extend 120 | [have a fixed number of arguments]: ./documentation#shave 121 | [deal with data in an immutable way]: ./documentation#put 122 | [take data as the last argument]: ./documentation#split 123 | [be in a separate micro-module]: https://github.com/1-liners/1-liners/blob/28b02d0939d6bb4034693e48440f450141453ae9/module/compose.js 124 | [be thoroughly tested]: https://coveralls.io/r/1-liners/1-liners?branch=master 125 | [have great documentation]: ./documentation 126 | [native object methods]: ./documentation#reduce 127 | [language constructs]: ./documentation#plus 128 | 129 | 130 | ## Usage 131 | 132 | ### Install 133 | 134 | ``` 135 | $ npm install --save 1-liners 136 | ``` 137 | 138 | ### Usage in ES5 139 | 140 | ```js 141 | // The lightweight, recommended way: 142 | var map = require('1-liners/map'); 143 | 144 | // Sometimes practical: 145 | var map = require('1-liners').map; 146 | ``` 147 | 148 | ### Usage in ES 2015 (formerly ES6) 149 | 150 | ```js 151 | // The lightweight, recommended way: 152 | import map from '1-liners/module/map'; 153 | 154 | // Sometimes practical: 155 | import { map, filter } from '1-liners/module/index'; 156 | ``` 157 | 158 | ## API 159 | 160 | Checkout the [documentation](./documentation) 161 | 162 | ## Maintainers 163 | 164 | | [![stoeffel](https://avatars.githubusercontent.com/u/1217681?v=3&s=80)](https://github.com/stoeffel) | [![hemanth](https://avatars.githubusercontent.com/u/18315?v=3&s=80)](https://github.com/hemanth) | [![tomekwi](https://avatars.githubusercontent.com/u/4624660?v=3&s=80)](https://github.com/tomekwi) | 165 | | :--:|:--:|:--: | 166 | | [stoeffel](https://github.com/stoeffel) | [hemanth](https://github.com/hemanth) | [tomekwi](https://github.com/tomekwi) | 167 | 168 | created with [gh-contributors-table](https://github.com/stoeffel/gh-contributors-table) 169 | 170 | 171 | ## License 172 | 173 | MIT © [stoeffel](http://schtoeffel.ch) [tomekwi](http://github.com/tomekwi) [hemanth](http://www.h3manth.com/) 174 | -------------------------------------------------------------------------------- /documentation/README.md: -------------------------------------------------------------------------------- 1 |  

  2 | # Documentation 3 | 4 | 5 | 6 | 7 | 8 | - [always](#always) 9 | - [and](#and) 10 | - [average](#average) 11 | - [between](#between) 12 | - [bind](#bind) 13 | - [bitAnd](#bitand) 14 | - [bitOr](#bitor) 15 | - [butLast](#butlast) 16 | - [by](#by) 17 | - [compose](#compose) 18 | - [composeAll](#composeall) 19 | - [concat](#concat) 20 | - [converge](#converge) 21 | - [curry](#curry) 22 | - [curryRight](#curryright) 23 | - [dec](#dec) 24 | - [drop](#drop) 25 | - [endsWith](#endswith) 26 | - [endsWithAt](#endswithat) 27 | - [equal](#equal) 28 | - [every](#every) 29 | - [exec](#exec) 30 | - [explode](#explode) 31 | - [extend](#extend) 32 | - [filter](#filter) 33 | - [flatMap](#flatmap) 34 | - [flip](#flip) 35 | - [fold](#fold) 36 | - [foldRight](#foldright) 37 | - [forEach](#foreach) 38 | - [greaterOrEqual](#greaterorequal) 39 | - [greaterThan](#greaterthan) 40 | - [hasOwnProperty](#hasownproperty) 41 | - [head](#head) 42 | - [identity](#identity) 43 | - [ifThen](#ifthen) 44 | - [ifThenElse](#ifthenelse) 45 | - [implode](#implode) 46 | - [inc](#inc) 47 | - [isBetween](#isbetween) 48 | - [isBoolean](#isboolean) 49 | - [isFalse](#isfalse) 50 | - [isFalsy](#isfalsy) 51 | - [isFunction](#isfunction) 52 | - [isNull](#isnull) 53 | - [isNumber](#isnumber) 54 | - [isObject](#isobject) 55 | - [isPlainObject](#isplainobject) 56 | - [isString](#isstring) 57 | - [isTrue](#istrue) 58 | - [isTruthy](#istruthy) 59 | - [isTypeOf](#istypeof) 60 | - [isUndefined](#isundefined) 61 | - [isUnknown](#isunknown) 62 | - [join](#join) 63 | - [keys](#keys) 64 | - [last](#last) 65 | - [length](#length) 66 | - [lessOrEqual](#lessorequal) 67 | - [lessThan](#lessthan) 68 | - [looseEqual](#looseequal) 69 | - [map](#map) 70 | - [match](#match) 71 | - [max](#max) 72 | - [method](#method) 73 | - [min](#min) 74 | - [minus](#minus) 75 | - [nand](#nand) 76 | - [noop](#noop) 77 | - [nor](#nor) 78 | - [not](#not) 79 | - [nth](#nth) 80 | - [or](#or) 81 | - [pick](#pick) 82 | - [pipe](#pipe) 83 | - [pipeAll](#pipeall) 84 | - [plus](#plus) 85 | - [product](#product) 86 | - [property](#property) 87 | - [push](#push) 88 | - [put](#put) 89 | - [reduce](#reduce) 90 | - [reduceRight](#reduceright) 91 | - [replace](#replace) 92 | - [shallowClone](#shallowclone) 93 | - [shave](#shave) 94 | - [signum](#signum) 95 | - [some](#some) 96 | - [split](#split) 97 | - [startsWith](#startswith) 98 | - [startsWithAt](#startswithat) 99 | - [sum](#sum) 100 | - [tail](#tail) 101 | - [take](#take) 102 | - [takeUntil](#takeuntil) 103 | - [takeWhile](#takewhile) 104 | - [test](#test) 105 | - [times](#times) 106 | - [uncurry](#uncurry) 107 | - [uncurry3](#uncurry3) 108 | - [values](#values) 109 | - [xor](#xor) 110 | 111 | 112 | 113 | 114 | ### always 115 | 116 | Creates a function that always returns a given value 117 | 118 | ```js 119 | var always = require('1-liners/always'); 120 | 121 | var T = always(true); 122 | T(); // => true 123 | T(); // => true 124 | 125 | var fortyTwo = always(42); 126 | fortyTwo(); // => 42 127 | fortyTwo(); // => 42 128 | ``` 129 | 130 |
131 | Spec 132 | • 133 | Source: val => () => val; 134 |
135 | 136 | 137 | ### and 138 | 139 | Same as `a && b`. 140 | 141 | ```js 142 | var and = require('1-liners/and'); 143 | 144 | and(true, true); // => true 145 | and(false, true); // => false 146 | ``` 147 | 148 |
149 | Spec 150 | • 151 | Source: (x, y) => x && y; 152 |
153 | 154 | 155 | ### average 156 | 157 | Returns the average of all items of an `array`. 158 | 159 | ```js 160 | const average = require('1-liners/average'); 161 | 162 | average([2, 3, 4]); // => 3 163 | average([]); // => NaN 164 | ``` 165 | 166 |
167 | Spec 168 | • 169 | Source: (array) => array.reduce((a, b) => (a + b), 0) / array.length; 170 |
171 | 172 | 173 | ### between 174 | 175 | Return `number` if it’s greater than `min` and lower than `max`. Else return `min` or `max` respectively. 176 | 177 | ```js 178 | var between = require('1-liners/between'); 179 | 180 | between(1, 10, 2.5); // => 2.5 181 | between(1, 10, -5); // => 1 182 | between(1, 10, 25); // => 10 183 | ``` 184 | 185 |
186 | Spec 187 | • 188 | Source: (min, max, number) => (number < min ? min : (number > max ? max : number)); 189 |
190 | 191 | 192 | ### bind 193 | 194 | Binds a context to a function. Same as [`fun.bind(thisArg[, arg1[, arg2[, ...]]])`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) 195 | 196 | ```js 197 | var bind = require('1-liners/bind'); 198 | 199 | setTimeout(bind(console, ['Hello'], console.log), 2000); // => 'Hello' (after 2s) 200 | ``` 201 | 202 |
203 | Spec 204 | • 205 | Source: (thisArg, args, fun) => fun.bind(thisArg, ...args); 206 |
207 | 208 | 209 | ### bitAnd 210 | 211 | Same as `a & b`. 212 | 213 | ```js 214 | var bitAnd = require('1-liners/bitAnd'); 215 | 216 | bitAnd(1, 2); // => 0 217 | bitAnd(2, 2); // => 2 218 | ``` 219 | 220 |
221 | Spec 222 | • 223 | Source: (x, y) => x & y; 224 |
225 | 226 | 227 | ### bitOr 228 | 229 | Same as `a | b`. 230 | 231 | ```js 232 | var bitOr = require('1-liners/bitOr'); 233 | 234 | bitOr(0, 1); // => 1 235 | bitOr(1, 1); // => 1 236 | ``` 237 | 238 |
239 | Spec 240 | • 241 | Source: (x, y) => x | y; 242 |
243 | 244 | 245 | ### butLast 246 | 247 | Return a copy of `array`, without the last item. 248 | 249 | ```js 250 | import butLast from '1-liners/butLast'; 251 | 252 | const array = [1, 2, 3]; 253 | 254 | butLast(array); // => [1, 2] 255 | array; // => [1, 2, 3] 256 | ``` 257 | 258 |
259 | Spec 260 | • 261 | Source: (array) => array.slice(0, -1); 262 |
263 | 264 | 265 | ### by 266 | 267 | Same as `a / b` 268 | 269 | ```js 270 | var by = require('1-liners/by'); 271 | 272 | by(6, 2); // => 3 273 | ``` 274 | 275 |
276 | Spec 277 | • 278 | Source: (a, b) => a / b; 279 |
280 | 281 | 282 | ### compose 283 | 284 | Compose a new function from two given functions. 285 | 286 | ```js 287 | var compose = require('1-liners/compose'); 288 | 289 | compose(f, g)(1, 2) === f(g(1, 2)); 290 | ``` 291 | 292 |
293 | Spec 294 | • 295 | Source: (f, g) => (...args) => f(g(...args)); 296 |
297 | 298 | 299 | ### composeAll 300 | 301 | Compose a new function with a given array of functions. 302 | 303 | ```js 304 | var composeAll = require('1-liners/composeAll'); 305 | 306 | composeAll([f, g, h])(1, 2) === f(g(h(1, 2))); 307 | ``` 308 | 309 |
310 | Spec 311 | • 312 | Source: (fns) => fns.reduce( (f, g) => (...args) => f(g(...args)) ); 313 |
314 | 315 | 316 | ### concat 317 | 318 | Returns a copy of `array` with `values` or `value` appended at the end. Same as [`array.concat(values)` or `array.concat(value)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat). 319 | 320 | ```js 321 | const concat = require('1-liners/concat'); 322 | 323 | concat(['c', 'd'], ['a', 'b']); // => ['a', 'b', 'c', 'd'] 324 | concat(['c'], ['a', 'b']); // => ['a', 'b', 'c'] 325 | concat('c', ['a', 'b']); // => ['a', 'b', 'c'] 326 | ``` 327 | 328 |
329 | Spec 330 | • 331 | Source: (values, array) => array.concat(values); 332 |
333 | 334 | 335 | ### converge 336 | 337 | Converge two functions into one. 338 | 339 | ```js 340 | var converge = require('1-liners/converge'); 341 | 342 | converge(f, g, h)(1, 2) === f(g(1, 2), h(1, 2)); 343 | ``` 344 | 345 |
346 | Spec 347 | • 348 | Source: (f, g, h) => (...args) => f(g(...args), h(...args)); 349 |
350 | 351 | 352 | ### curry 353 | 354 | Curry a function – split its list of parameters into 2 lists. 355 | 356 | ```js 357 | import curry from '1-liners/curry'; 358 | import reduce from '1-liners/reduce'; 359 | import compose from '1-liners/compose'; 360 | 361 | // You can use reduce and compose to create curry3,4 and so on. 362 | const curry3 = compose(curry, curry); 363 | const curry4 = reduce(compose, [curry, curry, curry]); 364 | 365 | const f = (a, b, c, d) => a * b * c * d; 366 | const fβ = curry(f); // ~= curry2 367 | const fγ = curry3(f); // ~= curry3 368 | const fδ = curry4(f); // ~= curry4 369 | 370 | f(1, 2, 3, 4) === 24 371 | 372 | fβ(1)(2, 3, 4) === 24 373 | fβ(1, 2)(3, 4) === 24 374 | fβ(1, 2, 3)(4) === 24 375 | 376 | fγ(1)(2)(3, 4) === 24 377 | fγ(1)(2, 3)(4) === 24 378 | 379 | fδ(1)(2)(3)(4) === 24 380 | ``` 381 | 382 |
383 | Spec 384 | • 385 | Source: (f) => (...a) => (...b) => f(...a, ...b); 386 |
387 | 388 | 389 | ### curryRight 390 | 391 | 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. 392 | 393 | ```js 394 | import curryRight from '1-liners/curryRight'; 395 | 396 | const g = (a, b, c, d) => a + b * c - d; 397 | g(1, 2, 3, 4); // => 3 398 | 399 | const gλ = curryRight(g); 400 | gλ(4)(1, 2, 3); // => 3 401 | gλ(3, 4)(1, 2); // => 3 402 | gλ(2, 3, 4)(1); // => 3 403 | ``` 404 | 405 |
406 | Spec 407 | • 408 | Source: (f) => (...a) => (...b) => f(...b, ...a); 409 |
410 | 411 | 412 | ### dec 413 | 414 | Same as `a - 1` 415 | 416 | ```js 417 | var dec = require('1-liners/dec'); 418 | 419 | dec(1); // => 0 420 | ``` 421 | 422 |
423 | Spec 424 | • 425 | Source: (val) => val - 1; 426 |
427 | 428 | 429 | ### drop 430 | 431 | Creates a copy of the `object` without the given `props`. 432 | 433 | ```js 434 | const drop = require('1-liners/drop'); 435 | 436 | const object = {foo: 1, bar: 2, baz: 3}; 437 | 438 | drop(['foo', 'baz'], object); // => {bar: 2} 439 | object; // => {foo: 1, bar: 2, baz: 3} 440 | ``` 441 | 442 |
443 | Spec 444 | • 445 | Source: (props:Array, object) => Object.keys(object).reduce((res, k) => Object.assign(res, props.includes(k) ? null : {[k]: object[k]}), {}); 446 |
447 | 448 | 449 | ### endsWith 450 | 451 | Same as [`str.endsWith(searchString)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). 452 | 453 | ```js 454 | const endsWith = require('1-liners/endsWith'); 455 | 456 | endsWith('liners', '1-liners'); // => true 457 | endsWith('stoeffel', 'nope'); // => false 458 | ``` 459 | 460 |
461 | Spec 462 | • 463 | Source: (searchString, str) => str.endsWith(searchString); 464 |
465 | 466 | 467 | ### endsWithAt 468 | 469 | Same as [`str.endsWith(searchString, position)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). 470 | 471 | ```js 472 | const endsWithAt = require('1-liners/endsWithAt'); 473 | 474 | endsWithAt(8, 'liners', '1-liners/endsWithAt'); // => true 475 | endsWithAt(2, 'stoeffel', 'nope'); // => false 476 | ``` 477 | 478 |
479 | Spec 480 | • 481 | Source: (position, searchString, str) => str.endsWith(searchString, position); 482 |
483 | 484 | 485 | ### equal 486 | 487 | Same as `a === b`. 488 | 489 | ```js 490 | var equal = require('1-liners/equal'); 491 | 492 | equal(true, true); // => true 493 | equal(false, true); // => false 494 | equal(1, true); // => false 495 | ``` 496 | 497 |
498 | Spec 499 | • 500 | Source: (x, y) => x === y; 501 |
502 | 503 | 504 | ### every 505 | 506 | Same as `[1,2,3].every(GreaterThan16)`. 507 | 508 | ```js 509 | var every = require('1-liners/every'); 510 | 511 | every(elem => elem > 16, [16,17,18]); // => false 512 | ``` 513 | 514 |
515 | Spec 516 | • 517 | Source: (every, arr) => arr.every(every); 518 |
519 | 520 | 521 | ### exec 522 | 523 | Same as [`regexObj.exec(str)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec). 524 | 525 | ```js 526 | const exec = require('1-liners/exec'); 527 | const haystack = 'hAyHAYhayneEdLEHayHAy'; 528 | 529 | exec(haystack, /needle/i); // => ['neEdLE'] 530 | exec(haystack, /n(.+)e/i); // => ['neEdLE', 'eEdL'] 531 | exec(haystack, /needle/); // => null 532 | ``` 533 | 534 |
535 | Spec 536 | • 537 | Source: (str, regexObj) => regexObj.exec(str); 538 |
539 | 540 | 541 | ### explode 542 | 543 | The opposite of [implode](#implode). 544 | 545 | ```js 546 | var explode = require('1-liners/explode'); 547 | 548 | const sum = (numbers) => numbers.reduce((a, b) => a + b); 549 | 550 | explode(sum)(1, 2, 3, 4); // => 10 551 | ``` 552 | 553 |
554 | Spec 555 | • 556 | Source: (func) => (...args) => func(args); 557 |
558 | 559 | 560 | ### extend 561 | 562 | Returns a copy of `object`, extended by `extension`. 563 | 564 | ```js 565 | const extend = require('1-liners/extend'); 566 | 567 | const object = {id: 1}; 568 | 569 | extend({ name: 'stoeffel' }, object); // => { id: 1, name: 'stoeffel' } 570 | ``` 571 | 572 |
573 | Spec 574 | • 575 | Source: (extension, object) => Object.assign({}, object, extension); 576 |
577 | 578 | 579 | ### filter 580 | 581 | Same as `[1, 2, 3].filter(isOdd)`. 582 | 583 | ```js 584 | var filter = require('1-liners/filter'); 585 | 586 | filter(isOdd, [1, 2, 3]); // => [1, 3] 587 | ``` 588 | 589 |
590 | Spec 591 | • 592 | Source: (filter, arr) => arr.filter(filter); 593 |
594 | 595 | 596 | ### flatMap 597 | 598 | Map a function over a collection and flatten the result by one-level. 599 | 600 | ```js 601 | var flatMap = require('1-liners/flatMap'); 602 | 603 | flatMap((x) => [x, x], [1, 2, 3]); // => [1, 1, 2, 2, 3, 3] 604 | ``` 605 | 606 |
607 | Spec 608 | • 609 | Source: (fn, array) => [].concat.apply([], array.map(fn)); 610 |
611 | 612 | 613 | ### flip 614 | 615 | Flip a function’s arguments. 616 | 617 | ```js 618 | var flip = require('1-liners/flip'); 619 | 620 | var f = (a, b) => a / b; 621 | 622 | flip(f)(2, 6); // => 3 623 | flip(flip(f))(6, 2); // => 3 624 | ``` 625 | 626 |
627 | Spec 628 | • 629 | Source: (f) => (...args) => f(...args.reverse()); 630 |
631 | 632 | 633 | ### fold 634 | 635 | Same as [`array.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/array/reduce). 636 | 637 | ```js 638 | var fold = require('1-liners/fold'); 639 | 640 | fold(sum, 8, [1, 2, 3]); // => 2 641 | ``` 642 | 643 |
644 | Spec 645 | • 646 | Source: (fold, initial, arr) => arr.reduce(fold, initial); 647 |
648 | 649 | 650 | ### foldRight 651 | 652 | Same as [`array.reduceRight`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/array/reduceRight). 653 | 654 | ```js 655 | var foldRight = require('1-liners/foldRight'); 656 | 657 | foldRight(sub, 1, [1, 2, 3]); // => -5 658 | ``` 659 | 660 |
661 | Spec 662 | • 663 | Source: (fold, initial, arr) => arr.reduceRight(fold, initial); 664 |
665 | 666 | 667 | ### forEach 668 | 669 | Same as `[1, 2, 3].forEach(Math.sqrt)`. 670 | 671 | ```js 672 | var forEach = require('1-liners/forEach'); 673 | 674 | forEach(i => console.log('Item: ' + i), [9, 25]); // => logs "Item: 9" and "Item: 25" 675 | ``` 676 | 677 |
678 | Spec 679 | • 680 | Source: (forEach, arr) => arr.forEach(forEach); 681 |
682 | 683 | 684 | ### greaterOrEqual 685 | 686 | Same as `a >= b`. 687 | 688 | ```js 689 | var greaterOrEqual = require('1-liners/greaterOrEqual'); 690 | 691 | greaterOrEqual(2, 1); // => true 692 | greaterOrEqual(2, 2); // => true 693 | greaterOrEqual(1, 2); // => false 694 | ``` 695 | 696 |
697 | Spec 698 | • 699 | Source: (x, y) => x >= y; 700 |
701 | 702 | 703 | ### greaterThan 704 | 705 | Same as `a > b`. 706 | 707 | ```js 708 | var greaterThan = require('1-liners/greaterThan'); 709 | 710 | greaterThan(2, 1); // => true 711 | greaterThan(2, 2); // => false 712 | greaterThan(1, 2); // => false 713 | ``` 714 | 715 |
716 | Spec 717 | • 718 | Source: (x, y) => x > y; 719 |
720 | 721 | 722 | ### hasOwnProperty 723 | 724 | Same as [`obj.hasOwnProperty(prop)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty). 725 | 726 | ```js 727 | const hasOwnProperty = require('1-liners/hasOwnProperty'); 728 | 729 | hasOwnProperty('a', {a: 1, b: 2}); // => true 730 | hasOwnProperty('c', {a: 1, b: 2}); // => false 731 | ``` 732 | 733 |
734 | Spec 735 | • 736 | Source: (prop, obj) => obj.hasOwnProperty(prop); 737 |
738 | 739 | 740 | ### head 741 | 742 | Returns the first item of an array. 743 | 744 | ```js 745 | var head = require('1-liners/head'); 746 | 747 | head([1, 2, 3]); // => 1 748 | ``` 749 | 750 |
751 | Spec 752 | • 753 | Source: ([head,]) => head; 754 |
755 | 756 | 757 | ### identity 758 | 759 | Returns the value you pass to the function 760 | 761 | ```js 762 | var identity = require('1-liners/identity'); 763 | 764 | identity(true); // => true 765 | identity(1); // => 1 766 | identity({ foo: 1 }); // => { foo: 1 } 767 | identity("1-liners"); // => "1-liners" 768 | ``` 769 | 770 |
771 | Spec 772 | • 773 | Source: I => I; 774 |
775 | 776 | 777 | ### ifThen 778 | 779 | Creates a function which calls `then` if the `predicate` is true 780 | and returns `undefined` if the `predicate` is false. 781 | 782 | ```js 783 | let ifThen = require('1-liners/ifThen'); 784 | 785 | let eq = (a, b) => a === b; 786 | let add = (a, b) => a + b; 787 | let sub = (a, b) => a - b; 788 | 789 | let words = ifThen((str) => typeof str === 'string', (str) => str.split(' ')); 790 | 791 | words('Hello ES2015'); // => ['Hello', 'ES2015'] 792 | ``` 793 | 794 |
795 | Spec 796 | • 797 | Source: (perdicate, then) => (...args) => perdicate(...args) ? then(...args) : undefined; 798 |
799 | 800 | 801 | ### ifThenElse 802 | 803 | Creates a function which calls `then` if the `predicate` is true 804 | and `otherwise` if the `predicate` is false. 805 | 806 | ```js 807 | let ifThenElse = require('1-liners/ifThenElse'); 808 | 809 | let eq = (a, b) => a === b; 810 | let add = (a, b) => a + b; 811 | let sub = (a, b) => a - b; 812 | 813 | let addIfEq = ifThenElse(eq, add, sub); 814 | 815 | addIfEq(1, 1); // => 2 816 | addIfEq(2, 1); // => 1 817 | ``` 818 | 819 |
820 | Spec 821 | • 822 | Source: (predicate, then, otherwise) => (...args) => predicate(...args) ? then(...args) : otherwise(...args); 823 |
824 | 825 | 826 | ### implode 827 | 828 | Collapse a list of arguments into an array of arguments. 829 | 830 | ```js 831 | var implode = require('1-liners/implode'); 832 | 833 | const f = (a, b) => a + b; 834 | 835 | [ 836 | [1, 2], 837 | [3, 4], 838 | [5, 6], 839 | ].map(implode(f)); // => [3, 7, 11] 840 | ``` 841 | 842 |
843 | Spec 844 | • 845 | Source: (func) => (args) => func(...args); 846 |
847 | 848 | 849 | ### inc 850 | 851 | Same as `a + 1` 852 | 853 | ```js 854 | var inc = require('1-liners/inc'); 855 | 856 | inc(1); // => 2 857 | ``` 858 | 859 |
860 | Spec 861 | • 862 | Source: (val) => val + 1; 863 |
864 | 865 | 866 | ### isBetween 867 | 868 | Check if the `number` lies between `min` and `max`, inclusive. 869 | 870 | ```js 871 | var isBetween = require('1-liners/isBetween'); 872 | 873 | isBetween(1, 10, 2.5); // => true 874 | isBetween(1, 10, -5); // => false 875 | isBetween(1, 10, 25); // => false 876 | ``` 877 | 878 |
879 | Spec 880 | • 881 | Source: (min, max, number) => (min <= number && number <= max); 882 |
883 | 884 | 885 | ### isBoolean 886 | 887 | Same as `typeof value === 'boolean'`. 888 | 889 | ```js 890 | var isBoolean = require('1-liners/isBoolean'); 891 | 892 | isBoolean(false); // => true 893 | isBoolean(true); // => true 894 | 895 | isBoolean(null); // => false 896 | isBoolean(/anything else/); // => false 897 | ``` 898 | 899 |
900 | Spec 901 | • 902 | Source: (value) => typeof value === 'boolean'; 903 |
904 | 905 | 906 | ### isFalse 907 | 908 | Same as `x === false`. 909 | 910 | ```js 911 | var isFalse = require('1-liners/isFalse'); 912 | 913 | isFalse(false); // => true 914 | 915 | isFalse('yes'); // => false 916 | isFalse(true); // => false 917 | isFalse([]); // => false 918 | isFalse(''); // => false 919 | isFalse(0); // => false 920 | ``` 921 | 922 |
923 | Spec 924 | • 925 | Source: (x) => x === false; 926 |
927 | 928 | 929 | ### isFalsy 930 | 931 | Same as `!`. 932 | 933 | ```js 934 | var isFalsy = require('1-liners/isFalsy'); 935 | 936 | isFalsy('yes'); // => false 937 | isFalsy(true); // => false 938 | isFalsy([]); // => false 939 | 940 | isFalsy(''); // => true 941 | isFalsy(0); // => true 942 | isFalsy(false); // => true 943 | ``` 944 | 945 |
946 | Spec 947 | • 948 | Source: (x) => !x; 949 |
950 | 951 | 952 | ### isFunction 953 | 954 | Same as `typeof value === 'function'`. 955 | 956 | ```js 957 | var isFunction = require('1-liners/isFunction'); 958 | 959 | isFunction(function() {}); // => true 960 | isFunction(function named() {}); // => true 961 | 962 | isFunction('any other value'); // => false 963 | ``` 964 | 965 |
966 | Spec 967 | • 968 | Source: (value) => typeof value === 'function'; 969 |
970 | 971 | 972 | ### isNull 973 | 974 | Same as `=== null`. 975 | 976 | ```js 977 | var isNull = require('1-liners/isNull'); 978 | 979 | isNull(null); // => true 980 | 981 | isNull(undefined); // => false 982 | isNull(NaN); // => false 983 | isNull('anything else'); // => false 984 | ``` 985 | 986 |
987 | Spec 988 | • 989 | Source: (value) => (value === null); 990 |
991 | 992 | 993 | ### isNumber 994 | 995 | Same as `typeof value === 'number'`. Use [`Number.isFinite`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite) instead if you want to filter out `NaN` and `Infinity`. 996 | 997 | ```js 998 | var isNumber = require('1-liners/isNumber'); 999 | 1000 | isNumber(1); // => true 1001 | isNumber(3.14); // => true 1002 | isNumber(NaN); // => true 1003 | isNumber(Infinity); // => true 1004 | 1005 | isNumber('3.14'); // => false 1006 | isNumber(/anything else/); // => false 1007 | ``` 1008 | 1009 |
1010 | Spec 1011 | • 1012 | Source: (value) => typeof value === 'number'; 1013 |
1014 | 1015 | 1016 | ### isObject 1017 | 1018 | Same as `value !== null && typeof value === 'object'`. 1019 | 1020 | ```js 1021 | var isObject = require('1-liners/isObject'); 1022 | 1023 | isObject({}); // => true 1024 | isObject([]); // => true 1025 | isObject(/anything/); // => true 1026 | 1027 | isObject(null); // => false 1028 | isObject('anything else'); // => false 1029 | ``` 1030 | 1031 |
1032 | Spec 1033 | • 1034 | Source: (value) => (value !== null && typeof value === 'object'); 1035 |
1036 | 1037 | 1038 | ### isPlainObject 1039 | 1040 | Checks if an object inherits directly from `null` or `Object.prototype` – like an object literal (`{...}`) does. 1041 | 1042 | Heads up! This function is [not supported on IE 10 and below](https://babeljs.io/docs/usage/caveats/). 1043 | 1044 | ```js 1045 | var isPlainObject = require('1-liners/isPlainObject'); 1046 | 1047 | isPlainObject({}); // => true 1048 | isPlainObject(Object.create(null)); // => true 1049 | 1050 | isPlainObject(null); // => false 1051 | isPlainObject([]); // => false 1052 | isPlainObject(/anything else/); // => false 1053 | ``` 1054 | 1055 |
1056 | Spec 1057 | • 1058 | Source: (value) => (value && typeof value === 'object' && (value.__proto__ == null || value.__proto__ === Object.prototype)); 1059 |
1060 | 1061 | 1062 | ### isString 1063 | 1064 | Same as `typeof value === 'string'`. 1065 | 1066 | ```js 1067 | var isString = require('1-liners/isString'); 1068 | 1069 | isString(''); // => true 1070 | isString('anything'); // => true 1071 | 1072 | isString(/anything else/); // => false 1073 | ``` 1074 | 1075 |
1076 | Spec 1077 | • 1078 | Source: (value) => typeof value === 'string'; 1079 |
1080 | 1081 | 1082 | ### isTrue 1083 | 1084 | Same as `x === true`. 1085 | 1086 | ```js 1087 | var isTrue = require('1-liners/isTrue'); 1088 | 1089 | isTrue(true); // => true 1090 | 1091 | isTrue('yes'); // => false 1092 | isTrue([]); // => false 1093 | isTrue(''); // => false 1094 | isTrue(0); // => false 1095 | isTrue(false); // => false 1096 | ``` 1097 | 1098 |
1099 | Spec 1100 | • 1101 | Source: (x) => x === true; 1102 |
1103 | 1104 | 1105 | ### isTruthy 1106 | 1107 | Same as `!!`. 1108 | 1109 | ```js 1110 | var isTruthy = require('1-liners/isTruthy'); 1111 | 1112 | isTruthy('yes'); // => true 1113 | isTruthy(true); // => true 1114 | isTruthy([]); // => true 1115 | 1116 | isTruthy(''); // => false 1117 | isTruthy(0); // => false 1118 | isTruthy(false); // => false 1119 | ``` 1120 | 1121 |
1122 | Spec 1123 | • 1124 | Source: (x) => !!x; 1125 |
1126 | 1127 | 1128 | ### isTypeOf 1129 | 1130 | Same as `typeof value === TYPE`. 1131 | 1132 | ```js 1133 | var isTypeOf = require('1-liners/isTypeOf'); 1134 | 1135 | isTypeOf('boolean', false); // => true 1136 | isTypeOf('boolean', true); // => true 1137 | 1138 | isTypeOf('boolean', null); // => false 1139 | isTypeOf('boolean', /anything else/); // => false 1140 | ``` 1141 | 1142 |
1143 | Spec 1144 | • 1145 | Source: (type, value) => typeof value === type; 1146 |
1147 | 1148 | 1149 | ### isUndefined 1150 | 1151 | Returns `true` if a value or reference is `undefined`. 1152 | 1153 | ```js 1154 | var isUndefined = require('1-liners/isUndefined'); 1155 | 1156 | isUndefined(undefined); // => true 1157 | 1158 | isUndefined(null); // => false 1159 | isUndefined(false); // => false 1160 | isUndefined(NaN); // => false 1161 | isUndefined('anything else'); // => false 1162 | ``` 1163 | 1164 |
1165 | Spec 1166 | • 1167 | Source: (value) => (value === void 0); 1168 |
1169 | 1170 | 1171 | ### isUnknown 1172 | 1173 | Same as `== null`. 1174 | 1175 | ```js 1176 | var isUnknown = require('1-liners/isUnknown'); 1177 | 1178 | isUnknown(null); // => true 1179 | isUnknown(undefined); // => true 1180 | 1181 | isUnknown(false); // => false 1182 | isUnknown(''); // => false 1183 | isUnknown(NaN); // => false 1184 | isUnknown(/anything else/); // => false 1185 | ``` 1186 | 1187 |
1188 | Spec 1189 | • 1190 | Source: (value) => (value == null); 1191 |
1192 | 1193 | 1194 | ### join 1195 | 1196 | Same as `[1, 'liners'].join('-')` 1197 | 1198 | ```js 1199 | var join = require('1-liners/join'); 1200 | 1201 | join('-', [1, 'liners']); // => '1-liners' 1202 | ``` 1203 | 1204 |
1205 | Spec 1206 | • 1207 | Source: (superglue, arr) => arr.join(superglue); 1208 |
1209 | 1210 | 1211 | ### keys 1212 | 1213 | Same as [`Object.keys(obj)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys). 1214 | 1215 | ```js 1216 | const keys = require('1-liners/keys'); 1217 | 1218 | keys({ 100: 'a', 2: 'b', 7: 'c' }); // => ['2', '7', '100'] 1219 | keys([1, 2, 3]); // => [0, 1, 2] 1220 | ``` 1221 | 1222 |
1223 | Spec 1224 | • 1225 | Source: (obj) => Object.keys(obj); 1226 |
1227 | 1228 | 1229 | ### last 1230 | 1231 | Returns the last item of `array`. 1232 | 1233 | ```js 1234 | var last = require('1-liners/last'); 1235 | 1236 | last([1, 2, 3]); // => 3 1237 | ``` 1238 | 1239 |
1240 | Spec 1241 | • 1242 | Source: (array) => array[array.length - 1]; 1243 |
1244 | 1245 | 1246 | ### length 1247 | 1248 | Returns the length of an array. 1249 | 1250 | ```js 1251 | var length = require('1-liners/length'); 1252 | 1253 | length([0, 1, 2]); // => 3 1254 | ``` 1255 | 1256 |
1257 | Spec 1258 | • 1259 | Source: (arr) => arr.length; 1260 |
1261 | 1262 | 1263 | ### lessOrEqual 1264 | 1265 | Same as `a <= b`. 1266 | 1267 | ```js 1268 | var lessOrEqual = require('1-liners/lessOrEqual'); 1269 | 1270 | lessOrEqual(1, 2); // => true 1271 | lessOrEqual(1, 1); // => true 1272 | lessOrEqual(2, 1); // => false 1273 | ``` 1274 | 1275 |
1276 | Spec 1277 | • 1278 | Source: (x, y) => x <= y; 1279 |
1280 | 1281 | 1282 | ### lessThan 1283 | 1284 | Same as `a < b`. 1285 | 1286 | ```js 1287 | var lessThan = require('1-liners/lessThan'); 1288 | 1289 | lessThan(1, 2); // => true 1290 | lessThan(1, 1); // => false 1291 | lessThan(2, 1); // => false 1292 | ``` 1293 | 1294 |
1295 | Spec 1296 | • 1297 | Source: (x, y) => x < y; 1298 |
1299 | 1300 | 1301 | ### looseEqual 1302 | 1303 | Same as `a == b`. 1304 | 1305 | ```js 1306 | var looseEqual = require('1-liners/looseEqual'); 1307 | 1308 | looseEqual(true, true); // => true 1309 | looseEqual(false, true); // => false 1310 | looseEqual(1, true); // => true 1311 | ``` 1312 | 1313 |
1314 | Spec 1315 | • 1316 | Source: (x, y) => x == y; 1317 |
1318 | 1319 | 1320 | ### map 1321 | 1322 | Same as `[1, 2, 3].map(Math.sqrt)`. 1323 | 1324 | ```js 1325 | var map = require('1-liners/map'); 1326 | 1327 | map(Math.sqrt, [9, 25]); // => [3, 5] 1328 | ``` 1329 | 1330 |
1331 | Spec 1332 | • 1333 | Source: (map, arr) => arr.map(map); 1334 |
1335 | 1336 | 1337 | ### match 1338 | 1339 | Same as `haystack.match(needle)`. 1340 | 1341 | ```js 1342 | var match = require('1-liners/match'); 1343 | 1344 | match(/\d+/g, 'Items: 3,2'); // => ["3", "2"] 1345 | ``` 1346 | 1347 |
1348 | Spec 1349 | • 1350 | Source: (needle, haystack) => haystack.match(needle); 1351 |
1352 | 1353 | 1354 | ### max 1355 | 1356 | Same as `Math.max` – but with a stable number of arguments. 1357 | 1358 | ```js 1359 | var max = require('1-liners/max'); 1360 | 1361 | max(3, 6); // => 6 1362 | 1363 | [3, 6, 9].reduce(max); // => 9 1364 | [3, 6, 9].reduce(Math.max); // => NaN 1365 | ``` 1366 | 1367 |
1368 | Spec 1369 | • 1370 | Source: (a, b) => ((a > b) ? a : b); 1371 |
1372 | 1373 | 1374 | ### method 1375 | 1376 | Same as `object[method](...args)` 1377 | 1378 | ```js 1379 | const method = require('1-liners/method'); 1380 | 1381 | const object = { 1382 | base: 1, 1383 | add(number) { return this.base + number; }, 1384 | }; 1385 | 1386 | method('add', object)(5); // => 6 1387 | ``` 1388 | 1389 |
1390 | Spec 1391 | • 1392 | Source: (method, object) => (...args) => object[method](...args); 1393 |
1394 | 1395 | 1396 | ### min 1397 | 1398 | Same as `Math.min` – but with a stable number of arguments. 1399 | 1400 | ```js 1401 | var min = require('1-liners/min'); 1402 | 1403 | min(3, 6); // => 3 1404 | 1405 | [3, 6, 1].reduce(min); // => 1 1406 | [3, 6, 1].reduce(Math.min); // => NaN 1407 | ``` 1408 | 1409 |
1410 | Spec 1411 | • 1412 | Source: (a, b) => ((a > b) ? b : a); 1413 |
1414 | 1415 | 1416 | ### minus 1417 | 1418 | Same as `a - b` 1419 | 1420 | ```js 1421 | var minus = require('1-liners/minus'); 1422 | 1423 | minus(3, 2); // => 1 1424 | ``` 1425 | 1426 |
1427 | Spec 1428 | • 1429 | Source: (a, b) => a - b; 1430 |
1431 | 1432 | 1433 | ### nand 1434 | 1435 | Same as `!(a && b)`. 1436 | 1437 | ```js 1438 | var nand = require('1-liners/nand'); 1439 | 1440 | nand(0, 0); // => true 1441 | nand(1, 1); // => false 1442 | ``` 1443 | 1444 |
1445 | Spec 1446 | • 1447 | Source: (x, y) => !(x && y); 1448 |
1449 | 1450 | 1451 | ### noop 1452 | 1453 | Same as `function(){}`. 1454 | 1455 | ```js 1456 | var noop = require('1-liners/noop'); 1457 | 1458 | window.console = { 1459 | log: noop, 1460 | error: noop, 1461 | warn: noop, 1462 | table: noop 1463 | }; 1464 | ``` 1465 | 1466 |
1467 | Spec 1468 | • 1469 | Source: () => {}; 1470 |
1471 | 1472 | 1473 | ### nor 1474 | 1475 | Same as `!(a || b)`. 1476 | 1477 | ```js 1478 | var nor = require('1-liners/nor'); 1479 | 1480 | nor(0, 0); // => true 1481 | nor(1, 0); // => false 1482 | ``` 1483 | 1484 |
1485 | Spec 1486 | • 1487 | Source: (x, y) => !(x || y); 1488 |
1489 | 1490 | 1491 | ### not 1492 | 1493 | Same as `!a`. 1494 | 1495 | ```js 1496 | var not = require('1-liners/not'); 1497 | 1498 | not(true); // => false 1499 | not(false); // => true 1500 | ``` 1501 | 1502 |
1503 | Spec 1504 | • 1505 | Source: (a) => !a; 1506 |
1507 | 1508 | 1509 | ### nth 1510 | 1511 | Returns the nth item of an array. 1512 | 1513 | ```js 1514 | var nth = require('1-liners/nth'); 1515 | 1516 | nth(1, [1, 2, 3]); // => 2 1517 | ``` 1518 | 1519 |
1520 | Spec 1521 | • 1522 | Source: (n, arr) => arr[n]; 1523 |
1524 | 1525 | 1526 | ### or 1527 | 1528 | Same as `a || b`. 1529 | 1530 | ```js 1531 | var or = require('1-liners/or'); 1532 | 1533 | or(true, true); // => true 1534 | or(false, true); // => true 1535 | or(false, false); // => false 1536 | ``` 1537 | 1538 |
1539 | Spec 1540 | • 1541 | Source: (a, b) => a || b; 1542 |
1543 | 1544 | 1545 | ### pick 1546 | 1547 | Copies only specified `properties` from an `object` into a new object. 1548 | 1549 | ```js 1550 | const pick = require('1-liners/pick'); 1551 | 1552 | const object = {foo: 1, bar: 2, baz: 3}; 1553 | 1554 | pick(['foo', 'baz'], object); // => {foo: 1, baz: 3} 1555 | ``` 1556 | 1557 |
1558 | Spec 1559 | • 1560 | Source: (properties, object) => Object.assign({}, ...properties.map(key => ({[key]: object[key]}))); 1561 |
1562 | 1563 | 1564 | ### pipe 1565 | 1566 | Pipe arguments through functions. 1567 | 1568 | ```js 1569 | var pipe = require('1-liners/pipe'); 1570 | 1571 | pipe(f, g)(1, 2) === g(f(1, 2)); 1572 | ``` 1573 | 1574 |
1575 | Spec 1576 | • 1577 | Source: (f, g) => (...args) => g(f(...args)); 1578 |
1579 | 1580 | 1581 | ### pipeAll 1582 | 1583 | Pipe arguments through an array of functions. 1584 | 1585 | ```js 1586 | var pipeAll = require('1-liners/pipeAll'); 1587 | 1588 | pipeAll([f, g, h])(1, 2) === h(g(f(1, 2))); 1589 | ``` 1590 | 1591 |
1592 | Spec 1593 | • 1594 | Source: (fns) => fns.reverse().reduce( (f, g) => (...args) => f(g(...args)) ); 1595 |
1596 | 1597 | 1598 | ### plus 1599 | 1600 | Same as `a + b`. 1601 | 1602 | ```js 1603 | var plus = require('1-liners/plus'); 1604 | 1605 | plus(2, 8); // => 10 1606 | plus('a', 'b'); // => 'ab' 1607 | ``` 1608 | 1609 |
1610 | Spec 1611 | • 1612 | Source: (a, b) => a + b; 1613 |
1614 | 1615 | 1616 | ### product 1617 | 1618 | Returns the product of all items of an `array`. 1619 | 1620 | ```js 1621 | const product = require('1-liners/product'); 1622 | 1623 | product([2, 3, 4]); // => 24 1624 | product([]); // => 1 1625 | ``` 1626 | 1627 |
1628 | Spec 1629 | • 1630 | Source: (array) => array.reduce((a, b) => (a * b), 1); 1631 |
1632 | 1633 | 1634 | ### property 1635 | 1636 | Same as `object[property]` 1637 | 1638 | ```js 1639 | const property = require('1-liners/property'); 1640 | 1641 | const object = {foo: 1}; 1642 | 1643 | property('foo', object); // => 1 1644 | ``` 1645 | 1646 |
1647 | Spec 1648 | • 1649 | Source: (property, object) => object[property]; 1650 |
1651 | 1652 | 1653 | ### push 1654 | 1655 | Same as [push](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push) but immutable. 1656 | 1657 | ```js 1658 | var push = require('1-liners/push'); 1659 | 1660 | push(4, [1, 2, 3]); // => [1, 2, 3, 4] 1661 | ``` 1662 | 1663 |
1664 | Spec 1665 | • 1666 | Source: (element, arr) => [...arr, element]; 1667 |
1668 | 1669 | 1670 | ### put 1671 | 1672 | Same as `Object.assign({}, obj, {[key]: val})` 1673 | 1674 | ```js 1675 | const put = require('1-liners/put'); 1676 | 1677 | const object = {id: 1}; 1678 | 1679 | put('name', 'stoeffel', object); // => { id: 1, name: 'stoeffel' } 1680 | ``` 1681 | 1682 |
1683 | Spec 1684 | • 1685 | Source: (key, val, obj) => Object.assign({}, obj, {[key]: val}); 1686 |
1687 | 1688 | 1689 | ### reduce 1690 | 1691 | Same as `[1, 2, 3].reduce(sum)`. 1692 | 1693 | ```js 1694 | var reduce = require('1-liners/reduce'); 1695 | 1696 | reduce(sum, [1, 2, 3]); // => 6 1697 | ``` 1698 | 1699 |
1700 | Spec 1701 | • 1702 | Source: (reduce, arr) => arr.reduce(reduce); 1703 |
1704 | 1705 | 1706 | ### reduceRight 1707 | 1708 | Same as `[1, 2, 3].reduceRight(sub)`. 1709 | 1710 | ```js 1711 | var reduceRight = require('1-liners/reduceRight'); 1712 | 1713 | reduceRight(sub, [1, 2, 3]); // => -4 1714 | ``` 1715 | 1716 |
1717 | Spec 1718 | • 1719 | Source: (reduce, arr) => arr.reduceRight(reduce); 1720 |
1721 | 1722 | 1723 | ### replace 1724 | 1725 | Same as `haystack.replace(needle, replace)`. 1726 | 1727 | ```js 1728 | var replace = require('1-liners/replace'); 1729 | 1730 | replace(/\d+/g, sub => `"${sub}"`, 'Items: 3,2'); // => Items: "3","2" 1731 | replace(':', '=', 'Items: 3,2'); // => Items= 3,2 1732 | ``` 1733 | 1734 |
1735 | Spec 1736 | • 1737 | Source: (needle, replace, haystack) => haystack.replace(needle, replace); 1738 |
1739 | 1740 | 1741 | ### shallowClone 1742 | 1743 | Copy all properties of an object into a new plain object. 1744 | 1745 | ```js 1746 | import shallowClone from '1-liners/shallowClone'; 1747 | 1748 | const source = { 1749 | value: 'value', 1750 | reference: /reference/, 1751 | }; 1752 | const target = shallowClone(source); 1753 | 1754 | target === source // => false 1755 | target.value === source.value // => true 1756 | target.reference === source.reference // => true 1757 | ``` 1758 | 1759 |
1760 | Spec 1761 | • 1762 | Source: (object) => Object.assign({}, object); 1763 |
1764 | 1765 | 1766 | ### shave 1767 | 1768 | Shave ensures that a function is called with n arguments. 1769 | 1770 | ```js 1771 | var shave = require('1-liners/shave'); 1772 | 1773 | map(parseInt, [0, 1.1, 2.2]); // => [0, NaN, NaN] 1774 | map(shave(1, parseInt), [0, 1.1, 2.2]); // => [0, 1, 2] 1775 | ``` 1776 | 1777 |
1778 | Spec 1779 | • 1780 | Source: (shave, f) => (...args) => f(...(args.slice(0, shave))); 1781 |
1782 | 1783 | 1784 | ### signum 1785 | 1786 | Returns the [sign of a number](https://en.wikipedia.org/wiki/Signum_function). `1` if `n` is positive, `-1` if `n` is negative and `0` if `n` is `0`. Otherwise returns `NaN`. 1787 | 1788 | ```js 1789 | const signum = require('1-liners/signum'); 1790 | 1791 | signum(-5); // => -1 1792 | signum(-Infinity); // => -1 1793 | 1794 | signum(10); // => 1 1795 | signum(Infinity); // => 1 1796 | 1797 | signum(0); // => 0 1798 | signum(-0); // => 0 1799 | ``` 1800 | 1801 |
1802 | Spec 1803 | • 1804 | Source: (n) => (n === 0 ? 0 : (n > 0 ? 1 : (n < 0 ? -1 : NaN))); 1805 |
1806 | 1807 | 1808 | ### some 1809 | 1810 | Same as `[1,2,3].some(GreaterThan16)` 1811 | 1812 | ```js 1813 | var some = require('1-liners/some'); 1814 | 1815 | some(elem => elem > 16, [16,17,18]); // => true 1816 | ``` 1817 | 1818 |
1819 | Spec 1820 | • 1821 | Source: (some, arr) => arr.some(some); 1822 |
1823 | 1824 | 1825 | ### split 1826 | 1827 | Same as `'1-liners'.split('-')` 1828 | 1829 | ```js 1830 | var split = require('1-liners/split'); 1831 | 1832 | split('-', '1-liners'); // => [1, 'liners'] 1833 | ``` 1834 | 1835 |
1836 | Spec 1837 | • 1838 | Source: (split, str) => str.split(split); 1839 |
1840 | 1841 | 1842 | ### startsWith 1843 | 1844 | Same as [`str.startsWith(searchString)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith). 1845 | 1846 | ```js 1847 | const startsWith = require('1-liners/startsWith'); 1848 | 1849 | startsWith('1', '1-liners'); // => true 1850 | startsWith('stoeffel', 'nope'); // => false 1851 | ``` 1852 | 1853 |
1854 | Spec 1855 | • 1856 | Source: (searchString, str) => str.startsWith(searchString); 1857 |
1858 | 1859 | 1860 | ### startsWithAt 1861 | 1862 | Same as [`str.startsWith(searchString, position)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith). 1863 | 1864 | ```js 1865 | const startsWithAt = require('1-liners/startsWithAt'); 1866 | 1867 | startsWithAt(2, 'liners', '1-liners/startsWithAt'); // => true 1868 | startsWithAt(2, 'stoeffel', 'nope'); // => false 1869 | ``` 1870 | 1871 |
1872 | Spec 1873 | • 1874 | Source: (position, searchString, str) => str.startsWith(searchString, position); 1875 |
1876 | 1877 | 1878 | ### sum 1879 | 1880 | Sums all items of an `array`. 1881 | 1882 | ```js 1883 | const sum = require('1-liners/sum'); 1884 | 1885 | sum([1, 2, 3]); // => 6 1886 | sum([]); // => 0 1887 | ``` 1888 | 1889 |
1890 | Spec 1891 | • 1892 | Source: (array) => array.reduce((a, b) => (a + b), 0); 1893 |
1894 | 1895 | 1896 | ### tail 1897 | 1898 | Returns the tail of an array 1899 | 1900 | ```js 1901 | var tail = require('1-liners/tail'); 1902 | 1903 | tail([1, 2, 3]); // => [2, 3] 1904 | ``` 1905 | 1906 |
1907 | Spec 1908 | • 1909 | Source: ([,...tail]) => tail; 1910 |
1911 | 1912 | 1913 | ### take 1914 | 1915 | Take n items of an array. Same as `arr.slice(0, n)`. 1916 | 1917 | ```js 1918 | var take = require('1-liners/take'); 1919 | 1920 | take(2, [1, 2, 3]); // => [1, 2] 1921 | ``` 1922 | 1923 |
1924 | Spec 1925 | • 1926 | Source: (take, arr) => arr.slice(0, take); 1927 |
1928 | 1929 | 1930 | ### takeUntil 1931 | 1932 | Take items of an array until they fulfill a predicate. 1933 | 1934 | ```js 1935 | var takeUntil = require('1-liners/takeUntil'); 1936 | 1937 | takeUntil(i => i % 2 === 1, [2, 4, 6, 8, 7, 8, 8]); // => [2, 4, 6, 8] 1938 | ``` 1939 | 1940 |
1941 | Spec 1942 | • 1943 | Source: (pred, arr) => arr.reduce((newArr, i) => { if (pred(i)) arr.length = 0; else newArr.push(i); return newArr; }, []); 1944 |
1945 | 1946 | 1947 | ### takeWhile 1948 | 1949 | Take items of an array while they fulfill a predicate. 1950 | 1951 | ```js 1952 | var takeWhile = require('1-liners/takeWhile'); 1953 | 1954 | takeWhile(i => i % 2 === 0, [2, 4, 6, 8, 7, 8, 8]); // => [2, 4, 6, 8] 1955 | ``` 1956 | 1957 |
1958 | Spec 1959 | • 1960 | Source: (pred, arr) => arr.reduce((newArr, i) => { if (!pred(i)) arr.length = 0; else newArr.push(i); return newArr; }, []); 1961 |
1962 | 1963 | 1964 | ### test 1965 | 1966 | Same as [`regexObj.test(str)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test). 1967 | 1968 | ```js 1969 | const test = require('1-liners/test'); 1970 | const haystack = 'hAyHAYhayneEdLEHayHAy'; 1971 | 1972 | test(haystack, /needle/); // => false 1973 | test(haystack, /needle/i); // => true 1974 | ``` 1975 | 1976 |
1977 | Spec 1978 | • 1979 | Source: (str, regexObj) => regexObj.test(str); 1980 |
1981 | 1982 | 1983 | ### times 1984 | 1985 | Same as `a * b` 1986 | 1987 | ```js 1988 | var times = require('1-liners/times'); 1989 | 1990 | times(3, 2); // => 6 1991 | ``` 1992 | 1993 |
1994 | Spec 1995 | • 1996 | Source: (a, b) => a * b; 1997 |
1998 | 1999 | 2000 | ### uncurry 2001 | 2002 | Uncurry a function – collapse 2 lists of parameters into one. 2003 | 2004 | ```js 2005 | import uncurry from '1-liners/uncurry'; 2006 | 2007 | const f = (a) => (b) => a + b; 2008 | const fβ = uncurry(f); 2009 | fβ(1, 2); // => 3 2010 | 2011 | const g = (a) => (b, c) => a + b + c 2012 | const gβ = uncurry(g); 2013 | gβ(1, 2, 3); // => 6 2014 | ``` 2015 | 2016 |
2017 | Spec 2018 | • 2019 | Source: (f) => (a, ...rest) => f(a)(...rest); 2020 |
2021 | 2022 | 2023 | ### uncurry3 2024 | 2025 | Uncurry a function – collapse 3 lists of parameters into one. 2026 | 2027 | ```js 2028 | import uncurry3 from '1-liners/uncurry3'; 2029 | 2030 | const f = (a) => (b) => (c) => a + b + c; 2031 | const fβ = uncurry3(f); 2032 | fβ(1, 2, 3); // => 6 2033 | 2034 | const g = (a) => (b) => (c, d) => a + b + c + d; 2035 | const gβ = uncurry3(g); 2036 | gβ(1, 2, 3, 4); // => 10 2037 | ``` 2038 | 2039 |
2040 | Spec 2041 | • 2042 | Source: (f) => (a, b, ...rest) => f(a)(b)(...rest); 2043 |
2044 | 2045 | 2046 | ### values 2047 | 2048 | Get all values of an object 2049 | Same as `Object.keys(obj).map(i => obj[i])`. 2050 | 2051 | ```js 2052 | const values = require('1-liners/values'); 2053 | 2054 | values({ 100: 'a', 2: 'b', 7: 'c' }); // => ['a', 'b', 'c'] 2055 | values(['a', 'b', 'c']); // => ['a', 'b', 'c'] 2056 | ``` 2057 | 2058 |
2059 | Spec 2060 | • 2061 | Source: (obj) => Object.keys(obj).map(i => obj[i]); 2062 |
2063 | 2064 | 2065 | ### xor 2066 | 2067 | Same as `(x && !y) || (!x && y)` 2068 | 2069 | ```js 2070 | var xor = require('1-liners/xor'); 2071 | 2072 | xor(0, 1); // => 1 2073 | xor(1, 1); // => 0 2074 | ``` 2075 | 2076 |
2077 | Spec 2078 | • 2079 | Source: (x, y) => (x && !y) || (!x && y) 2080 |
2081 | 2082 | --------------------------------------------------------------------------------