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

aobj

v1.0.7

Published

aobj - Object Manipulation Module for Nodejs

Downloads

7

Readme

aobj

AOBJ is an Object Manipulation Module for Nodejs

Setup

Install in your project using npm

npm install aobj

Require in your file as

const aobj = require('aobj');

Methods

extract

extract(obj: Object, keys: Array<String>) :Object

Extract keys/value into a new Object

const person = {name: 'Donald', lastname: 'Trump', favorite_color: 'red'}

aobj.extract(person, ['name', 'lastname'])
// {name: 'Donald', lastname: 'Trump'}

has

has(obj: Object, keys: Array<String>) :Boolean

Check if an Object has certain keys

const person = {name: 'Donald', lastname: 'Trump', favorite_color: 'red'}

aobj.has(person, 'lastname') // true
aobj.has(person, ['name', 'lastname']) // true
aobj.has(person, ['name', 'lastname', 'age']) // false

clone

clone(obj: Object) :Object

Deep Clones an Object

Without Cloning

const person = {name: 'Donald', lastname: 'Trump', favorite_color: 'red'}

const samePerson = person;
samePerson.name = 'Mike';

samePerson.name; // 'Mike'
person.name; // 'Mike'

With Deep Cloning

const person = {name: 'Donald', lastname: 'Trump', favorite_color: 'red'}

const anotherPerson = aobj.clone(person);
anotherPerson.name = 'Mike';

anotherPerson.name; // 'Mike'
person.name; // 'Donald'

isEmpty

isEmpty(obj: Object) :Boolean

Check if an Object is empty

const person = {name: 'Donald', lastname: 'Trump', favorite_color: 'red'}

aobj.isEmpty({}) // true

aobj.isEmpty(person) // false

isObject

isObject(obj: Object) :Boolean

Check if a variable is an Object

const person = {name: 'Donald', lastname: 'Trump', favorite_color: 'red'}

aobj.isObject(person) // true
aobj.isObject({}) // true

aobj.isObject('') // false
aobj.isObject(new Number(1)) // false
aobj.isObject(undefined) // false
aobj.isObject(null) // false

map

map(obj: Object, action: (key: String, value: any) => { key: String; value: any; })) :Object

Like Array.map for Objects (For deep objects use traverse)

const person = {
    name: 'Donald',
    age: 10
}

const newPerson = aobj.map(person, (key, value) => {
    if(key == 'age'){
        key = 'birthyear';
        value = 2020 - age;
    }
    return { key, value };
});

console.log(newPerson);
/*
{
    name: 'Donald',
    birthyear: 2010
}
*/

mapKeys

mapKeys(obj: Object, action: (key: String) => key: String)) :Object

Like Array.map for Objects Keys (For deep objects use traverseKeys)

const person = {
    name: 'Donald',
    age: 10
}

const newPerson = aobj.map(person, (key) => {
    if(key == 'name') key = 'fullname';
    return key;
});

console.log(newPerson);
/*
{
    fullname: 'Donald',
    birthyear: 2010
}
*/

mapValues

mapValues(obj: Object, action: (value: any) => value: any)) :Object

Like Array.map for Objects Values (For deep objects use traverse)

const person = {
    name: 'Donald',
    age: 10
}

const newPerson = aobj.mapValues(person, (value) => {
    if(typeof value == 'number') value += 50;
    return value;
});

console.log(newPerson);
/*
{
    name: 'Donald',
    age: 60
}
*/

traverse

traverse(obj: Object, action: (key: String, value: any) => { key: String; value: any; })) :Object

Like map but for deeply nested objects

const house = {
    rooms: {
        dinner_room: {
            m2: 30,
            chairs: {
                oldChair: {
                    m2: 0.2,
                    age: 5
                }
            }
        },
        bedroom: {
            m2: 30,
            gamerChair: {
                m2: 0.23,
                age: 1
            }
        }
    }
}

const newPerson = aobj.traverse(person, (key, value) => {
    if(key == 'age'){
        key = 'year';
        value = 2020 - age;
    }
    return { key, value };
});

console.log(newPerson);
/*
{
    rooms: {
        dinner_room: {
            m2: 30,
            chairs: {
                oldChair: {
                    m2: 0.2,
                    year: 2015
                }
            }
        },
        bedroom: {
            m2: 30,
            gamerChair: {
                m2: 0.23,
                year: 2019
            }
        }
    }
}
*/

traverseKeys

traverseKeys(obj: Object, action: (key: String) => key: String)) :Object

Like map but for deeply nested objects

const house = {
    rooms: {
        a: {
            m2: 120
        },
        b: {
            m2: 112
        },
        c: {
            m2: 117
        }
    }
}

const newPerson = aobj.traverseKeys(person, (key) => {
    if(key == 'a') key = 'Biggest_room';
    if(key == 'b') key = 'Small_room';
    if(key == 'c') key = 'Medium_room';
    return key;
});

console.log(newPerson);
/*
{
    rooms: {
        Biggest_room: {
            m2: 120
        },
        Small_room: {
            m2: 112
        },
        Medium_room: {
            m2: 117
        }
    }
}
*/

traverseValues

traverseValues(obj: Object, action: (value: String) => value: String)) :Object

Like map but for deeply nested objects

const house = {
    rooms: {
        a: {
            m2: 120
        },
        b: {
            m2: 112
        },
        c: {
            m2: 117
        }
    }
}

const newPerson = aobj.traverseValues(person, (value) => {
    return value + 100;
});

console.log(newPerson);
/*
{
    rooms: {
        a: {
            m2: 220
        },
        b: {
            m2: 212
        },
        c: {
            m2: 217
        }
    }
}
*/

TODO:

Tests for:

  • [ ] deepFilter
  • [ ] deepFilterKeys
  • [ ] deepFilterValues
  • [ ] filter
  • [ ] filterKeys
  • [ ] filterValues

Examples for:

  • [ ] deepFilter
  • [ ] deepFilterKeys
  • [ ] deepFilterValues
  • [ ] filter
  • [ ] filterKeys
  • [ ] filterValues
  • [ ] invert
  • [ ] extractDefault