| /*! (C) Andrea Giammarchi - MIT Style License */ | ||
| var Class=Class||function(e){"use strict";function w(e,t,n){for(var r,s=[],o=0;o<e.length;o++)r=e[o],c.call(r,i)&&s.push(r[i]),E(r,t,n,!1,!1);return s}function E(e,t,n,r,i){for(var s,o=typeof e!="function",u=m(e),a=0;a<u.length;a++)s=u[a],(o||!g.test(s))&&T(s,i)&&(c.call(t,s)&&k("duplicated: "+s),N(n,t,s,v(e,s),r))}function S(e,t,n,r){var i=x(t,r);d(e,t,{enumerable:!1,configurable:i,writable:i,value:n})}function x(e,t){return t?!/^[A-Z_]+$/.test(e):!0}function T(e,f){return e!==t&&e!==n&&e!==r&&e!==s&&e!==o&&e!==u&&e!==a&&(f||e!==i)}function N(e,t,n,r,i){var s=c.call(r,"value"),o,u;if(i){if(c.call(t,n))return}else s?(u=r.value,typeof u=="function"&&y.test(u)&&(r.value=L(e,n,u,i))):(A(e,n,r,"get"),A(e,n,r,"set"));o=x(n,i),r.enumerable=!1,r.configurable=o,s&&(r.writable=o),d(t,n,r)}function C(e,t){for(var n,r,i=0;i<e.length;i++){n=e[i];for(r in n)c.call(n,r)&&!c.call(t,r)&&k(r+" is not implemented")}}function k(e){try{console.warn(e)}catch(t){}}function L(e,t,n,r){return function(){c.call(this,u)||S(this,u,null,r);var i=this[u],s=this[u]=e[t],o=n.apply(this,arguments);return this[u]=i,o}}function A(e,t,n,r,i){c.call(n,r)&&y.test(n[r])&&(n[r]=L(v(e,t),r,n[r],i))}var t="constructor",n="extends",r="implements",i="init",s="prototype",o="static",u="super",a="with",f=["hasOwnProperty","isPrototypeOf","propertyIsEnumerable","toLocaleString","toString","valueOf"],l=!{valueOf:0}[f[2]](f[5]),c=e[f[0]],h=e.create||function(e){var t=this instanceof h;return h[s]=t?p:e,t?this:new h},p=h[s],d=e.defineProperty,v=e.getOwnPropertyDescriptor||function(e,t){return{value:e[t]}},m=e.getOwnPropertyNames||function(e){var t=[],n,r;for(r in e)c.call(e,r)&&t.push(r);if(l)for(n=0;n<f.length;n++)r=f[n],c.call(e,r)&&t.push(r);return t},g=new RegExp("^(?:"+m(function(){}).join("|")+")$"),y=/\bsuper\b/.test(function(){this["super"]()})?/\bsuper\b/:/.*/;try{d({},"{}",{})}catch(b){d=function(e,t,n){return e[t]=n.value,e}}return function(e){var i=c.call(e,t),u=c.call(e,n),f=u&&e[n],l=u&&typeof f=="function",p=l?f[s]:f,d=i?e[t]:l?function(){return f.apply(this,arguments)}:function(){},v=u&&i&&y.test(d),m=u?h(p):d[s],g,b;return v&&(d=L(p,t,d,!1)),c.call(e,a)&&(g=w([].concat(e[a]),m,p),b=g.length,b&&(d=function(e){return function(){var t=0;while(t<b)g[t++].call(this);return e.apply(this,arguments)}}(d),d[s]=m)),c.call(e,o)&&E(e[o],d,p,!0,!0),u&&(f!==p&&E(f,d,p,!0,!0),d[s]=m),m[t]!==d&&S(m,t,d,!1),E(e,m,p,!1,!0),c.call(e,r)&&C([].concat(e[r]),m),d}}(Object); | ||
| var Class=Class||function(e){"use strict";function x(e,t,n){for(var r,s=[],o=0;o<e.length;o++)r=O(e[o]),h.call(r,i)&&s.push(r[i]),N(r,t,n,!1,!1);return s}function T(e){A(e,!1)&&!h.call(this,e)&&v(this,e,m(this.init.prototype,e))}function N(e,t,n,r,i){for(var s,o=typeof e!="function",u=g(e),a=0;a<u.length;a++)s=u[a],(o||!w.test(s))&&A(s,i)&&(h.call(t,s)&&D("duplicated: "+s),M(n,t,s,m(e,s),r))}function C(e,t){var n=function(){};return e&&""+t!=""+n?function(){return t.apply(this,arguments)}:n}function k(e,t,n,r){var i=L(t,r);v(e,t,{enumerable:!1,configurable:i,writable:i,value:n})}function L(e,t){return t?!/^[A-Z_]+$/.test(e):!0}function A(e,f){return e!==t&&e!==n&&e!==r&&e!==s&&e!==o&&e!==u&&e!==a&&(f||e!==i)}function O(t){if(typeof t=="object")return t;t.length&&D((t.name||"Class")+" should not expect arguments");for(var n={init:t},r=t.prototype;r&&r!==e.prototype;r=b(r))g(r).forEach(T,n);return n}function M(e,t,n,r,i){var s=h.call(r,"value"),o,u;if(i){if(h.call(t,n))return}else s?(u=r.value,typeof u=="function"&&E.test(u)&&(r.value=P(e,n,u,i))):(H(e,n,r,"get"),H(e,n,r,"set"));o=L(n,i),r.enumerable=!1,r.configurable=o,s&&(r.writable=o),v(t,n,r)}function _(e,t){for(var n,r,i=0;i<e.length;i++){n=e[i];for(r in n)h.call(n,r)&&!h.call(t,r)&&D(r+" is not implemented")}}function D(e){try{console.warn(e)}catch(t){}}function P(e,t,n,r){return function(){h.call(this,u)||k(this,u,null,r);var i=this[u],s=this[u]=e[t],o=n.apply(this,arguments);return this[u]=i,o}}function H(e,t,n,r,i){h.call(n,r)&&E.test(n[r])&&(n[r]=P(m(e,t),r,n[r],i))}var t="constructor",n="extends",r="implements",i="init",s="prototype",o="static",u="super",a="with",f="__proto__",l=["hasOwnProperty","isPrototypeOf","propertyIsEnumerable","toLocaleString","toString","valueOf"],c=!{valueOf:0}[l[2]](l[5]),h=e[l[0]],p=e.create||function(e){var t=this instanceof p;return p[s]=t?d:e,t?this:new p},d=p[s],v=e.defineProperty,m=e.getOwnPropertyDescriptor||function(e,t){return{value:e[t]}},g=e.getOwnPropertyNames||function(e){var t=[],n,r;for(r in e)h.call(e,r)&&t.push(r);if(c)for(n=0;n<l.length;n++)r=l[n],h.call(e,r)&&t.push(r);return t},y=e.getPrototypeOf,b=y||function(e){return e[f]||null},w=new RegExp("^(?:"+g(function(){}).join("|")+")$"),E=/\bsuper\b/.test(function(){this["super"]()})?/\bsuper\b/:/.*/;try{v({},"{}",{})}catch(S){v=function(e,t,n){return e[t]=n.value,e}}return function(e){var i=h.call(e,t),u=h.call(e,n),l=u&&e[n],c=u&&typeof l=="function",d=c?l[s]:l,v=i?e[t]:C(c,l),m=u&&i&&E.test(v),g=u?p(d):v[s],b,w;return m&&(v=P(d,t,v,!1)),h.call(e,a)&&(b=x([].concat(e[a]),g,d),w=b.length,w&&(v=function(e){return function(){var t=0;while(t<w)b[t++].call(this);return e.apply(this,arguments)}}(v),v[s]=g)),h.call(e,o)&&N(e[o],v,d,!0,!0),u&&(l!==d&&N(l,v,d,!0,!0),v[s]=g),g[t]!==v&&k(g,t,v,!1),N(e,g,d,!1,!0),h.call(e,r)&&_([].concat(e[r]),g),u&&!y&&k(g,f,d,!1),v}}(Object); |
@@ -39,2 +39,6 @@ /*! | ||
| // infamous property used as fallback | ||
| // for IE8 and lower only | ||
| PROTO = '__proto__', | ||
| // used to copy non enumerable properties on IE | ||
@@ -97,2 +101,10 @@ nonEnumerables = [ | ||
| // needed to verify the existence | ||
| getPrototypeOf = Object.getPrototypeOf, | ||
| // needed to allow Classes as traits | ||
| gPO = getPrototypeOf || function (o) { | ||
| return o[PROTO] || null; | ||
| }, | ||
| // used to avoid setting `arguments` and other function properties | ||
@@ -134,3 +146,3 @@ // when public static are copied over | ||
| ) { | ||
| source = mixins[i]; | ||
| source = transformMixin(mixins[i]); | ||
| if (hOP.call(source, INIT)) { | ||
@@ -144,2 +156,10 @@ init.push(source[INIT]); | ||
| // used to copy properties from a class used as trait | ||
| function assignFromPrototype(key) { | ||
| /*jshint validthis: true */ | ||
| if (isNotASpecialKey(key, false) && !hOP.call(this, key)) { | ||
| defineProperty(this, key, gOPD(this.init.prototype, key)); | ||
| } | ||
| } | ||
| // configure source own properties in the target | ||
@@ -166,2 +186,14 @@ function copyOwn(source, target, inherits, publicStatic, allowInit) { | ||
| // return the right constructor analyzing the parent. | ||
| // if the parent is empty there is no need to call it. | ||
| function createConstructor(hasParentPrototype, parent) { | ||
| var Class = function Class() {}; | ||
| return hasParentPrototype && ('' + parent) !== ('' + Class) ? | ||
| function Class() { | ||
| return parent.apply(this, arguments); | ||
| } : | ||
| Class | ||
| ; | ||
| } | ||
| // common defineProperty wrapper | ||
@@ -199,2 +231,20 @@ function define(target, key, value, publicStatic) { | ||
| // will eventually convert classes or constructors | ||
| // into trait objects, before assigning them as such | ||
| function transformMixin(trait) { | ||
| if (typeof trait === 'object') return trait; | ||
| if (trait.length) { | ||
| warn((trait.name || 'Class') + ' should not expect arguments'); | ||
| } | ||
| for (var | ||
| object = {init: trait}, | ||
| proto = trait.prototype; | ||
| proto && proto !== Object.prototype; | ||
| proto = gPO(proto) | ||
| ) { | ||
| gOPN(proto).forEach(assignFromPrototype, object); | ||
| } | ||
| return object; | ||
| } | ||
| // set a property via defineProperty using a common descriptor | ||
@@ -299,9 +349,4 @@ // only if properties where not defined yet. | ||
| constructor = hasConstructor ? | ||
| description[CONSTRUCTOR] : ( | ||
| hasParentPrototype ? | ||
| function Class() { | ||
| return parent.apply(this, arguments); | ||
| } : | ||
| function Class() {} | ||
| ), | ||
| description[CONSTRUCTOR] : | ||
| createConstructor(hasParentPrototype, parent), | ||
| hasSuper = hasParent && hasConstructor && superRegExp.test(constructor), | ||
@@ -350,2 +395,5 @@ prototype = hasParent ? create(inherits) : constructor[PROTOTYPE], | ||
| } | ||
| if (hasParent && !getPrototypeOf) { | ||
| define(prototype, PROTO, inherits, false); | ||
| } | ||
| return constructor; | ||
@@ -352,0 +400,0 @@ }; |
+2
-0
@@ -199,3 +199,5 @@ ### constructor | ||
| Since version `1.0.0`, in case a trait is a class, `init` will be represented by the `constructor`, and properties will be borrowed from its `prototype`. | ||
| ### static | ||
@@ -202,0 +204,0 @@ Every class can have one or more static definitions. |
+5
-2
| { | ||
| "version": "0.9.0", | ||
| "version": "1.0.0", | ||
| "license": "MIT", | ||
@@ -11,2 +11,3 @@ "name": "es-class", | ||
| "ES6", | ||
| "ES2015", | ||
| "ES7", | ||
@@ -19,3 +20,5 @@ "KISS", | ||
| "simple", | ||
| "lightweight" | ||
| "lightweight", | ||
| "mixin", | ||
| "trait" | ||
| ], | ||
@@ -22,0 +25,0 @@ "author": { |
+84
-1
| es-class | ||
| ======== | ||
| [](http://travis-ci.org/WebReflection/es-class) | ||
| [](http://travis-ci.org/WebReflection/es-class) | ||
@@ -53,2 +53,4 @@ | ||
| #### Features Example | ||
| This is an example of what's possible: | ||
@@ -91,4 +93,85 @@ ```js | ||
| ### ES6 Ready | ||
| Using [6to5](http://6to5.org/) it is possible to make your code directly compatible down to ES5 or even ES3 without loosing the ability to debug in every platform without needing source-map. What you see is basically what you get. | ||
| ```js | ||
| // how you would write in native ES6 | ||
| class B extends A { | ||
| method(x, y) { | ||
| return super.method(x, y); | ||
| } | ||
| get value() { | ||
| return super.value; | ||
| } | ||
| set value(value) { | ||
| super.value = value; | ||
| } | ||
| } | ||
| // how you would write in es-class | ||
| var B = Class({ extends: A, | ||
| method(x, y) { | ||
| return this.super(x, y); | ||
| }, | ||
| get value() { | ||
| return this.super(); | ||
| }, | ||
| set value(value) { | ||
| return this.super(value); | ||
| } | ||
| }); | ||
| ``` | ||
| A simple call to `6to5 --whitelist=es6.arrowFunctions,es6.properties.shorthand f.js` and the output will be way cleaner than any automation produced by the same transpiler. | ||
| Your output will be more **readable** and also probably faster at execution time. | ||
| ### ES7 Ready | ||
| Looking at the future, grouped static properties and lightweight traits are also in, giving the ability to compose classes through eventual traits initialization without being on the way after class definition. | ||
| ```js | ||
| var wheels = { | ||
| init: function () { | ||
| if (this instanceof Car) { | ||
| this.frontWheels = 2; | ||
| this.rearWheels = 2; | ||
| } else { | ||
| this.frontWheels = 1; | ||
| this.rearWheels = | ||
| this instanceof Tricycle ? 2 : 1; | ||
| } | ||
| }, | ||
| turn: function (where) { | ||
| console.log('turning ' + where); | ||
| } | ||
| }; | ||
| var engine = { | ||
| hp: 120 | ||
| }; | ||
| var Car = Class({ | ||
| with: [ | ||
| wheels, | ||
| engine | ||
| ] | ||
| }); | ||
| var c = new Car; | ||
| c.turn('left'); // trning left | ||
| c.hp; // 80 | ||
| ``` | ||
| ### Which File ? | ||
| * [as global](build/es-class.js) to include inline or before any other file that uses classes | ||
| * [require/CommonJS](build/es-class.npm.js) to include it via browserify or node.js | ||
| * [AMD module](build/es-class.amd.js) if you want to load it via AMD | ||
| You can also simply `npm install -g es-class` and use `var Class = require('es-class');` whenever you need it. | ||
| ### License | ||
| [MIT Style License](LICENSE.txt) |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
31071
15.33%359
13.25%1
-50%175
90.22%