core-extensions
Advanced tools
Comparing version 0.0.7 to 0.1.0
77
index.js
@@ -9,3 +9,3 @@ "use strict"; | ||
enumerable: false, | ||
value: function () { | ||
value: function () { | ||
@@ -15,12 +15,3 @@ var target = null; | ||
if ( this === undefined ) { | ||
target = undefined; | ||
} | ||
else if ( this === null ) { | ||
target = null; | ||
} | ||
else if ( type == 'string' || type == 'number' || type == 'boolean' ) { | ||
target = this; | ||
} | ||
else if ( this instanceof Date ) { | ||
if ( this instanceof Date ) { | ||
target = new Date( this.toISOString() ); | ||
@@ -31,4 +22,4 @@ } | ||
} | ||
else { // functions, etc. not clonable yet | ||
target = undefined; | ||
else { // functions, etc. not clonable yet, just pass through | ||
target = this; | ||
} | ||
@@ -40,12 +31,21 @@ | ||
var mixinDepth = 0; | ||
Object.defineProperty( Object.prototype, "mixin", { | ||
enumerable: false, | ||
value: function () { | ||
value: function () { | ||
mixinDepth++; | ||
if ( mixinDepth >= 100 ) { | ||
throw new Error( 'max mixin depth of ' + mixinDepth + ' reached' ); | ||
} | ||
var child = this.clone(); // clone so we don't modify the original | ||
// Handle case when target is a string or something (possible in deep copy) | ||
if ( typeof child !== "object" && typeof child != 'function' ) { | ||
if ( typeof child !== "object" && typeof child !== 'function' ) { | ||
child = {}; | ||
} | ||
var childIsArray = Array.isArray( child ); | ||
@@ -67,8 +67,24 @@ // handle arbitrary number of mixins. precedence is from last to first item passed in. | ||
// only allow integer fields from parent if target is an array | ||
if ( childIsArray ) { | ||
var parsedName = parseFloat( name ); | ||
// detect not numbers | ||
if ( isNaN( parsedName ) ) { | ||
continue; | ||
} | ||
// detect floating point | ||
if ( name.length !== Math.floor( parsedName ).toString().length ) { | ||
continue; | ||
} | ||
} | ||
var target = child[ name ]; | ||
var source = parent[ name ]; | ||
// Prevent never-ending loop | ||
if ( child === source ) { | ||
continue; | ||
if ( typeof target === 'function' || typeof source === 'function' ) { | ||
throw new Error( 'mixin does not support function values' ); | ||
} | ||
@@ -79,2 +95,3 @@ | ||
// can't replace an array in the target with anything else | ||
if ( Array.isArray( source ) ) { | ||
@@ -85,8 +102,3 @@ | ||
if ( typeof child[ name ][j] === 'object' ) { | ||
child[ name ][j] = child[ name ][j].mixin( source[j] ); | ||
} | ||
else { | ||
child[ name ][j] = source[j]; | ||
} | ||
child[ name ][j] = child[ name ][j].mixin( source[j] ); | ||
@@ -98,13 +110,19 @@ } | ||
} | ||
// if target is an object, try to mixin source | ||
else if ( Object.isObject( target ) ) { | ||
child[ name ] = target.mixin( source ); | ||
} | ||
target.mixin( source ); | ||
else if ( source === undefined ) { | ||
child[ name ] = undefined; | ||
} | ||
else if ( source === null ) { | ||
child[ name ] = null; | ||
} | ||
// otherwise, target becomes source | ||
else { | ||
child[ name ] = source; | ||
child[ name ] = source.clone(); | ||
} | ||
@@ -116,3 +134,6 @@ } | ||
mixinDepth--; | ||
return child; | ||
} | ||
@@ -119,0 +140,0 @@ } ); |
{ | ||
"name": "core-extensions", | ||
"name": "core-extensions", | ||
"description": "A set of extensions to the node core library, such as an isObject method to parallel isArray, a clone method and a mixin method.", | ||
"author": "Anthony Hildoer <anthony@bluerival.com>", | ||
"version": "0.0.7", | ||
"repository": { | ||
"author": "Anthony Hildoer <anthony@bluerival.com>", | ||
"version": "0.1.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/bluerival/node-core-extensions.git" | ||
"url": "git://github.com/bluerival/node-core-extensions.git" | ||
}, | ||
"keywords": [ | ||
"keywords": [ | ||
"object", | ||
@@ -18,6 +18,9 @@ "array", | ||
], | ||
"engines": { | ||
"devDependencies": { | ||
"mocha": "~1" | ||
}, | ||
"engines": { | ||
"node": "~0.8" | ||
}, | ||
"license": "MIT" | ||
"license": "MIT" | ||
} |
core-extensions | ||
==================== | ||
A set of extensions to the node core library, such as an isObject method to parallel isArray, a clone method and a mixin method. | ||
A set of extensions to the node core library, such as an Object.isObject() | ||
method to parallel Array.isArray(), a clone() method on all objects and a | ||
mixin() method on all objects. | ||
Documentation | ||
==================== | ||
For the time being, please see the test cases in test/default.js to understand | ||
exactly how Object.isObject(), clone() and mixin() behave. | ||
TO DO | ||
==================== | ||
- Write test suite (mocha maybe?) | ||
- Add feature of custom clone functions based on object type | ||
- Make each extension optional | ||
- write examples in this doc | ||
- add support of custom clone functions based on object type | ||
- add support of defaulting to clone() methods already on items to be cloned | ||
- make each extension optional | ||
- refactoring: mixin has grown unruly | ||
@@ -14,0 +25,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
15001
7
413
44
1
1