objects-normalizer
v1.0.4
Published
Normalizes object keys, to have all the same ones
Downloads
1,399
Readme
Objects Normalizer
Code Quality Status
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' }
*/