mol-proto
Advanced tools
Comparing version 0.0.2 to 0.0.4
@@ -6,6 +6,7 @@ 'use strict'; | ||
extendProto: extendProto, | ||
createSubclass: createSubclass, | ||
makeSubclass: makeSubclass, | ||
extend: extend, | ||
clone: clone, | ||
createSubclass: createSubclass, | ||
makeSubclass: makeSubclass, | ||
deepExtend: deepExtend, | ||
allKeys: Object.getOwnPropertyNames.bind(Object), | ||
@@ -18,2 +19,3 @@ keyOf: keyOf, | ||
prependArray: prependArray, | ||
toArray: toArray, | ||
firstUpperCase: firstUpperCase, | ||
@@ -69,2 +71,26 @@ firstLowerCase: firstLowerCase | ||
function deepExtend(self, obj, onlyEnumerable) { | ||
return _extendTree(self, obj, onlyEnumerable, []); | ||
} | ||
function _extendTree(selfNode, objNode, onlyEnumerable, objTraversed) { | ||
if (objTraversed.indexOf(objNode) >= 0) return; // node already traversed | ||
objTraversed.push(objNode); | ||
_.eachKey(objNode, function(value, prop) { | ||
var descriptor = Object.getOwnPropertyDescriptor(objNode, prop); | ||
if (typeof value == 'object') { | ||
if (selfNode.hasOwnProperty(prop) && typeof selfNode[prop] == 'object') | ||
_extendTree(selfNode[prop], value, onlyEnumerable, objTraversed) | ||
else | ||
Object.defineProperty(selfNode, prop, descriptor); | ||
} else | ||
Object.defineProperty(selfNode, prop, descriptor); | ||
}, this, onlyEnumerable); | ||
return selfNode; | ||
} | ||
function clone(obj) { | ||
@@ -90,9 +116,3 @@ var clonedObject = Object.create(obj.constructor.prototype); | ||
// pprototype chain | ||
subclass.prototype = Object.create(thisClass.prototype); | ||
// subclass identity | ||
_.extendProto(subclass, { | ||
constructor: subclass | ||
}); | ||
_.makeSubclass(subclass, thisClass); | ||
@@ -109,4 +129,9 @@ // copy class methods | ||
function makeSubclass(thisClass, Superclass) { | ||
// prototype chain | ||
thisClass.prototype = Object.create(Superclass.prototype); | ||
thisClass.prototype.constructor = thisClass; | ||
// subclass identity | ||
_.extendProto(thisClass, { | ||
constructor: thisClass | ||
}); | ||
return thisClass; | ||
@@ -188,2 +213,12 @@ } | ||
function toArray(arrayLike) { | ||
var arr = []; | ||
Array.prototype.forEach.call(arrayLike, function(item) { | ||
arr.push(item) | ||
}); | ||
return arr; | ||
} | ||
function firstUpperCase(str) { | ||
@@ -190,0 +225,0 @@ return str[0].toUpperCase() + str.slice(1); |
{ | ||
"name": "mol-proto", | ||
"version": "0.0.2", | ||
"description": "Prototypes aware object manipulation library for browser", | ||
"version": "0.0.4", | ||
"description": "Prototypes aware object manipulation library for node and modern browsers", | ||
"main": "lib/proto.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -6,2 +6,4 @@ proto | ||
[![Build Status](https://travis-ci.org/MailOnline/proto.png?branch=master)](https://travis-ci.org/MailOnline/proto) | ||
Why not use underscore or lo-dash | ||
@@ -8,0 +10,0 @@ --------------------------------- |
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
26280
10
560
204
3