Skip to content

MorganConrad/fobu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status License NPM Downloads Known Vulnerabilities Coverage Status

fobu

Yet another set of utilities to do "functional programming" on JavaScript Objects.
You can find many other versions of these online, but I like mine better :-).

Basically, allows you to use the ES6 array methods like map() and reduce() on objects.

Maybe you will find this useful. I mainly put it on GitHub so I can easily cut and paste these into other code.

API

All methods take the object as the first argument, and the user function (or "predicate") to be applied as the 2nd. The method will loop over the key/value pairs in the object, as determined by Object.keys(theObject).

The user function will usually be called with three arguments, much like the array versions.

  • The value
  • the key (for the corresponding array versions, this is the index)
  • the entire object

For reduce() and reduceRight(), the user function takes an additional acc` first argument.

every(object, predicate)

Tests if every key/value pair passes predicate(value, key, object). Returns true or false, and will "short-circuit".

find(object, predicate)

Finds the first key/value pair that passes predicate(value, key, object).

  • returns it as an array: [key, value]

filter(object, predicate)

Creates a new object with (shallow) copies of all key/value pairs that pass predicate(value, key, object).

forEach(object, fn)

Calls fn(value, key, object) for every key/value pair.

map(object, fn)

Returns a new result object, with result[key] = fn(object[key], key, object).

reduce(object, fn, initialValue)

reduceRight(object, fn, initialValue)

some(object, predicate)

Tests if any key/value pair passes predicate(value, key, object). Returns true or false, and will "short-circuit".

About

YA functional utilities for JavaScript Objects, mimicking ES6 Array methods.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors