Comparing version 0.1.0 to 0.1.1
/*! (C) WebReflection *//** @preserve https://github.com/WebReflection/redefine */ | ||
var _=this._=function(e,l,d){function m(c,r,b,d){if(!(b instanceof v))if(b instanceof w){var e=b.valueOf,h=f.call(b,n)?!!b[n]:!0,l=f.call(b,p)&&b[p],m=f.call(b,j)&&b[j],s;b[t]=function(){a[k]=e.call(s=this);a[n]=h;a[p]=l;a[j]=m;x(s,r,a);y(a);return s[r]}}else b=(u[k]=b,u);q(d||g.defaults||{},a);if(f.call(b,t)||f.call(b,z))delete a[j],delete a[k];q(b,a);x(c,r,a);y(a);delete u[k]}function v(c){q(c,this)}function w(c){this.valueOf=c}function g(c,a,b,d){if("string"==typeof a)m(c,a,b,d);else for(var e in a)f.call(a, | ||
e)&&m(c,e,a[e],b);return c}var n="configurable",p="enumerable",t="get",z="set",k="value",j="writable",x=e.defineProperty||d.defineProperty,f=e.hasOwnProperty||d.hasOwnProperty,A=e.create||e.inherit||d.create;d=[n,p,t,z,k,j];var y=l("o","delete o."+d.join(";delete o.")),a=A(null),u={},h,q;for(h=0;h<d.length;h++)d[h]=["if(h.call(a,'","'))b.","=a.",";"].join(d[h]);q=l("h","return function(a,b){"+d.join("")+"}")(f);g.as=function(c){return new v(c)};g.from=function(c,a,b){c=A("function"==typeof c?c.prototype: | ||
c);return a?g(c,a,b):c};g.later=function(a){return new w(a)};g.defaults={};e.redefine=g;return e}(_||this,Function,Object); | ||
var _=this._=function(e,m,d){function n(a,r,c,d){if(!(c instanceof v))if(c instanceof w){var e=c._,h=f.call(c,p)?!!c[p]:!0,m=f.call(c,q)&&c[q],n=f.call(c,k)&&c[k],s;c[t]=function(){b[j]=e.call(s=this);b[p]=h;b[q]=m;b[k]=n;x(s,r,b);y(b);return s[r]}}else c=(u[j]=c,u);l(d||g.defaults||{},b);l(c,b);if(f.call(c,t)||f.call(c,z))delete b[k],delete b[j];x(a,r,b);y(b);delete u[j]}function v(a){l(a,this)}function w(a){this._="function"==typeof a?a:l(a,this)||a[j]}function g(a,b,c,d){if("string"==typeof b)n(a, | ||
b,c,d);else for(var e in b)f.call(b,e)&&n(a,e,b[e],c);return a}var p="configurable",q="enumerable",t="get",z="set",j="value",k="writable",x=e.defineProperty||d.defineProperty,f=e.hasOwnProperty||d.hasOwnProperty,A=e.create||e.inherit||d.create;d=[p,q,t,z,j,k];var y=m("o","delete o."+d.join(";delete o.")),b=A(null),u={},h,l;for(h=0;h<d.length;h++)d[h]=["if(h.call(a,'","'))b.","=a.",";"].join(d[h]);l=m("h","return function(a,b){"+d.join("")+"}")(f);g.as=function(a){return new v(a)};g.from=function(a, | ||
b,c){a=A("function"==typeof a?a.prototype:a);return b?g(a,b,c):a};g.later=function(a){return new w(a)};g.defaults={};e.redefine=g;return e}(_||this,Function,Object); |
{ | ||
"name": "redefine", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A lightweight utility for an easier ES5 aware object properties definition introducing new, performance oriented, patterns.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/WebReflection/redefine", |
@@ -175,3 +175,3 @@ redefine.js | ||
So you are right guys, the way ES5 lets us implement amazing new patterns and possibilities is even hard to understand or imagine. This is why `redefine.js` comes with a pattern many other programming languages can only dream about: the efficient and performance oriented lazy inherited getter replaced with a direct property access! | ||
So you are right guys, the way ES5 lets us implement amazing new patterns and possibilities is even hard to understand or imagine. This is why `redefine.js` comes with a pattern many other programming languages can only dream about: the memory efficient and performance oriented **inherited getter replaced on demand with a direct property access**! (BOOM, I know your mind just blown!) | ||
@@ -261,4 +261,5 @@ // what you would do today in ES3 classes | ||
* `redefine(obj:Object, key:string, value:any[, defaults:Object]):Object`, returns the first argument and define a value straight forward using ES5 defaults unless specified differently. | ||
* `redefine(obj:Object, key:string, value:As[, defaults:Object]):Object`, returns the first argument and define a property `key` using `redefine.as({descriptor})` as value descriptor. `As` is an internal, private, class that overrides any default, if specified, or inherited behavior. | ||
* `redefine(obj:Object, key:string, value:Later[, defaults:Object]):Object`, returns the first argument and define a property `key` as lazily accessed and replaced as direct property that could be deleted at any time in order to reuse the inherited getter. `Later` is an internal, private, class that overrides any default, if specified, or inherited behavior. | ||
This signature has these two kind of overloads | ||
* `redefine(obj:Object, key:string, value:As[, defaults:Object]):Object`, returns the first argument and define a property `key` using `redefine.as({descriptor})` as value descriptor. `As` is an internal, private, class that overrides any default, if specified, or inherited behavior. | ||
* `redefine(obj:Object, key:string, value:Later[, defaults:Object]):Object`, returns the first argument and define a property `key` as lazily accessed and replaced as direct property that could be deleted at any time in order to reuse the inherited getter. `Later` is an internal, private, class that overrides any default, if specified, or inherited behavior. | ||
* `redefine(obj:Object, properties:Object[, defaults:Object])`, returns the first argument, it does exactly what other overloads do in this case looping through own properties in the specified `properties` Object. | ||
@@ -272,2 +273,4 @@ | ||
Here an example: | ||
var son = redefine.from( | ||
@@ -318,2 +321,15 @@ ClassName, {age: 123} | ||
It is possible to use a descriptor in order to overwrite the default configuration for this use case which is `{configurable:true, writable:false, enumerable:false}`. In this case, the `value` should be the callback able to return the lazily defined property. | ||
var setAsObjectLaterOn = redefine.later({ | ||
writable: true, //we want be able to change it later on | ||
enumerable: true, // shows up in for/in loops | ||
configurable: false,// once define there's no way to delete it | ||
value: function (){ | ||
return {}; | ||
} | ||
}); | ||
I see what you are thinking about: "*What? How can those properties have value and writable if we are defining a getter?*" Did I mention this method is called `later()` because is later that the property is define through the inherited getter ? :-) | ||
### Libraries Compatibility | ||
@@ -343,4 +359,5 @@ The `redefine.js` API is compatible with [Underscore](http://underscorejs.org) and [Lo-Dash](http://lodash.com) too as `_.redefine` utility. Bear in mind, **you don't need these libraries** at all, in fact `redefine.js` is completely dependencies free but in order to avoid global scope pollution the `redefine` function is defined into a global `_` object. If this is not present it is created, while if it's already there, is simply enriched. | ||
* node.js | ||
* Rhino | ||
Other server side engines such **Rhino** or **Ringo** should be supported too since these are compatible with ES5 and ES5.1. The best way to know if your device, browser, or server side JS engine is working is to [grab wru](https://github.com/WebReflection/wru) and run those tests :-) | ||
Other server side engines such **Ringo** should be supported too since these are compatible with ES5 and ES5.1. The best way to know if your device, browser, or server side JS engine is working is to [grab wru](https://github.com/WebReflection/wru) and run those tests :-) | ||
@@ -347,0 +364,0 @@ |
@@ -50,2 +50,3 @@ var _ = this._ = function(_, Function, Object) { | ||
assign(defaults || redefine.defaults || {}, nullObject); | ||
assign(descriptor, nullObject); | ||
if ( | ||
@@ -58,3 +59,2 @@ hasOwnProperty.call(descriptor, GET) || | ||
} | ||
assign(descriptor, nullObject); | ||
defineProperty(object, key, nullObject); | ||
@@ -95,11 +95,5 @@ clear(nullObject); | ||
var | ||
callback = descriptor.valueOf, | ||
// by default, configurable is true so it can be deleted | ||
// however, it is possible to make it not deletable once set | ||
// explicitly setting it as falsy | ||
callback = descriptor._, | ||
configurable = hasOwnProperty.call(descriptor, CONFIGURABLE) ? | ||
!!descriptor[CONFIGURABLE] : true | ||
, | ||
// defaults here might be dangerous | ||
// but might be useful too ... TODO: think about it | ||
!!descriptor[CONFIGURABLE] : true, | ||
enumerable = hasOwnProperty.call(descriptor, ENUMERABLE) && descriptor[ENUMERABLE], | ||
@@ -109,5 +103,2 @@ writable = hasOwnProperty.call(descriptor, WRITABLE) && descriptor[WRITABLE], | ||
; | ||
// a function per prototype definition could be | ||
// more expensive during prototype definition | ||
// but full of wins per each instance of that function | ||
descriptor[GET] = function get() { | ||
@@ -152,3 +143,6 @@ nullObject[VALUE] = callback.call(self = this); | ||
function Later(callback) { | ||
this.valueOf = callback; | ||
this._ = typeof callback == "function" ? | ||
callback : | ||
assign(callback, this) || callback[VALUE] | ||
; | ||
} | ||
@@ -155,0 +149,0 @@ |
//remove: | ||
// to test this via node, from redefine folder | ||
// npm install wru | ||
// wru test/redefine.js | ||
// ------------------------------------------------------- | ||
// sorry, this works locally and for Rhino only | ||
// cd ~/code/redefine | ||
// java -jar ~/code/wru/builder/jar/js.jar ~/code/redefine/test/redefine.js | ||
// Rhino ... doesn't work anyhow unless you have wru repo too | ||
var require = require || function (global, cache, base) { | ||
return function require(path) { | ||
if (cache[path]) return cache[path]; | ||
var | ||
filename = new java.io.File( | ||
base + java.io.File.separator + path | ||
).getCanonicalPath(), | ||
exports = cache[path] = {}, | ||
module = { | ||
id: path, | ||
parent: global, | ||
filename: filename, | ||
exports: exports | ||
} | ||
; | ||
Function( | ||
"global", "module", "exports", | ||
readFile(filename) | ||
).call(exports, | ||
global, module, exports | ||
); | ||
({}.hasOwnProperty).call( | ||
exports = module.exports, | ||
"loaded" | ||
) || (exports.loaded = true); | ||
return cache[path] = exports; | ||
}; | ||
}(this, {}, new java.io.File( | ||
environment["sun.java.command"].split(/ +/).pop().replace( | ||
new RegExp(java.io.File.separator + "[^" + java.io.File.separator + "]*$"), "") | ||
).getCanonicalPath() | ||
); | ||
// still Rhino ... | ||
if (typeof load == "function") { | ||
load( | ||
["..", "wru", "build", "wru.console.js"].join( | ||
java.io.File.separator | ||
) | ||
); | ||
} | ||
// Rhino and node.js | ||
var | ||
@@ -14,2 +68,3 @@ redefine = require('../src/redefine.js').redefine | ||
// the shared test | ||
wru.test([{ | ||
@@ -165,2 +220,31 @@ name: "inheritance", | ||
},{ | ||
name: "lazy patter configurability", | ||
test: function () { | ||
var | ||
oTest, | ||
oOther, | ||
source = redefine({}, { | ||
test: later(function () { | ||
return oTest = {}; | ||
}), | ||
other: later({ | ||
writable: true, //we want be able to change it later on | ||
enumerable: true, // shows in a forin | ||
configurable: false,// once defined cannot be deleted | ||
value: function () { | ||
return oOther = {}; | ||
} | ||
}) | ||
}), | ||
o = redefine.from(source) | ||
; | ||
wru.assert("re defined", o.test && o.other); | ||
wru.assert("method test called and variable assigned", oTest === o.test && !!oTest); | ||
wru.assert("method other called and variable assigned", oOther === o.other && !!oOther); | ||
oTest = Object.getOwnPropertyDescriptor(o, "test"); | ||
wru.assert("o.test has expected descriptor", !oTest.writable && !oTest.enumerable && oTest.configurable); | ||
oOther = Object.getOwnPropertyDescriptor(o, "other"); | ||
wru.assert("o.other has expected descriptor", oOther.writable && oOther.enumerable && !oOther.configurable); | ||
} | ||
},{ | ||
name: "the Camera use case", | ||
@@ -167,0 +251,0 @@ test: function (reality) {var now; |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
46402
8
504
398
1
1