3.7.0 • Published 7 years ago

paretojs v3.7.0

Weekly downloads
8
License
MIT
Repository
github
Last release
7 years ago

pareto.js

An extremely small, intuitive and fast functional utility library for JavaScript

  • Only 14 core functions
  • Written in TypeScript
  • Encourages immutability
  • Only pure functions (no side-effects)
  • Smaller than lodash

npm.io npm.io build downloads npm

Example

import { flatten, tail } from 'paretojs'

flatten([1, 2, [3, 4], 5]) // [1, 2, 3, 4, 5]
tail([1, 2, 3]) // [2, 3]

Installation

To install the stable version:

npm install --save paretojs

This assumes that you’re using npm with a module bundler like Webpack

How

ES2015 or TypeScript:

import _ from 'paretojs'

or

import { chunk, debounce } from 'paretojs'

CommonJS:

var _ = require('paretojs');

or

var chunk = require('paretojs').chunk;
var debounce = require('paretojs').debounce;

UMD:

<script src="https://unpkg.com/paretojs/dist/paretojs.min.js"></script>

API

chunk

Returns the chunk of an array based on an integer n

import { chunk } from 'paretojs';

chunk([1,2,3,4,5,6,7], 3); // [ [1,2,3], [4,5,6], [7] ]

compose

Gets a composed function

import { compose } from 'paretojs';

const toUpperCase = x => x.toUpperCase();
const exclaim = x => x + '!!!';

const angry = compose(toUpperCase, exclaim);

angry('stop'); // 'STOP!!!

curry

Gets a curried function

import { curry } from 'paretojs';

const add = (x, y) => x + y;

curry(add, 1, 2); // 3
curry(add)(1)(2); // 3
curry(add)(1, 2); // 3
curry(add, 1)(2); // 3

debounce

Creates and returns a new debounced version of the passed function which will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked.

import { debounce } from 'paretojs';

let a = 1;
const fn = () => a = 42;

const debounce = debounce(fn, 500);
debounce();

console.log(a); // 1 before 500ms

console.log(a); // 42 after 500ms

deepCopy

Creates a deep copy of an object

import { deepCopy } from 'paretojs';

const object = {
  a: 1,
  b: 2,
  c: {
    d: 3,
  },
};

deepCopy(object); // { a: 1, b: 2, c: { d: 3} }

flatMap

Generates a flattened array by iterating through a collection and applying a function to each element

import { flatMap } from 'paretojs';

const inc = n => n + 1;
flatMap([1, 2, 3], inc)); // [2, 3, 4]

const dup = n => [n, n];
flatMap([1, 2, 3], dup)); // [1, 1, 2, 2, 3, 3]

const sq = n => n ** 2;
flatMap([1, 2, 3], sq)) // [1, 4, 9]

flatten

Flattens (recursively) an array

import { flatten } from 'paretojs';

flatten([1, [2, 3], 4]); // [1, 2, 3, 4]

get

Gets the value of an object property based on a string path provided. If the property is not found, the defaultValues is returned

import { get } from 'paretojs';

get({ a: 1 }, "a")); // 1
get({ a: 1 }, "b", "default")); // "default"
get({ a: { b: 2 } }, "a")); // { b: 2 }
get({ a: { b: 2 } }, "a.b")); // 2
get({ a: { b: 2 } }, "a.c")); // undefined

matches

Checks if an objects matches with some properties

import { matches } from 'paretojs';

const object1 = { a: 1, b: 2 };

matches(object1, { a: 1 }); // true
matches(object1, { a: 1, b: 2 }); // true
matches(object1, { a: 3 }); // false

memoize

Creates a function that memoizes (caches) the result

import { memoize } from 'paretojs';

let count = 0;

const square = x => {
  count = count + 1;
  return x * x;
};

const memoSquare = memoize(square);

count; // 0
memoSquare(10); // 100
memoSquare(10); // 100
memoSquare(10); // 100
count; // 1

pipe

Creates and returns a new function that performs a left-to-right function composition.

import { pipe } from 'paretojs';

const increment = x => x + 1;
const decrement = x => x - 1;

const piped = pipe(increment, increment, decrement);
piped(0); // 1

prop

Gets the property of an object

import { prop } from 'paretojs';

const object = {
  label: 'custom label',
};

prop('label', object); // custom label

sort

Sorts a collection based on a property

import { sort } from 'paretojs';

const collection = [
  {
    id: 2,
  },
  {
    id: 1,
  },
];

sort(collection, 'id'); // [{ id: 1 }, { id: 2 }]

tail

Gets all, except the first element of an array.

import { tail } from 'paretojs';

tail([1, 2, 3]); // [2, 3]

Misc

If you want to add a new function, please open an issue and explain why.

Docs

3.7.0

7 years ago

3.6.0

7 years ago

3.5.1

7 years ago

3.5.0

7 years ago

3.4.1

7 years ago

3.4.0

7 years ago

3.3.1

7 years ago

3.3.0

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.2.2

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.10

8 years ago

2.0.7

8 years ago

2.0.6

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.10.0

8 years ago

0.9.20

8 years ago

0.9.19

8 years ago

0.9.18

8 years ago

0.9.17

8 years ago

0.9.16

8 years ago

0.9.15

8 years ago

0.9.14

8 years ago

0.9.13

8 years ago

0.9.12

8 years ago

0.9.11

8 years ago

0.9.10

8 years ago

0.9.9

8 years ago

0.9.8

8 years ago

0.9.7

8 years ago

0.9.6

8 years ago

0.9.5

8 years ago

0.9.4

8 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago

0.8.20

8 years ago

0.8.19

8 years ago

0.8.18

8 years ago

0.8.17

8 years ago

0.8.16

8 years ago

0.8.15

8 years ago

0.8.14

8 years ago

0.8.13

8 years ago

0.8.12

8 years ago

0.8.11

8 years ago

0.8.10

8 years ago

0.8.9

8 years ago

0.8.8

8 years ago

0.8.7

8 years ago

0.8.6

8 years ago

0.8.5

8 years ago

0.8.4

8 years ago

0.8.3

8 years ago

0.8.2

8 years ago

0.8.1

8 years ago

0.8.0

8 years ago

0.7.2

8 years ago

0.7.1

8 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago