Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

normalize-obj

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalize-obj

Rename keys, and change structure of an object

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
2
Weekly downloads
 
Created
Source

Build Status Coverage Status npm version

normalize-obj

Rename keys, and/or change structure of an object.

Install

npm install normalize-obj --save

How to use

var normalize = require('normalize-obj');

var object = {name: 'Darlan', age: 25, address: {num: '117'}};

var normalized = normalize(object).change('name', 'fullname');
// => {fullname: 'Darlan', age: 25, address: {num: '117'}};

Methods can be chained, example:

normalize(object)
	.change('name', 'fullname')
	.change('age', 'old');
// => {fullname: 'Darlan', old: 25, address: {num:  '117'}};

Accept nesting keys, with dot syntax, to change key name

normalize(object).change('address.num', 'address.number');
// => {name: 'Darlan', age: 25, address: {number: '117'}};

or change structure too

normalize(object).change('address.num', 'number');

// => {name: 'Darlan', age: 25, number: '117'};

Important

On change structure, like below, if old structure don't have others properties, there are deleted. I.e:

var object = {
	address: {
		street: 'Paulista',
		country: 'br',
		num: '1107'
	},
	phone: {
		mobile: '0000-0000'
	}
};

normalize(object).change('address.num', 'number');
/* => 
	{
		number: 1107,
		// keep address, because have others properties
		address: {
			street: 'Paulista',
			country: 'br'
		},
		phone: {
			mobile: '0000-0000'
		}
	};

*/

// now, if dont have properties
normalize(object).change('phone.mobile', 'mobile');

/* => 
	{
		number: 1107,
		address: {
			street: 'Paulista',
			country: 'br'
		},
		mobile: '0000-0000'
		// delete phone, because don't have others properties
	};

*/

And offer method to copy field

normalize(object).copy('address.num', 'number');
// => {name: 'Darlan', age: 25, address: {num: '117'}, number: '117'};
Tests
npm test

Keywords

FAQs

Package last updated on 28 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc