npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

objfp

v1.2.0

Published

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

Downloads

2

Readme

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}