Comparing version 1.3.1 to 1.4.0
76
oow.js
@@ -1,25 +0,53 @@ | ||
const Collection = { | ||
isEmpty() { return this.length === 0; }, | ||
notEmpty() { return !this.isEmpty(); }, | ||
first() { return this[0]; }, | ||
last() { return this[this.length-1]; }, | ||
any(predicate) { | ||
for (let elem in this) | ||
if (predicate(this[elem])) return true; | ||
return false; | ||
}, | ||
all(predicate) { | ||
return !this.any(elem => !predicate(elem)); | ||
} | ||
}; | ||
// ideal implementation, not recommended | ||
//Object.setPrototypeOf(Array.prototype, Collection); | ||
//Object.setPrototypeOf(String.prototype, Collection); | ||
// implementation w/o changing prototypes hierarchy | ||
Object.keys(Collection).forEach(function (methodName) { | ||
[Array.prototype, String.prototype].forEach(function (proto) { | ||
Object.defineProperty(proto, methodName, { value: Collection[methodName] }); | ||
(function () { | ||
const Collection = { | ||
isEmpty() { return this.length === 0; }, | ||
notEmpty() { return !this.isEmpty(); }, | ||
first() { return this[0]; }, | ||
last() { return this[this.length-1]; }, | ||
any(predicate) { | ||
for (let elem in this) | ||
if (predicate(this[elem])) return true; | ||
return false; | ||
}, | ||
all(predicate) { | ||
return !this.any(elem => !predicate(elem)); | ||
}, | ||
take(n) { return this.slice(0, n); }, | ||
drop(n) { return this.slice(n, this.length); } | ||
}; | ||
const ArrayExtensions = { | ||
equals(array) { | ||
if (!array) return false; | ||
if (this.length !== array.length) return false; | ||
for (let i = 0, l=this.length; i < l; i++) { | ||
if (this[i] instanceof Array && array[i] instanceof Array) { | ||
if (!this[i].equals(array[i])) return false; | ||
} | ||
else if (this[i] !== array[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
}; | ||
let eachExtensionOf = function(extension, block) { | ||
Object.keys(extension).forEach(block) | ||
}; | ||
let extend = function(proto, extension, methodName) { | ||
Object.defineProperty(proto, methodName, { value: extension[methodName] }); | ||
}; | ||
eachExtensionOf(Collection, function (methodName) { | ||
[Array.prototype, String.prototype].forEach(function (proto) { | ||
extend(proto, Collection, methodName); | ||
}); | ||
}); | ||
}); | ||
eachExtensionOf(ArrayExtensions, function (methodName) { | ||
extend(Array.prototype, ArrayExtensions, methodName); | ||
}); | ||
})(); |
{ | ||
"name": "oow", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Extension methods that should exist in JS core by default", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/ngarbezza/oow", |
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
3112
47