GithubHelp home page GithubHelp logo

concretesolutions / pareto.js Goto Github PK

View Code? Open in Web Editor NEW
261.0 17.0 20.0 272 KB

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

License: Other

JavaScript 15.10% TypeScript 84.90%
javascript functional lightweight fast utility hacktoberfest

pareto.js's Introduction

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

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

setTimeout(() => {
  console.log(a); // 42 after 500ms
}, 600)

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

pareto.js's People

Contributors

matheusml avatar raphaelpor avatar brunoziie avatar

Stargazers

alex rodriguez avatar Will avatar Mohammed Mehdi avatar Hippolyte L avatar Faded Weiss avatar Alexandre Rigão avatar KarlitosD avatar Rebeca Baruch avatar Rahul Ballal avatar qi avatar Devm avatar Luis Mendes avatar Adam Yarger avatar Agoro, Adegbenga. B avatar  avatar Jose Francisco diaz lopez avatar José Romildo Mendes Júnior avatar Julio Barbosa avatar  avatar K Dh avatar Wender Lucas avatar Léu Almeida avatar Carlos Daniel avatar Eduardo Oliveira Fernandes avatar sonopictorial avatar  avatar Bohdan Moroziuk avatar Sacha Froment avatar BeA avatar Richard Anchieta avatar Jan Christoph Ebersbach avatar Robson  avatar Lucas Ferreira avatar Dayvson Marques avatar Henrique Vignando avatar  avatar Ketan Sangle avatar Felipe Maziero Pereira avatar Ernesto Araujo avatar Lucas Inocente avatar Dalton avatar Lucivaldo avatar Florian Bellazouz avatar  avatar Patrick Ferraz avatar Arthemus avatar Diêgo Bolina avatar Peter DeMartini avatar liliangyun avatar Lucas Alexander Floriani avatar  avatar Douglas Zaltron avatar Igor Guastalla avatar Guilherme Matias avatar Fernando Migliorini Tenório avatar Marcos Florencio avatar  avatar Yago Azedias avatar Wesley Luyten avatar Abdelrahman Elsaidy avatar Jackson Ricardo Schroeder avatar Franklin Andrade avatar Marcus Duarte avatar Nuno Rafael avatar Julio Augusto avatar Hermes Netto avatar luo0412 avatar Lucas Pessoa avatar LEI avatar Fábio Neves avatar Steve Brand avatar  avatar Igor Berlenko avatar Illia Diachenko avatar Dayana Maia avatar Everaldo Lima avatar Mauricio Nobrega avatar Wagner Souza avatar Alex Galhardo avatar Anton St. avatar Wellington Vieira avatar John Lee avatar Luan AP avatar Erick avatar Zach Sherman avatar Raphael Kieling avatar Valerii Kravchenko avatar Constantine Genchevsky avatar Loc Phan avatar Tamim Ibrahim avatar Stacey Reiman avatar Matheus I. Pedro avatar Julio Xavier avatar Eduardo Borges avatar Matheus Alves avatar Pablo Salgado avatar Luiz Guilherme avatar Leandro Kersting de Freitas avatar John Huang avatar Tim Davis avatar

Watchers

Victor Lima avatar Alexandre Bairos de Medeiros avatar Bruno Moneró avatar  avatar timelyportfolio avatar  avatar Thiago Lioy avatar Marcus Ortense avatar Leandro Nunes avatar Caio Fossá avatar  avatar Victor Oliveira Nascimento avatar Wesley Rodrigues da Silva avatar Luis Mendes avatar Julio César da Silva avatar Fábio Neves avatar  avatar

pareto.js's Issues

Cannot resolve module 'paretojs' in webpack

I'm using webpack 1.11.0. The library is installed and I try use:

import _ from 'paretojs';

result:

Module not found: Error: Cannot resolve module 'paretojs'

how to solve?

Equals not working with complex objects

Hi,

The comparison between objects work with simple objects:

_.equals({ a:1 }, { a:1}) // true
_.equals({ a:1 }, { a:2}) // false

but not working with complex objects:

_.equals({ a:[] }, { a:[]}) // false

[BUG] - Teste da função flatMap com problemas

Estou "brincando" com a lib e em uma dessa brincadeiras reparei que o teste da função flatMap pode estar falho, pois quando realizo o teste ele me retorna:

Uncaught TypeError: fn(...) is not a function or its return value is not iterable

Ou seja, a função inc() (referente ao primeiro teste), não retorna um valor iterável consequentemente o teste sempre irá falhar, porém quando colocamos esse retorno em um iterável (array) os testes passam.

Na função temos um spread operator e como sabemos ele funciona em arrays e objetos, e as funções testadas não retornavam isso.

export default function flatMap<T, U>(array: T[], fn: Function): U[] {
  return array.reduce((acc, current) => [...acc, ...fn(current)], []);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.