Comparing version 1.1.0 to 2.0.0
// Constructor | ||
var Ul = module.exports = {}; | ||
function Ul() {} | ||
/** | ||
* merge | ||
* Merges two objects. The second parameter has higher priority. | ||
* That means the defaults will be passed in the first parameter. | ||
* Recursively merge the objects from arguments, returning a new object. | ||
* | ||
* @name merge | ||
* @function | ||
* @param {Object} obj1 The first object. | ||
* @param {Object} obj2 The second object. | ||
* @return {Object} The merged objects. | ||
*/ | ||
Ul.merge = function (obj1, obj2) { | ||
Ul.prototype.merge = function (/*obj1, obj2, obj3, ..., objn */) { | ||
for (var p in obj2) { | ||
try { | ||
if (obj2[p].constructor == Object) { | ||
obj1[p] = Ul.merge(obj1[p], obj2[p]); | ||
var dst = {} | ||
, src | ||
, p | ||
, args = [].splice.call(arguments, 0) | ||
; | ||
while (args.length > 0) { | ||
src = args.splice(-1)[0]; | ||
if (toString.call(src) != "[object Object]") { continue; } | ||
for (p in src) { | ||
if (!src.hasOwnProperty(p)) { continue; } | ||
if (toString.call(src[p]) == "[object Object]") { | ||
dst[p] = this.merge(dst[p] || {}, src[p]); | ||
} else { | ||
obj1[p] = obj2[p]; | ||
if (src[p] !== undefined) { | ||
dst[p] = src[p]; | ||
} | ||
} | ||
} catch (e) { | ||
obj1[p] = obj2[p]; | ||
} | ||
} | ||
return obj1; | ||
return dst; | ||
}; | ||
@@ -41,6 +47,7 @@ | ||
*/ | ||
Ul.clone = function (item) { | ||
Ul.prototype.clone = function (item) { | ||
if (!item) { return item; } | ||
var types = [Number, String, Boolean] | ||
var self = this | ||
, types = [Number, String, Boolean] | ||
, result | ||
@@ -60,3 +67,3 @@ , i | ||
item.forEach(function(child, index) { | ||
result[index] = Ul.clone(child); | ||
result[index] = self.clone(child); | ||
}); | ||
@@ -70,3 +77,3 @@ } else if (typeof item == "object") { | ||
for (i in item) { | ||
result[i] = Ul.clone(item[i]); | ||
result[i] = self.clone(item[i]); | ||
} | ||
@@ -86,2 +93,4 @@ } | ||
// Returns the absolute path to the user directory (`~/`) | ||
Ul.USER_DIR = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']; | ||
Ul.prototype.USER_DIR = process.env[(process.platform == "win32") ? "USERPROFILE" : "HOME"]; | ||
module.exports = new Ul(); |
{ | ||
"name": "ul", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "A minimalist utility library.", | ||
@@ -24,2 +24,2 @@ "main": "lib/index.js", | ||
"homepage": "https://github.com/IonicaBizau/node-ul" | ||
} | ||
} |
@@ -30,10 +30,5 @@ ul | ||
# Documentation | ||
## `merge(obj1, obj2)` | ||
Merges two objects. The second parameter has higher priority. | ||
That means the defaults will be passed in the first parameter. | ||
## `merge(/* obj1, obj2, obj3, ..., objN*/)` | ||
Recursively merge the objects from arguments, returning a new object. | ||
### Params | ||
- **Object** `obj1`: The first object. | ||
- **Object** `obj2`: The second object. | ||
### Return | ||
@@ -40,0 +35,0 @@ - **Object** The merged objects. |
@@ -12,7 +12,11 @@ var Ul = require("../lib"); | ||
} | ||
, last = { c: 1 } | ||
, tmp = null | ||
; | ||
console.log(tmp = Ul.merge(def, obj)); | ||
console.log(tmp = Ul.merge(obj, def)); | ||
console.log(tmp === Ul.clone(tmp)); | ||
console.log(Ul.USER_DIR); | ||
console.log(Ul.merge({}, obj, def, last)); | ||
console.log(Ul.merge({ a: { c: {}, d: 3 } }, { a: {d: undefined, c: {s: {}}} })); | ||
console.log(Ul.merge({ a: 4, b: 1 }, { b: 2, c: 3 })); |
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
5532
100
61