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

js-object-methods

v1.0.0

Published

A JS library which provides more important methods other then methods which Object provides in JS.

Downloads

3

Readme

JSObject

A dependency free JS library which provides additional methods other then methods which Object provides in JS.

Functions available

1. freeze
2. cloneWithDescriptor    (clone deeply with property descriptors)
3. seal
4. isSealed
5. isFrozen
6. addProperty
7. keys
8. values
9. entries
10. keysDeep
11. valuesDeep
12. entriesDeep
13. flattenObj
14. deepClone
15. deepFreeze
16. newFreezedInstance

How to import

import JSObject from 'object-js'

Methods

// Let an example object

let obj = {
    outer: "dummy",
    nested: {
        inner: 'inner dummy',
    }
}
1. JSObject.freeze( objectToBefreezed )
// It freezes the passed object and also return it

JSObject.freeze(obj)
// OR
const freezedObj = JSObject.freeze(obj)
2. JSObject.cloneWithDescriptor( objectToBeCloned )
// It clone deeply with property descriptors
// It do nothing to passed object and returns cloned object (including the descriptors)

const freezedObj = JSObject.cloneWithDescriptor(obj)
3. JSObject.seal( objectToBeSealed )
// It seals the passed object and also return it

JSObject.seal(obj)
// OR
const sealedObj = JSObject.seal(obj)
4. JSObject.isSealed( objectToBeChecked )
// It return true if passed object is sealed, otherwise false

const isSealed = JSObject.isSealed(obj)
5. JSObject.isFrozen( objectToBeChecked )
// It return true if passed object is freezed, otherwise false

const isFrozen = JSObject.isFrozen(obj)
6. JSObject.addProperty( objInWhichPropertyToBeAdded, propertyName, propertyValue, isWritable, isEnumerable, isConfigurable )
// It add property to passed object and also returns it
// It is different then : obj['propertyName'] = 'propertyValue', because this way all descriptor flags are set to true

const isFrozen = JSObject.isFrozen(obj)
7. JSObject.keys( objWhoseKeysToBeFound )
// It returns array of keys name

const objKeysArray = JSObject.keys(obj)
8. JSObject.keysDeep( objWhoseKeysToBeFound, isSymbolicKeysAlsoRequired )
// It returns array of keys name
// if isSymbolicKeysAlsoRequired = true : returns symbolic keys also
// if isSymbolicKeysAlsoRequired = false : returns non-symbolic keys (all keys irrespective of enumerable flag value)

const objKeysArrayIncludingSymbolicKeys = JSObject.keysDeep(obj, true)
const objKeysArrayExcludingSymbolicKeys = JSObject.keysDeep(obj)
9. JSObject.values( objWhoseValuesToBeFound )
// It returns array of values

const objValuesArray = JSObject.values(obj)
10. JSObject.valuesDeep( objWhoseValuesToBeFound, isSymbolicValuesAlsoRequired )
// It returns array of values
// if isSymbolicValuesAlsoRequired = true : returns symbolic values also
// if isSymbolicValuesAlsoRequired = false : returns non-symbolic values

const objValuesArrayIncludingSymbolicValues = JSObject.valuesDeep(obj, true)
const objValuesArrayExcludingSymbolicValues = JSObject.valuesDeep(obj)
11. JSObject.entries( objWhoseEntriesToBeFound )
// It returns array of [key, value]

const objEntriesArray = JSObject.entries(obj)
12. JSObject.entriesDeep( objWhoseEntriesToBeFound, isSymbolicEntriesAlsoRequired )
// It returns array of values
// if isSymbolicEntriesAlsoRequired = true : returns symbolic entries also
// if isSymbolicEntriesAlsoRequired = false : returns non-symbolic entries

const objEntriesArrayIncludingSymbolicEntries = JSObject.entriesDeep(obj, true)
const objEntriesArrayExcludingSymbolicEntries = JSObject.entriesDeep(obj)

Let's take some other example for other methods

const obj = {
    name: 'aman',  
    class: '1',  
    nested1: { 
        nested2: { 
            name: "moar", 
            nested3: { 
                arr: ["1",'2'] 
            } 
        } 
    } 
}; 
13. JSObject.flattenObj( objToBeFlattened );
// returns an object with all keys-value pair at 0 level

const flattenedObj = JSObject.flattenObj(obj)
flattenedObj         // { name: 'moar', class: '1', arr: [ '1', '2' ] }
14. JSObject.deepClone( objToBeCloned, alreadyExistingVariableOrObject ) // OR
const result2 = JSObject.deepClone( objToBeCloned ) // OR
const result3 = JSObject.deepClone( objToBeCloned, {})
// Object.clone(obj) put same refernce as of parent for nexted object
// But this method clone it into new Object by creating new Object and not any reference


const result = {initialValue: 1}
JSObject.deepClone(obj, result)

const isEqual = result == obj;  // false 

result.nested1.nested2.name = "someName"  
result.nested1.nested2.name  // someName 
obj.nested1.nested2.name     // moar 
15. JSObject.deepFreeze( objToBeFreezed )
// Object.freeze(obj) freezes only keys-value at 0 level
// But this method freeze the whole object, even inner objects


const obj = { prop: { innerProp: 1 } };
JSObject.deepFreeze(obj);

obj.prop.innerProp = 5;
console.log(obj.prop.innerProp); // 1  
16. const newInstance = JSObject.newFreezedInstance( referenceObj )
// Returns a new freezed object


const obj = { prop: { innerProp: 1 } };
JSObject.newFreezedInstance(obj);

obj.prop.innerProp = 5;
console.log(obj.prop.innerProp); // 1