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

objects-normalizer

v1.0.4

Published

Normalizes object keys, to have all the same ones

Downloads

1,246

Readme

Objects Normalizer

Code Quality Status

Build Status Coverage Status

npm-object-normalizer

Description

Normalizes object keys, to have all the same ones

Installation

npm i objects-normalizer

Params

items

Object or Array of Objects.

The set of data to normalize.

Conditions

  • If set is empty, return an empty list.
  • If only one object is passed, only one object is return
  • List of objects, returns a list of objects

Examples

Single Object

{
    "name": "Kamala Khan",
    "alterego": "Miss Marvel",
    "superpower": "super-stretch"
}

List of Objects

[
    {
        "name": "Kamala Khan",
        "alterego": "Miss Marvel",
        "superpower": "super-stretch"
    },
    {
        "name": "Lois Lane",
        "alterego": "Lois Lane",
        "superpower": "press",
        "city": "Metropolis"
    },
]

options

Object.

The way to normalize.

  • fieldsToKeep: array of strings, list of fields to keep after formatting.
  • fieldsToRemove: array of strings, list of fields to remove.

Conditions

  • Only one of them, if both is passed, fieldsToKeep has priority, an will be considered the only option.
  • If any of them is passed, or another non-supported property, or nothing is passed, will return the same object or objects without modifications.

Examples

{
    "fieldsToKeep": ["name", "city"]
}
{
    "fieldsToRemove": ["name", "city"]
}

Usage

objectNormalizer(items)

Items without any option or valid options.

const objectsNormalizer = require('objects-normalizer');

objectsNormalizer({ name: 'Bruce Wayne', alterego: 'Batman', superPower: 'money' });

// It's equivalent
objectsNormalizer({ name: 'Bruce Wayne', alterego: 'Batman', superPower: 'money' }, {});
objectsNormalizer({ name: 'Bruce Wayne', alterego: 'Batman', superPower: 'money' }, { fieldsToDuplicate: ['city']});

/*
    output: 
    {
        name: 'Bruce Wayne',
        alterego: 'Batman',
        superPower: 'money'
    }
*/

objectsNormalizer(items, { fieldsToKeep: [fields] })

Format items to only keep some fields. If these fields not exist in the items, will not be added.

const objectsNormalizer = require('objects-normalizer');

const superHeroes = [
    { name: 'Bruce Wayne', alterego: 'Batman', superPower: 'money', city: 'Gotham' },
    { name: 'Peter Parker', alterego: 'Spiderman', superPower: 'spider things but stronger', city: 'New York' },
    { name: 'Zatanna Zatara', alterego: 'Zatanna', superPower: 'magic'},
    { name: 'Ororo Monroe', alterego: 'Storm', superPower: 'weather control' }
];

objectsNormalizer(superHeroes, { fieldsToKeep:['name', 'city'] });

// It's equivalent
objectsNormalizer(superHeroes, { fieldsToKeep:['name', 'city'], fieldsToRemove: ['alterego', 'superPower'] });
objectsNormalizer(superHeroes, { fieldsToKeep:['name', 'city'], fieldsToRemove: ['name', 'city'] });

/*
    output: 
    [
        { name: 'Bruce Wayne', city: 'Gotham' },
        { name: 'Peter Parker', city: 'New York' },
        { name: 'Zatanna Zatara' },
        { name: 'Ororo Monroe' }
    ]
*/

objectsNormalizer(superHeroes[0], { fieldsToKeep:['name', 'city'] });

/*
    output: { name: 'Bruce Wayne', city: 'Gotham' }
*/

objectsNormalizer(items, { fieldsToRemove: [fields] })

Format items to remove some fields and keep the others.

const objectsNormalizer = require('objects-normalizer');

const superHeroes = [
    { name: 'Bruce Wayne', alterego: 'Batman', superPower: 'money', city: 'Gotham' },
    { name: 'Peter Parker', alterego: 'Spiderman', superPower: 'spider things but stronger', city: 'New York' },
    { name: 'Zatanna Zatara', alterego: 'Zatanna', superPower: 'magic'},
    { name: 'Ororo Monroe', alterego: 'Storm', superPower: 'weather control' }
];

objectsNormalizer(superHeroes, { fieldsToRemove:['name', 'city'] });

/*
    output: 
    [
        { alterego: 'Batman', superPower: 'money' },
        { alterego: 'Spiderman', superPower: 'spider things but stronger' },
        { alterego: 'Zatanna', superPower: 'magic'},
        { alterego: 'Storm', superPower: 'weather control' }
    ]
*/

objectsNormalizer(superHeroes[0], { fieldsToRemove:['name', 'city'] });

/*
    output: { alterego: 'Batman', superPower: 'money' }
*/