Comparing version 1.0.0 to 2.0.0
377
neon.js
@@ -1,219 +0,234 @@ | ||
/** | ||
* neon.js v1.0.0 http://github.com/azendal/neon | ||
* | ||
* Class DSL | ||
* This files provides a dsl for the following design patterns: | ||
* inheritance, interface, module. | ||
* | ||
* It also provides a dsl for class creation. | ||
* | ||
* The philosophy is that it should not try to emulate other languages, | ||
* and it preserves the javascript good parts, but with a nicer syntax to | ||
* create classes that ensure interfaces and include reusable functionality as modules. | ||
* | ||
* Author: Fernando Trasviña | ||
* | ||
* Usage: | ||
* Interface('Editable')({ | ||
* constructor : ['x'], | ||
* prototype : ['x'] | ||
* }); | ||
* | ||
* Module('Composition')({ | ||
* y : 5, | ||
* prototype : { | ||
* z : 3 | ||
* } | ||
* }); | ||
* | ||
* Module('Other')({ | ||
* a : 5, | ||
* prototype : { | ||
* b : 3 | ||
* } | ||
* }); | ||
* | ||
* Class('Overlay').inherits(Widget).ensures(Editable).includes(Composition, Other)({ | ||
* html : '<div></div>', | ||
* prototype : { | ||
* init : function (element){ | ||
* if(!element){ | ||
* element = document.createElement('div'); | ||
* element.innerHTML = 'hello'; | ||
* document.body.appendChild(element); | ||
* } | ||
* }, | ||
* b : 5 | ||
* } | ||
* }); | ||
*/ | ||
(function(global) { | ||
var Neon = {}; | ||
if (typeof global === "undefined") { | ||
global = window; | ||
} | ||
Neon.Interface = function Interface(nameOrNameSpace, name) { | ||
var nameSpace, interfaceName, factory; | ||
nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this; | ||
interfaceName = (nameOrNameSpace && name) ? name : | ||
(nameOrNameSpace) ? nameOrNameSpace : 'interface' + Math.random().toString(); | ||
factory = function(definition) { | ||
definition.isInterface = true; | ||
definition.name = interfaceName; | ||
nameSpace[interfaceName] = definition; | ||
return nameSpace[interfaceName]; | ||
}; | ||
return factory; | ||
global.Interface = function Interface(nameOrNameSpace, name) { | ||
var nameSpace, interfaceName, factory; | ||
nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this; | ||
interfaceName = (nameOrNameSpace && name) ? name : | ||
(nameOrNameSpace) ? nameOrNameSpace : 'interface' + Math.random().toString(); | ||
factory = function(definition) { | ||
definition.isInterface = true; | ||
definition.name = interfaceName; | ||
nameSpace[interfaceName] = definition; | ||
return nameSpace[interfaceName]; | ||
}; | ||
return factory; | ||
}; | ||
Neon.Module = function Module(nameOrNameSpace, name) { | ||
var nameSpace, moduleName, factory; | ||
nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this; | ||
moduleName = (nameOrNameSpace && name) ? name : | ||
(nameOrNameSpace) ? nameOrNameSpace : 'module' + Math.random().toString(); | ||
factory = function(definition) { | ||
definition.isModule = true; | ||
nameSpace[moduleName] = definition; | ||
return nameSpace[moduleName]; | ||
}; | ||
global.Module = function Module(nameOrNameSpace, name) { | ||
var nameSpace, moduleName, factory, newModule; | ||
nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this; | ||
moduleName = (nameOrNameSpace && name) ? name : | ||
(nameOrNameSpace) ? nameOrNameSpace : 'module' + Math.random().toString(); | ||
newModule = { | ||
moduleName : moduleName, | ||
prototype : {}, | ||
__includedModules : [], | ||
include : function(module) { | ||
var property; | ||
for (property in module) { | ||
if (module.hasOwnProperty(property) | ||
&& property !== 'prototype' | ||
&& property !== 'isModule' | ||
&& property !== '__includedModules' | ||
&& property !== 'include' | ||
&& property !== 'moduleName') { | ||
newModule[property] = module[property]; | ||
} | ||
} | ||
if (module.hasOwnProperty('prototype') && module.prototype) { | ||
for (property in module.prototype) { | ||
if (module.prototype.hasOwnProperty(property)) { | ||
newModule.prototype[property] = module.prototype[property]; | ||
} | ||
} | ||
} | ||
else { | ||
module.prototype = {}; | ||
} | ||
this.__includedModules.push(module); | ||
return this; | ||
} | ||
} | ||
factory = function(definition){ | ||
var property; | ||
newModule.isModule = true; | ||
for (property in definition) { | ||
if (definition.hasOwnProperty(property) | ||
&& property !== 'prototype' | ||
&& property !== 'isModule' | ||
&& property !== '__includedModules' | ||
&& property !== 'include' | ||
&& property !== 'moduleName') { | ||
newModule[property] = definition[property]; | ||
} | ||
} | ||
if (definition.hasOwnProperty('prototype') && definition.prototype) { | ||
for (property in definition.prototype) { | ||
if (definition.prototype.hasOwnProperty(property)) { | ||
newModule.prototype[property] = definition.prototype[property]; | ||
} | ||
} | ||
} | ||
nameSpace[moduleName] = newModule; | ||
return nameSpace[moduleName]; | ||
}; | ||
factory.includes = function () { | ||
for(var i = 0; i < arguments.length; i++){ | ||
newModule.include(arguments[i]); | ||
} | ||
return factory; | ||
}; | ||
return factory; | ||
}; | ||
Neon.Class = function Class(classNameOrNameSpace, className) { | ||
var nameSpace, newClass, classFactory; | ||
nameSpace = (classNameOrNameSpace && className) ? classNameOrNameSpace : this; | ||
className = (classNameOrNameSpace && className) ? className : | ||
(classNameOrNameSpace) ? classNameOrNameSpace : 'class' + Math.random().toString(); | ||
global.Class = function Class(classNameOrNameSpace, className) { | ||
var nameSpace, newClass, classFactory; | ||
nameSpace = (classNameOrNameSpace && className) ? classNameOrNameSpace : global; | ||
className = (classNameOrNameSpace && className) ? className : | ||
(classNameOrNameSpace) ? classNameOrNameSpace : 'class' + Math.random().toString(); | ||
newClass = function() { | ||
if (this.init) { | ||
this.init.apply(this, arguments); | ||
} | ||
}; | ||
newClass = function() { | ||
if (this.init) { | ||
this.init.apply(this, arguments); | ||
} | ||
}; | ||
newClass.__descendants = []; | ||
newClass.__implementedInterfaces = []; | ||
newClass.__includedModules = []; | ||
newClass.className = className; | ||
newClass.include = function(module) { | ||
var property; | ||
for (property in module) { | ||
if (module.hasOwnProperty(property) | ||
&& property != 'prototype' | ||
&& property != 'constructor' | ||
&& property != 'isModule' | ||
&& property != 'superClass') { | ||
newClass[property] = module[property]; | ||
} | ||
newClass.__descendants = []; | ||
newClass.__implementedInterfaces = []; | ||
newClass.__includedModules = []; | ||
newClass.className = className; | ||
newClass.include = function(module) { | ||
var property; | ||
for (property in module) { | ||
if (module.hasOwnProperty(property) | ||
&& property != 'prototype' | ||
&& property != 'constructor' | ||
&& property != 'isModule' | ||
&& property != 'superClass' | ||
&& property != 'include') { | ||
newClass[property] = module[property]; | ||
} | ||
} | ||
if (module.hasOwnProperty('prototype') && module.prototype) { | ||
for (property in module.prototype) { | ||
if (module.prototype.hasOwnProperty(property)) { | ||
newClass.prototype[property] = module.prototype[property]; | ||
} | ||
if (module.hasOwnProperty('prototype') && module.prototype) { | ||
for (property in module.prototype) { | ||
if (module.prototype.hasOwnProperty(property)) { | ||
newClass.prototype[property] = module.prototype[property]; | ||
} | ||
} else { | ||
module.prototype = {}; | ||
} | ||
} else { | ||
module.prototype = {}; | ||
} | ||
newClass.__includedModules.push(module); | ||
return this; | ||
}; | ||
newClass.__includedModules.push(module); | ||
return this; | ||
}; | ||
classFactory = function(classDefinition) { | ||
var i, il, j, jl, property, classPrototype = classDefinition.prototype; | ||
if (classPrototype) { | ||
for (property in classPrototype) { | ||
if (classPrototype.hasOwnProperty(property)) { | ||
newClass.prototype[property] = classPrototype[property]; | ||
} | ||
classFactory = function(classDefinition) { | ||
var i, il, j, jl, property, classPrototype = classDefinition.prototype; | ||
if (classPrototype) { | ||
for (property in classPrototype) { | ||
if (classPrototype.hasOwnProperty(property)) { | ||
newClass.prototype[property] = classPrototype[property]; | ||
} | ||
delete classDefinition.prototype; | ||
} | ||
for (property in classDefinition) { | ||
if (classDefinition.hasOwnProperty(property)) { | ||
newClass[property] = classDefinition[property]; | ||
delete classDefinition.prototype; | ||
} | ||
for (property in classDefinition) { | ||
if (classDefinition.hasOwnProperty(property)) { | ||
newClass[property] = classDefinition[property]; | ||
} | ||
} | ||
for (i = 0, il = newClass.__implementedInterfaces.length; i < il; i++) { | ||
for (j = 0, jl = newClass.__implementedInterfaces[i].constructor.length; j < jl; j++) { | ||
if (!newClass[ newClass.__implementedInterfaces[i].constructor[j] ]) { | ||
console.log('must implement static ' + newClass.__implementedInterfaces[i].name); | ||
break; | ||
} | ||
} | ||
for (i = 0, il = newClass.__implementedInterfaces.length; i < il; i++) { | ||
for (j = 0, jl = newClass.__implementedInterfaces[i].constructor.length; j < jl; j++) { | ||
if (!newClass[ newClass.__implementedInterfaces[i].constructor[j] ]) { | ||
console.log('must implement static ' + newClass.__implementedInterfaces[i].name); | ||
if (newClass.__implementedInterfaces[i].hasOwnProperty('prototype') | ||
&& newClass.__implementedInterfaces[i].prototype) { | ||
for (j = 0, jl = newClass.__implementedInterfaces[i].prototype.length; j < jl; j++) { | ||
if (!newClass.prototype[newClass.__implementedInterfaces[i].prototype[j]]) { | ||
console.log('must implement prototype ' + newClass.__implementedInterfaces[i].name); | ||
break; | ||
} | ||
} | ||
if (newClass.__implementedInterfaces[i].hasOwnProperty('prototype') | ||
&& newClass.__implementedInterfaces[i].prototype) { | ||
for (j = 0, jl = newClass.__implementedInterfaces[i].prototype.length; j < jl; j++) { | ||
if (!newClass.prototype[newClass.__implementedInterfaces[i].prototype[j]]) { | ||
console.log('must implement prototype ' + newClass.__implementedInterfaces[i].name); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
nameSpace[className] = newClass; | ||
return newClass; | ||
}; | ||
classFactory.inherits = function(superClass) { | ||
var i, inheritedClass; | ||
newClass.superClass = superClass; | ||
if (superClass.hasOwnProperty('__descendants')) { | ||
superClass.__descendants.push(newClass); | ||
try { | ||
if (Li && Li.ObjectSpy && Li.Spy) { | ||
newClass.__objectSpy = new Li.ObjectSpy(); | ||
newClass.__objectSpy.spy(newClass); | ||
newClass.__objectSpy.spy(newClass.prototype); | ||
} | ||
inheritedClass = function() { | ||
}; | ||
inheritedClass.prototype = superClass.prototype; | ||
newClass.prototype = new inheritedClass(); | ||
newClass.prototype.constructor = newClass; | ||
} catch (error) {} | ||
for (i in superClass) { | ||
if (superClass.hasOwnProperty(i) | ||
&& i != 'prototype' | ||
&& i !== 'className' | ||
&& i !== 'superClass' | ||
&& i !== 'include' | ||
&& i != '__descendants') { | ||
newClass[i] = superClass[i]; | ||
} | ||
} | ||
nameSpace[className] = newClass; | ||
return newClass; | ||
}; | ||
delete this.inherits; | ||
return this; | ||
classFactory.inherits = function(superClass) { | ||
var i, inheritedClass; | ||
newClass.superClass = superClass; | ||
if (superClass.hasOwnProperty('__descendants')) { | ||
superClass.__descendants.push(newClass); | ||
} | ||
inheritedClass = function() { | ||
}; | ||
inheritedClass.prototype = superClass.prototype; | ||
newClass.prototype = new inheritedClass(); | ||
newClass.prototype.constructor = newClass; | ||
classFactory.ensures = function(interfaces) { | ||
for (var i = 0; i < arguments.length; i++) { | ||
newClass.__implementedInterfaces.push(arguments[i]); | ||
for (i in superClass) { | ||
if (superClass.hasOwnProperty(i) | ||
&& i != 'prototype' | ||
&& i !== 'className' | ||
&& i !== 'superClass' | ||
&& i !== 'include' | ||
&& i != '__descendants') { | ||
newClass[i] = superClass[i]; | ||
} | ||
delete this.ensures; | ||
return classFactory; | ||
}; | ||
} | ||
classFactory.includes = function() { | ||
for (var i = 0; i < arguments.length; i++) { | ||
newClass.include(arguments[i]); | ||
} | ||
return classFactory; | ||
}; | ||
delete this.inherits; | ||
return this; | ||
}; | ||
classFactory.ensures = function(interfaces) { | ||
for (var i = 0; i < arguments.length; i++) { | ||
newClass.__implementedInterfaces.push(arguments[i]); | ||
} | ||
delete this.ensures; | ||
return classFactory; | ||
}; | ||
classFactory.includes = function() { | ||
for (var i = 0; i < arguments.length; i++) { | ||
newClass.include(arguments[i]); | ||
} | ||
return classFactory; | ||
}; | ||
if (typeof define === 'function') { | ||
define(function() { | ||
return Neon; | ||
}); | ||
} else { | ||
global.Class = Neon.Class; | ||
global.Module = Neon.Module; | ||
global.Interface = Neon.Interface; | ||
} | ||
return classFactory; | ||
}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : null))); | ||
}; |
{ | ||
"name" : "neon", | ||
"version" : "1.0.0", | ||
"main" : "./neon", | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/Siedrix/neon.git" | ||
}, | ||
"keywords": ["class", "prototype", "inheritance", "oop"] | ||
} | ||
"name" : "neon", | ||
"version" : "2.0.0", | ||
"description" : "Javascript DSL for Classical Inheritance", | ||
"main" : "./neon", | ||
"directories" : { | ||
"lib" : "./lib" | ||
}, | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/azendal/neon.git" | ||
}, | ||
"keywords": ["class", "prototype", "inheritance", "oop"] | ||
} |
if(typeof require !== 'undefined') { // We are in Node, need to require the file | ||
console.log("Requiring neon from file"); | ||
var Neon = require('../neon.js'); | ||
console.log(Neon); | ||
require('neon'); | ||
// In Coffeescript, the following construct is recommended | ||
// { Class, Module, Interface } = require('neon') | ||
var Class = Neon.Class; | ||
var Module = Neon.Module; | ||
var Interface = Neon.Interface; | ||
} | ||
@@ -14,0 +8,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
26991
14
622
77
2