objfp

A library dedicated to functional-esque programming methods for JavaScript objects.


Keywords
javascript, objects, functional
License
MIT
Install
npm install objfp@1.2.0

Documentation

ObjFP

A library dedicated to functional-esque programming methods for JavaScript objects.

npm install objfp

const ObjFP = require('objfp');

Or include as a global:

<script src="[path_to_module]/ObjFP.js"></script>

 

Methods

ObjFP.map()

The ObjFP.map() method accepts an object reference and a callback, and returns a new object with the same property keys, but with values equal to the transformation applied via the supplied callback. This is a pure function, so the original object is not mutated. This works similar to JavaScript's Array.prototype.map() method.

Method signature:

ObjFP.map(obj, callback)

Method parameters:

  • obj - a JavaScript object with at least one property
  • callback - a function that will determine the transformation to be applied to each property value

Callback signature:

fn(value[, key, index, origObjRef])

Callback parameters:

  • value - the value of the property to be transformed
  • key (optional) - the key of the property currently being iterated over
  • index (optional) - the index number of the key currently being iterated over (Object.keys() is utilized)
  • origObjRef (optional) - a reference to the original object

Example:

// original object reference
let obj = {a: 1, b: 2, c: 3};

// create new object with transformation
let newObj = ObjFP.map(obj, val => val * 2);

console.log(newObj); // {a: 2, b: 4, c: 6}
console.log(obj); // {a: 1, b: 2, c: 3}

 


ObjFP.filter()

The ObjFP.filter() method accepts an object reference and a callback, and returns a new object with property keys determined by the result of the supplied callback's evaluation. This is a pure function, so the original object is not mutated. This works similar to JavaScript's Array.prototype.filter() method.

Method signature:

ObjFP.filter(obj, callback)

Method parameters:

  • obj - a JavaScript object with at least one property
  • callback - a function that will determine which properties to keep/remove

Callback signature:

fn(value[, key, index, origObjRef])

Callback parameters:

  • value - the value of the property currently being evaluated
  • key (optional) - the key of the property currently being iterated over
  • index (optional) - the index number of the key currently being iterated over (Object.keys() is utilized)
  • origObjRef (optional) - a reference to the original object

Example:

// original object reference
let obj = {a: 1, b: 2, c: 3, d: 4};

// create new object with transformation
let newObj = ObjFP.filter(obj, val => val > 2);

console.log(newObj); // {c: 3, d: 4}
console.log(obj); // {a: 1, b: 2, c: 3, d: 4}

 


ObjFP.reduce()

The ObjFP.reduce() method accepts an object reference and a callback (and an optional initial value), and returns a single accumulated value based on the transformation applied by the callback function. This is a pure function, so the original object is not mutated. This works similar to JavaScript's Array.prototype.reduce() method.

Method signature:

ObjFP.reduce(obj, callback[, initialValue])

Method parameters:

  • obj - a JavaScript object with at least two properties (or one property if initialValue supplied)
  • callback - a function that will determine how to accumulate the final value
  • initialValue (optional) - the value with which the accumulator starts (if not supplied, will be the first property in the original object reference)

Callback signature:

fn(accumulator, currentValue[, currentIndex, origObjRef])

Callback parameters:

  • accumulator - the accumulated value in the last invocation of the callback (or initialValue, if supplied)
  • currentValue - the current object property being iterated over
  • currentIndex (optional) - the index number of the key currently being iterated over (Object.keys() is utilized)
  • origObjRef (optional) - a reference to the original object

Example:

// original object reference
let obj = {a: 1, b: 2, c: 3, d: 4};

// create an accumulated value, providing an initial value
let result = ObjFP.reduce(obj, (acc, curr) => acc + curr, 10);

console.log(result); // 20
console.log(obj); // {a: 1, b: 2, c: 3, d: 4}

 


Chaining ObjFP Methods

The ObjFP.map(), .filter(), and .reduce() methods can be chained together without the need for nesting or creating separate values from multiple function calls. These functions return a new object with all other methods accessible on the object itself. The difference between these methods and the original methods is that the ones present on the return object need only the callback passed in as a single argument.

Note: The initialValue parameter is also available for .reduce(). Additionally for .reduce(), these chainable methods are only available when an object is the returned value.

Example:

// original object reference
let obj = {a: 1, b: 2, c: 3, d: 4};

// create an accumulated value using all three methods, providing an initial value for the reduce() method
let result = ObjFP.map(obj, val => val * 2)
  .filter(val => val > 4)
  .reduce((acc, curr) => acc + curr, 10);

console.log(result); // 24
console.log(obj); // {a: 1, b: 2, c: 3, d: 4};

 

Clean (Non-Chainable) Object Return

In the event that these chainable methods are not desired on the final object return, they can be quickly and easily removed by using the .clean() method. Instead of manually deleting the properties from the object, simply add this method to the end of the method chain. The original object is not mutated.

Note: This does not remove any native prototype methods.

Example

// original object reference
let obj = {a: 1, b: 2};

// create new object by chaining methods, then return a 'clean' result
let result = ObjFP.map(obj, val => val * 2)
  .filter(val => val > 2)
  .clean();
                  
console.log(result); // {b: 4}
console.log(result.hasOwnProperty('map')); // false
console.log(result.hasOwnProperty('filter')); // false
console.log(result.hasOwnProperty('reduce')); // false
console.log(result.hasOwnProperty('clean')); // false
console.log(obj); // {a: 1, b: 2}