Skip to content

KevinBonnoron/monadojs

Repository files navigation

MonadoJs

Commitizen friendly

Utility library for manipulating array, set, map & plain objects (à la lodash). Available in nodejs & in the browser. Contains also some utilities.

Getting started

To install just run

# npm
npm i monadojs
# yarn
yarn add monadojs

Then you can start using the library:

import { createFilterFn, filter, map, pipe, prop } from 'monadojs';

const persons = [
  { id: 1, firstName: 'James', lastName: 'Brown', age: 15, sex: 'M' },
  { id: 2, firstName: 'Robert', lastName: 'Jones', age: 30, sex: 'M' },
  { id: 3, firstName: 'Mary', lastName: 'Williams', age: 19, sex: 'F' },
  { id: 4, firstName: 'John', lastName: 'David', age: 26, sex: 'M' },
  { id: 5, firstName: 'Patricia', lastName: 'Brown', age: 52, sex: 'F' },
  { id: 6, firstName: 'Jennifer', lastName: 'Smith', age: 7, sex: 'F' },
];

// Get "firstName" for all persons into an array
const firstNameMapper = map(
  prop('firstName')
);
const firstNames = firstNameMapper(persons);

// Filter peoples which are older than 20
const firstNames = pipe(
  filter({ age: { $gt: 20 } }),
  map(
    prop('firstName')
  )
)(persons);

// You can create operator for later use
const ageGreaterThan = (age: number) => createFilterFn({ age: { $gt: age } });
const getFirstName = prop('firstName');
const firstNames2 = pipe(
  filter(ageGreaterThan(20)),
  map(getFirstName)
)(persons);

// You still can use regular array functions if needed
const firstNames3 = persons.filter(ageGreaterThan(20)).map(getFirstName);

There are some examples provided here

API

Filters

Logicals

Logical operators are function that can be used in combination with filter operator.

  • and
  • nand
  • nor
  • not
  • or
  • xor

Mappers

  • capitalize
  • exclude
  • lower
  • mapRange
  • math
  • pick
  • prop
  • replace
  • substring
  • transform
  • upper

Operators

Operators are functions that can be applyed on both object or array.

  • applySpec
  • catch-error
  • clone
  • combine
  • concat
  • difference
  • encloseIn
  • entries
  • every
  • filter
  • find
  • findIndex
  • freeze
  • get
  • groupBy
  • identity
  • iif
  • indexOf
  • keys
  • length
  • map
  • match
  • merge
  • none
  • pipe
  • pop
  • reduce
  • reverse
  • seal
  • shift
  • shuffle
  • some
  • sort
  • tap
  • throw-error
  • to
  • unique
  • values
  • walk

Reducers

Reducers are functions that produce a single result by iterating over the passed array.

  • avg
  • chunk
  • first
  • flatten
  • head
  • join
  • last
  • max
  • median
  • min
  • nth
  • sample
  • slice
  • sum
  • tail
  • zip

Sorters

  • asc
  • desc

Utils