Socket
Book a DemoInstallSign in
Socket

obj-trans

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

obj-trans

module that will run a function/ functions recursively over an object to transform all of the values at any nest level

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

obj-trans

Description: module that will run a function/ functions recursively over an object to transform all of the values at any nest level

Getting Started:

npm install obj-trans --save

example:

objTrans = require('obj-trans');

var testObj = {one:1,two:2,nestOne:{one:1,two:2}};
var testFunction = function(x){return x*2);

return objTrans(testObj,testFunction);

//returns {one:2,two:4,nestOne:{one:2,two:4}}

//second param that takes a function can also take an array of functions.

Method:

objTrans(object,function/functions)

	params:
		object:the object your transforming
		function(functions): either a function or array of functions to run.
		
	return: newly transformed object.
	

Things to note:

  • when using an array of functions the first function will fully be applied and then the new object will be passed in and transformed with the next until the array is drained.
  • Remember if calling objTrans on the same object more then once the object is a ref so even though your not storing the return its changing the origional object.
  • Make sure to type check inside your transform functions because it will be ran on every value regardless of it being a string/number/array.
  • Arrays are also treated as values...so each value in an array is also ran through transform functions.

Function format:

function(x) { //x is the orig value pre transformation.
	return x*2 //return the orig value transformed.	
}

Keywords

object

FAQs

Package last updated on 16 Dec 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