Comparing version 0.1.5 to 0.2.0
171
index.js
@@ -1,172 +0,3 @@ | ||
"use strict"; | ||
// clone whatever comes | ||
var clone = function( input ){ | ||
var result, i, keys; | ||
switch ( typeof input ){ | ||
case "object": | ||
if ( Array.isArray( input ) ){ | ||
result = input.length > 0 ? input.slice( 0 ) : []; | ||
i = result.length; | ||
while( i-- ) result[ i ] = clone( result[ i ] ); | ||
} | ||
else if ( Buffer.isBuffer( input ) ){ | ||
result = new Buffer( input.length ); | ||
input.copy( result ); | ||
} | ||
else if ( input === null ){ | ||
return null; | ||
} | ||
else if ( input instanceof RegExp ){ | ||
return input; | ||
} | ||
else { | ||
result = {}; | ||
keys = Object.keys( input ); | ||
i = keys.length; | ||
while( i-- ) result[ keys[ i ] ] = clone( input[ keys[ i ] ] ); | ||
} | ||
break; | ||
default: | ||
return input; | ||
} | ||
return result; | ||
} | ||
// create an properties object | ||
var makeProperties = function( input ){ | ||
var properties = {} | ||
, keys = Object.keys( input ) | ||
, i = keys.length; | ||
while( i-- ) properties[ keys[ i ] ] = { value: input[ keys[ i ] ], writable: true, configurable: true, enumerable: true }; | ||
return properties; | ||
} | ||
module.exports = function( classDefinition ){ | ||
var proto = {} | ||
, classProperties = {} | ||
, getters = {} | ||
, setters = {} | ||
, staticProperties = {} | ||
, depth = 0 | ||
, parentClass, keys, i, id, get, set, setterKeys, getterKeys, staticKeys, o, parent; | ||
// collect inherited data | ||
if ( classDefinition.inherits ){ | ||
// ee class | ||
if ( classDefinition.inherits.$$__iridium__$$ ){ | ||
parentClass = classDefinition.inherits.$$__iridium__$$; | ||
proto = Object.create( parentClass.proto ); | ||
classProperties = clone( parentClass.properties ); | ||
setters = clone( parentClass.setters ); | ||
getters = clone( parentClass.getters ); | ||
staticProperties = clone( parentClass.staticProperties ); | ||
parent = parentClass.proto; | ||
depth = parentClass.depth; | ||
} | ||
else { | ||
// js class | ||
proto = Object.create( classDefinition.inherits.prototype ); | ||
} | ||
} | ||
// extract definition components | ||
keys = Object.keys( classDefinition ); | ||
i = keys.length; | ||
while( i-- ){ | ||
( function( id ){ | ||
get = classDefinition.__lookupGetter__( id ); | ||
set = classDefinition.__lookupSetter__( id ); | ||
// separte getters & setters | ||
if ( get || set ){ | ||
if ( get ) getters[ id ] = get; | ||
if ( set ) setters[ id ] = set; | ||
} | ||
// separate static properties | ||
else if ( id.indexOf( "static " ) === 0 ){ | ||
staticProperties[ id.substr( 7 ) ] = classDefinition[ id ]; | ||
} | ||
// the rest | ||
else if ( id !== "inherits" ){ | ||
switch ( typeof classDefinition[ id ] ){ | ||
case "function": | ||
proto[ id ] = classDefinition[ id ]; | ||
break; | ||
default: | ||
classProperties[ id ] = classDefinition[ id ]; | ||
break; | ||
} | ||
} | ||
}.bind( this ) )( keys[ i ] ); | ||
} | ||
// get keys of the props | ||
setterKeys = Object.keys( setters ); | ||
getterKeys = Object.keys( getters ); | ||
// this is the actual class contructor | ||
var ClassConstructor = function( options ){ | ||
// instantiate the class with its prototype and porperties | ||
// wee need to clone the properties so the properties | ||
// typeof object wont be shared between the instances | ||
var instance = Object.create( proto, makeProperties( clone( classProperties ) ) ) | ||
, k = setterKeys.length | ||
, m = getterKeys.length | ||
, contructorResult; | ||
// add setters and getters | ||
while( m-- ) instance.__defineGetter__( getterKeys[ m ], getters[ getterKeys[ m ] ] ); | ||
while( k-- ) instance.__defineSetter__( setterKeys[ k ], setters[ setterKeys[ k ] ] ); | ||
// add eventlisteners | ||
if ( options && options.on && typeof instance.on === "function" ) instance.on( options.on ); | ||
// parent | ||
if ( parent ) instance.parent = parent; | ||
// call the class contsructor | ||
if ( typeof instance.init === "function" ){ | ||
contructorResult = instance.init( options || {} ); | ||
if ( typeof contructorResult === "object" ) instance = contructorResult; | ||
} | ||
// thats it :) | ||
return instance; | ||
}; | ||
// add static properties | ||
staticKeys = Object.keys( staticProperties ); | ||
o = staticKeys.length; | ||
while( o-- ) ClassConstructor[ staticKeys[ o ] ] = staticProperties[ staticKeys[ o ] ]; | ||
// store class components | ||
Object.defineProperty( ClassConstructor, "$$__iridium__$$", { value: { | ||
proto: proto | ||
, properties: classProperties | ||
, setters: setters | ||
, getters: getters | ||
, staticProperties: staticProperties | ||
, depth: ++depth | ||
} } ); | ||
return ClassConstructor; | ||
} | ||
module.exports = require( "./lib/class" ); |
{ | ||
"name" : "ee-class" | ||
, "description" : "js class implementation" | ||
, "version" : "0.1.5" | ||
, "homepage" : "https://github.com/eventEmitter/ee-class" | ||
, "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" | ||
, "licence" : "MIT" | ||
, "os" : "linux" | ||
"name" : "ee-class" | ||
, "description" : "Javascript Class implementation for node.js" | ||
, "version" : "0.2.0" | ||
, "homepage" : "https://github.com/eventEmitter/ee-class" | ||
, "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" | ||
, "licence" : "mit" | ||
, "repository": { | ||
"url" : "https://github.com/eventEmitter/ee-class" | ||
"url" : "https://github.com/eventEmitter/ee-class.git" | ||
, "type" : "git" | ||
} | ||
, "engines": { | ||
"node" : ">=0.10.7" | ||
"node" : ">=v0.10.0" | ||
} | ||
, "bugs": { | ||
"url" : "https://github.com/eventEmitter/ee-class/issues" | ||
"url" : "https://github.com/eventEmitter/ee-class/issues" | ||
} | ||
@@ -21,2 +21,6 @@ , "dependencies": {} | ||
, "optionalDependencies": {} | ||
, "keywords" : [ "class" ] | ||
, "scripts": { | ||
"test" : "node test.js" | ||
} | ||
} |
# ee-class | ||
Javascript Class implementation for node.js | ||
## installation | ||
npm install ee-class | ||
## build status | ||
[![Build Status](https://travis-ci.org/eventEmitter/ee-class.png?branch=master)](https://travis-ci.org/eventEmitter/ee-class) | ||
## usage | ||
var Class = require( "ee-class" ); | ||
var Human = new Class( { | ||
var LifeForm = new Class( { | ||
isAlive: false | ||
name: "" | ||
, age: 29 | ||
, init: function( options ){ | ||
this.isAlive = !!options.isAlive; | ||
} | ||
} ); | ||
, init: function( options ){ | ||
this.name = options.name; | ||
} | ||
, sayHello: function( to ){ | ||
console.log( "Hi %s, my name is %s and i'm %i years old.", to, this.name, this.age ); | ||
} | ||
var Human = new Class( { | ||
inherits: LifeForm | ||
, name: "" | ||
// pay attention to give the function a name so you can reference it when you are calling the super function | ||
, init: function myInitFunction( options ){ | ||
myInitFunction.super( options ); | ||
this.name = options.name; | ||
} | ||
} ); | ||
@@ -25,14 +45,34 @@ | ||
var Boy = new Class( { | ||
inherits: Human | ||
, age: 12 | ||
inherits: Human | ||
, age: 0 | ||
// pay attention to give the function a name so you can reference it when you are calling the super function | ||
, init: function myInitFunction( options ){ | ||
myInitFunction.super( options ); | ||
if ( options.age > 18 ) throw new Error( "Too old to be a boy!" ) | ||
this.age = options.age; | ||
} | ||
, describe: function(){ | ||
console.log( "Hi, my name is %s, i'm %s years old and i'm " + ( this.isAlive ? "alive :)" : "dead :(" ), this.name, this.age ); | ||
} | ||
} ); | ||
var fabian = new Boy( { name: "Fabian" } ); | ||
fabian.sayHello(); // Hi my name is Fabian and i'm 12 years old. | ||
var fabian = new Boy( { | ||
name: "fabian" | ||
, age: 15 | ||
, isAlive: true | ||
} ); | ||
fabian.describe(); // Hi, my name is fabian, i'm 15 years old and i'm alive :) | ||
# Version History | ||
- 0.1.0: initial version | ||
- 0.1.3: fixed integration with eventemitter objects | ||
- 0.1.3: fixed integration with eventemitter objects | ||
- 0.2.0: Added proper implementation for calling super functions, deprecated the «parent» property |
27
test.js
var Class = require( "./" ); | ||
var Class = require( "./" ) | ||
, assert = require( "assert" ); | ||
@@ -12,7 +13,4 @@ | ||
, init: function( options ){ | ||
console.log( "LifeForm", this.age ); | ||
this.isAlive = !!options.isAlive; | ||
} | ||
, lifeform: function(){} | ||
} ); | ||
@@ -28,9 +26,6 @@ | ||
, init: function( options ){ | ||
console.log( "Human", this.age ); | ||
//this.super.init( options ); | ||
, init: function init( options ){ | ||
init.super( options ); | ||
this.name = options.name; | ||
} | ||
, human: function(){} | ||
} ); | ||
@@ -40,3 +35,2 @@ | ||
var Boy = new Class( { | ||
@@ -47,7 +41,5 @@ inherits: Human | ||
, init: function( options ){ | ||
//console.log( this, this.__proto__, this.__proto__.__proto__, this.__proto__.__proto__.__proto__, this.__proto__.__proto__.__proto__.__proto__ ); | ||
Super( options ); | ||
, init: function init( options ){ | ||
init.super( options ); | ||
this.age = options.age; | ||
console.log( "Boy", this.age ); | ||
} | ||
@@ -57,3 +49,6 @@ | ||
, describe: function(){ | ||
console.log( "Hi, my name is %s, i'm %s years old and i'm " + ( this.isAlive ? "alive :)" : "dead :(" ), this.name, this.age ); | ||
//console.log( "Hi, my name is %s, i'm %s years old and i'm " + ( this.isAlive ? "alive :)" : "dead :(" ), this.name, this.age ); | ||
assert.equal( this.age, 15, "The «age» property was not set!"); | ||
assert.equal( this.name, "fabian", "The «fabian» property was not set!"); | ||
assert.equal( this.isAlive, true, "The «alive» property was not set!"); | ||
} | ||
@@ -67,3 +62,3 @@ } ); | ||
name: "fabian" | ||
, age: 12 | ||
, age: 15 | ||
, isAlive: true | ||
@@ -70,0 +65,0 @@ } ); |
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
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
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
10741
8
203
77
0
1