Comparing version 2.5.6 to 2.6.0
@@ -134,2 +134,59 @@ 'use strict' | ||
assigner.mask = function (source, mask) { | ||
var self = this | ||
if (!source || !mask) return source | ||
if (_.isPlainObject( source ) ) { | ||
var object = { } | ||
var index = -1, props = Object.keys( mask ), length = props.length | ||
while (++index < length) { | ||
var key = props[index] | ||
if ( !mask[key] ) continue | ||
object[ key ] = _.isPlainObject( mask[key] ) ? self.mask( source[key], mask[key] ) : source[key] | ||
} | ||
return object | ||
} | ||
else if (_.isArray( source ) ) { | ||
var array = [] | ||
source.forEach( function ( element ) { | ||
array.push( self.mask( element, mask ) ) | ||
} ) | ||
return array | ||
} | ||
return source | ||
} | ||
assigner.purify = function (source, mask) { | ||
var self = this | ||
if (!source || !mask) return source | ||
if (_.isPlainObject( source ) ) { | ||
var object = { } | ||
var index = -1, props = Object.keys( source ), length = props.length | ||
while (++index < length) { | ||
var key = props[index] | ||
if ( !mask[key] ) object[ key ] = source[key] | ||
else if ( _.isPlainObject(mask[key]) ) object[ key ] = self.purify( source[key], mask[key] ) | ||
} | ||
return object | ||
} | ||
else if (_.isArray( source ) ) { | ||
var array = [] | ||
source.forEach( function ( element ) { | ||
array.push( self.purify( element, mask ) ) | ||
} ) | ||
return array | ||
} | ||
return source | ||
} | ||
assigner.pick = function (object, properties) { | ||
@@ -136,0 +193,0 @@ var obj = { } |
{ | ||
"name": "assign.js", | ||
"version": "2.5.6", | ||
"version": "2.6.0", | ||
"description": "Tiny libary to assign objects", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -71,4 +71,12 @@ Assign.js - dependency-free very minimal assign function | ||
assigner.cloneObject( source ) | ||
assigner.cloneObject( source ) | ||
This created a cloned object from the source copied recursively. | ||
assigner.mask( source, mask ) | ||
This created an object with the references only allowed by the mask object. | ||
assigner.purify( source, mask ) | ||
This created an object with the references only not denifed by the mask object. |
@@ -24,2 +24,40 @@ 'use strict' | ||
} | ||
let entity = { | ||
name: 'Peter', | ||
id: 10, | ||
address: { | ||
street: 'Nowhere 1', | ||
country: 'UK' | ||
}, | ||
social: { | ||
twitter: '', | ||
facebook: '' | ||
} | ||
} | ||
// keep | ||
let mask = { | ||
name: true, | ||
address: { | ||
country: true | ||
}, | ||
social: { | ||
twitter: true, | ||
facebook: false | ||
} | ||
} | ||
// remove | ||
let purify = { | ||
id: 10, | ||
address: { | ||
street: true | ||
}, | ||
social: { | ||
twitter: true, | ||
facebook: false | ||
} | ||
} | ||
before(function (done) { | ||
@@ -64,2 +102,13 @@ done() | ||
}) | ||
it('Masking', function (done) { | ||
let res = assigner.mask( entity, mask ) | ||
console.log( res ) | ||
done() | ||
}) | ||
it('Purifying', function (done) { | ||
let res = assigner.purify( entity, purify ) | ||
console.log( res ) | ||
done() | ||
}) | ||
}) | ||
@@ -66,0 +115,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16554
427
82