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

objextension

v0.0.3

Published

The implementation of several methods the Object class is missing for all objects

Downloads

3

Readme

objExtension Build Status

The implementation of several methods the Object class that is missing for all objects

This uses Symbols to add to the prototype of the Object class, if you are not comfortable with this there is an option to add this to any class you please. This adds most of the array methods to an Object allowing you to use your functional programming on objects as well.

Install

$ npm install --save objextension

Usage

const o = require('objextension')();
let x = {
	a:1,
    b:{
    	c:4
    },
    d:[1,2,3],
    e:true
}
x[o.pick]('a')
//=> 1

Is this even safe?

I was reading this article and didn't even finish reading the article to see that he made an NPM module. I quickly made this package (and of course I like it better)

API

objextension([options])

Options

Type: object

Currently the only option to specify is toExtend where you can provide a class you would like to extend such as:

let single = function () {
    this.x = 34
}

let go = require('objextension')( {
    toExtend: single
} )

( new single() )[ go.pick ]( 'x' )
//=> 34

Methods that are now available to all objects

pick( query )

query

Type: string or Array of strings

Returns the value of an object the specified key.

x[ o.pick ]('a')
//=> 1

copy( )

Returns a copy of the current object.

let y = {
	a: 3
}
y[ o.copy ]()
//=> { a: 3 }

extend( objExtendingWith, [ otherObjects , , , ] )

objExtendingWith

Type: Object Description: The object whose properties will be copied onto a copy of the current object.

Returns a copy of the current object with objExtendingWith's properties deeply copied onto it. Multiple Objects can be placed in different arguments.

let obj={
		b:4
    },
	ext = {
		a: 3
	}
    
obj[ o.extend ](ext)
//=> { a: 3, b: 4 }

obj
//=> { b: 4 }

assign( objExtendingWith, [ otherObjects , , , ] )

objExtendingWith

Type: Object Description: The object whose properties will be copied onto the current object.

Merges the current object with objExtendingWith's properties deeply copied onto it. Therefore mutating the current object. Multiple Objects can be placed in different arguments.

let obj={
		b:4
    },
	ext = {
		a: 3
	}
    
obj[ o.assign ](ext)
//=> { a: 3, b: 4 }

obj
//=> { a: 3, b: 4 }

remove( query )

query

Type: string or Array of strings Description: The Key to be removed.

Returns the value of each key ( as an Array if query is an Array ).

x.removeMe = 'has been removed'
x[ o.remove ]( 'removeMe' )
//=> 'has been removed'

x.removeMe = 'has been removed'
x.meTo = 'also'
x[ o.remove ]( [ 'removeMe', 'meTo' ] )
//=> [ 'has been removed', 'also' ]

keys( )

Returns the keys of an object as an Array.

x[ o.keys ]()
//=> [ 'a', 'b', 'd' ]

values( )

Returns the values of an object as an Array.

x[ o.values ]()
//=>[ 1, { c:4 }, [ 1, 2, 3 ] ]

keyOf( query , searchingArray )

query

Type: Anything or Array of Anythings

searchingArray

Type: Boolean Description: If you are searching for an array as the value set this to true otherwise it will search for each value in that array. Default: false

Returns the key (or Array of keys) of the object where query is found.

x[ o.keyOf ](1)
//=> 'a'
x[ o.keyOf ]( [ 1, true ] )
//=> [ 'a', 'e' ]

includes( query )

query

Type: Anything or Array of Anythings

Returns true if and only if query is in the values of the object.

x[ o.includes ](1)
//=> true

has( query )

query

Type: Anything or Array of Anythings

Returns true if and only if query is in the keys of the object.

x[ o.has ]('nonexistent')
//=> false

forEach( callback )

callback

Type: function Accepts: (Current Value, Current Key, Object itself)

Performs callback once for each value of each key of the object.

x[ please.forEach ]( function ( currentValue, currentKey, obj ) {
        console.log(currentValue);
    } )
//=> 1
//=> { c:4 }
//=> [ 1, 2, 3 ]

map( callback )

callback

Type: function Accepts: (Current Value, Current Key, Object itself)

Performs callback once for each value of each key of the object. Returns a new object with the values transformed.

x[ please.map ]( function ( currentValue, currentKey, obj ) {
        if( Number(currentValue)===currentValue ){
        	return currentValue+1;
        }
        if( Array.isArray( currentValue ) ){
        	return currentValue.map(function(cur,i,arr){
            	return cur+2;
            });
        }
        return currentValue;
    } )
//=> { a:2, b:{ c:4 }, d:[ 3, 4, 5 ] }

every( callback )

callback

Type: function Accepts: (Current Value, Current Key, Object itself)

Performs callback once for each value of each key of the object. Returns true if and only if callback returns true for every value of the object.

let does=please;
x[ does.every ]( function ( currentValue, currentKey, obj ) {
        if( Number(currentValue)===currentValue ){
        	return true;
        }
        return false
    } )
 //=> false

none( callback )

callback

Type: function Accepts: (Current Value, Current Key, Object itself)

Performs callback once for each value of each key of the object. Returns true if and only if callback returns false for every value of the object.

let does=please;
x[ does.none ]( function ( currentValue, currentKey, obj ) {
        if( Number(currentValue)===currentValue ){
        	return true;
        }
        return false
    } )
 //=> false

some( callback )

callback

Type: function Accepts: (Current Value, Current Key, Object itself)

Performs callback once for each value of each key of the object. Returns true if callback returns true for any value of the object.

let does=please;
x[ does.some ]( function ( currentValue, currentKey, obj ) {
        if( Number(currentValue)===currentValue ){
        	return true;
        }
        return false
    } )
 //=> true

filter( callback )

callback

Type: function Accepts: (Current Value, Current Key, Object itself)

Performs callback once for each value of each key of the object. Returns a new object with the values if and only if callback returns true for the value of the object.

let does=please;
x[ does.filter ]( function ( currentValue, currentKey, obj ) {
        if( Number(currentValue)===currentValue ){
        	return true;
        }
        return false
    } )
 //=> { a: 1 }

License

MIT © Nick The Sick