generate-js
Advanced tools
Comparing version 2.0.4 to 2.1.0
360
generate.js
@@ -9,5 +9,5 @@ /** | ||
// Variables | ||
var Generator = {}, | ||
GeneratorMethods = {}, | ||
GeneratorProto = {}; | ||
var Creation = {}, | ||
Generation = {}, | ||
Generator = {}; | ||
@@ -17,10 +17,24 @@ // Helper Methods | ||
/** | ||
* Generator.toString method. | ||
* @return {String} A string representation of this generator. | ||
* Assert Error function. | ||
* @param {Boolean} condition Whether or not to throw error. | ||
* @param {String} message Error message. | ||
*/ | ||
function toString() { | ||
return '[' + (this.name || 'generator') + ' Generator]'; | ||
function assertError(condition, message) { | ||
if (!condition) { | ||
throw new Error(message); | ||
} | ||
} | ||
/** | ||
* Assert TypeError function. | ||
* @param {Boolean} condition Whether or not to throw error. | ||
* @param {String} message Error message. | ||
*/ | ||
function assertTypeError(test, type) { | ||
if (typeof test !== type) { | ||
throw new TypeError('Expected \'' + type + '\' but instead found \'' + typeof test +'\''); | ||
} | ||
} | ||
/** | ||
* Returns the name of function 'func'. | ||
@@ -107,126 +121,5 @@ * @param {Function} func Any function. | ||
/** | ||
* Generates a new generator that inherits from 'ParentGenerator'. | ||
* @param {Generator} ParentGenerator Generator to inherit from. | ||
* @param {Function} create Create method that gets called when creating a new instance of new generator. | ||
* @param {Function} init Inits any data stores needed by prototypal methods. | ||
* @return {Generator} New Generator that inherits from 'ParentGenerator'. | ||
*/ | ||
function GeneratorFunc(ParentGenerator, create) { | ||
ParentGenerator = Generator.isGenerator(ParentGenerator) ? ParentGenerator : Generator; | ||
var proto = Object.create(ParentGenerator.proto), | ||
properties = Object.create(ParentGenerator.proto.prototypeProperties), | ||
generator = Object.create(proto); | ||
create = typeof create === 'function' ? create : Generator.proto.__create; | ||
defineObjectProperties( | ||
properties, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
generator: generator | ||
} | ||
); | ||
defineObjectProperties( | ||
proto, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
proto: ParentGenerator.proto, | ||
prototypeProperties: properties, | ||
__parentGenerator: ParentGenerator, | ||
__create: create, | ||
} | ||
); | ||
defineObjectProperties( | ||
generator, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
name: getFunctionName(create), | ||
proto: proto | ||
} | ||
); | ||
return generator; | ||
} | ||
/** | ||
* [toGenerator description] | ||
* @param {Function} constructor A constructor function. | ||
* @return {Generator} A new generator who's create method is `constructor` and inherits from `constructor.prototype`. | ||
*/ | ||
function toGenerator(constructor) { | ||
var proto = Object.create(Generator.proto), | ||
properties = Object.create(constructor.prototype), | ||
generator = Object.create(proto); | ||
defineObjectProperties( | ||
properties, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
generator: generator | ||
} | ||
); | ||
defineObjectProperties( | ||
properties, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
Generator.proto.prototypeProperties | ||
); | ||
defineObjectProperties( | ||
proto, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
proto: Generator.proto, | ||
prototypeProperties: properties, | ||
__parentGenerator: Generator, | ||
__create: constructor, | ||
} | ||
); | ||
defineObjectProperties( | ||
generator, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
name: getFunctionName(constructor), | ||
proto: proto | ||
} | ||
); | ||
return generator; | ||
} | ||
// Generator Instance Inheritance | ||
// Creation Class | ||
defineObjectProperties( | ||
GeneratorMethods, | ||
Creation, | ||
{ | ||
@@ -247,2 +140,19 @@ configurable: false, | ||
return this; | ||
}, | ||
/** | ||
* returns the prototype of `this` Creation. | ||
* @return {Object} Prototype of `this` Creation. | ||
*/ | ||
getProto: function getProto() { | ||
return Object.getPrototypeOf(this); | ||
}, | ||
/** | ||
* returns the prototype of `this` super Creation. | ||
* @return {Object} Prototype of `this` super Creation. | ||
*/ | ||
getSuper: function getSuper() { | ||
return Object.getPrototypeOf(this.generator).proto; | ||
// return Object.getPrototypeOf(Object.getPrototypeOf(this)); | ||
} | ||
@@ -252,5 +162,5 @@ } | ||
// Generator Base Inheritance | ||
// Generation Class | ||
defineObjectProperties( | ||
GeneratorProto, | ||
Generation, | ||
{ | ||
@@ -262,3 +172,6 @@ configurable: false, | ||
{ | ||
prototypeProperties: GeneratorMethods, | ||
name: 'Generation', | ||
proto: Creation, | ||
/** | ||
@@ -270,43 +183,78 @@ * Creates a new instance of this Generator. | ||
var _ = this, | ||
args = Array.prototype.slice.call(arguments), | ||
newObj = Object.create(_.proto.prototypeProperties); | ||
newObj = Object.create(_.proto); | ||
_.__supercreate.apply(newObj, [_].concat(args)); | ||
_.__supercreate(newObj, arguments); | ||
return newObj; | ||
}, | ||
__supercreate: function __supercreate(generator) { | ||
__supercreate: function __supercreate(newObj, args) { | ||
var _ = this, | ||
args = Array.prototype.slice.call(arguments).slice(1), | ||
parentGenerator = generator.__parentGenerator, | ||
superGenerator = Object.getPrototypeOf(_), | ||
supercreateCalled = false; | ||
_.supercreate = function supercreate() { | ||
var args = Array.prototype.slice.call(arguments); | ||
newObj.supercreate = function supercreate() { | ||
supercreateCalled = true; | ||
if (Generator.isGenerator(parentGenerator)){ | ||
parentGenerator.__supercreate.apply(_, [parentGenerator].concat(args)); | ||
if (Generation.isGeneration(superGenerator)){ | ||
superGenerator.__supercreate(newObj, arguments); | ||
} | ||
}; | ||
generator.__create.apply(_, args); | ||
_.__create.apply(newObj, args); | ||
if (!supercreateCalled) { | ||
_.supercreate(); | ||
newObj.supercreate(); | ||
} | ||
delete _.supercreate; | ||
delete newObj.supercreate; | ||
}, | ||
__create: function () {}, | ||
/** | ||
* Returns a new Generator that inherits from this Generator. | ||
* @param {Function} create Create method that gets called when creating a new instance of new generator. | ||
* @param {Function} init Inits any data stores needed by prototypal methods. | ||
* @return {Generator} New generator that inherits from this Generator. | ||
* Generates a new generator that inherits from `this` generator. | ||
* @param {Generator} ParentGenerator Generator to inherit from. | ||
* @param {Function} create Create method that gets called when creating a new instance of new generator. | ||
* @return {Generator} New Generator that inherits from 'ParentGenerator'. | ||
*/ | ||
generate:function generate(create, init) { | ||
return GeneratorFunc(this, create, init); | ||
generate: function generate(create) { | ||
var _ = this; | ||
assertError(Generation.isGeneration(_) || _ === Generation, 'Cannot call method \'generate\' on non-Generations.'); | ||
assertTypeError(create, 'function'); | ||
var newGenerator = Object.create(_), | ||
newProto = Object.create(_.proto); | ||
defineObjectProperties( | ||
newProto, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
generator: newGenerator | ||
} | ||
); | ||
defineObjectProperties( | ||
newGenerator, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
name: getFunctionName(create), | ||
proto: newProto, | ||
__create: create | ||
} | ||
); | ||
return newGenerator; | ||
}, | ||
/** | ||
@@ -319,12 +267,5 @@ * Returns true if 'generator' was generated by this Generator. | ||
var _ = this; | ||
if (generator && typeof generator === 'object' && _ !== generator) { | ||
while (generator.__parentGenerator && typeof generator.__parentGenerator === 'object') { | ||
generator = generator.__parentGenerator; | ||
if (_ === generator) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
return _.isPrototypeOf(generator); | ||
}, | ||
/** | ||
@@ -337,8 +278,5 @@ * Returns true if 'object' was created by this Generator. | ||
var _ = this; | ||
return _.proto.isPrototypeOf(object); | ||
}, | ||
if (object && typeof object === 'object' && Generator.isGenerator(object.generator)) { | ||
return _ === object.generator || _.isGeneration(object.generator); | ||
} | ||
return false; | ||
}, | ||
/** | ||
@@ -351,6 +289,13 @@ * Defines shared properties for all objects created by this generator. | ||
definePrototype: function definePrototype(descriptor, properties) { | ||
defineObjectProperties(this.proto.prototypeProperties, descriptor, properties); | ||
defineObjectProperties(this.proto, descriptor, properties); | ||
return this; | ||
}, | ||
toString: toString | ||
/** | ||
* Generator.toString method. | ||
* @return {String} A string representation of this generator. | ||
*/ | ||
toString: function toString() { | ||
return '[' + (this.name || 'generation') + ' Generator]'; | ||
} | ||
} | ||
@@ -368,11 +313,78 @@ ); | ||
{ | ||
name: 'Generator', | ||
proto: GeneratorProto, | ||
generate: GeneratorProto.generate, | ||
isGenerator: GeneratorProto.isGeneration, | ||
toGenerator: toGenerator, | ||
toString: toString | ||
/** | ||
* Generates a new generator that inherits from `this` generator. | ||
* @param {Generator} ParentGenerator Generator to inherit from. | ||
* @param {Function} create Create method that gets called when creating a new instance of new generator. | ||
* @return {Generator} New Generator that inherits from 'ParentGenerator'. | ||
*/ | ||
generate: function generate (create) { | ||
return Generation.generate(create); | ||
}, | ||
/** | ||
* Returns true if 'generator' was generated by this Generator. | ||
* @param {Generator} generator A Generator. | ||
* @return {Boolean} true or false. | ||
*/ | ||
isGenerator: function isGenerator (generator) { | ||
return Generation.isGeneration(generator); | ||
}, | ||
/** | ||
* [toGenerator description] | ||
* @param {Function} constructor A constructor function. | ||
* @return {Generator} A new generator who's create method is `constructor` and inherits from `constructor.prototype`. | ||
*/ | ||
toGenerator: function toGenerator(constructor) { | ||
assertTypeError(constructor, 'function'); | ||
var newGenerator = Object.create(Generation), | ||
newProto = Object.create(constructor.prototype); | ||
defineObjectProperties( | ||
newProto, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
generator: newGenerator | ||
} | ||
); | ||
defineObjectProperties( | ||
newProto, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
Creation | ||
); | ||
defineObjectProperties( | ||
newGenerator, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
name: getFunctionName(constructor), | ||
proto: newProto, | ||
__create: constructor | ||
} | ||
); | ||
return newGenerator; | ||
} | ||
} | ||
); | ||
Object.freeze(Creation); | ||
Object.freeze(Generation); | ||
Object.freeze(Generator); | ||
// Exports | ||
@@ -379,0 +391,0 @@ if (typeof define === 'function' && define.amd) { |
{ | ||
"name": "generate-js", | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"description": "An easy to use prototypal inheritance model and generator.", | ||
@@ -5,0 +5,0 @@ "main": "generate.js", |
@@ -16,8 +16,10 @@ ## Table of Contents | ||
* [ Creation.defineProperties([descriptor,] properties) ](#creation-define-properties) | ||
* [ Creation.getProto() ](#creation-get-proto) | ||
* [ Creation.getSuper() ](#creation-get-super) | ||
<a name="generator"></a> | ||
Generator | ||
========= | ||
# Generator | ||
An easy to use prototypal inheritance model and generator. All generations inherit from Generator. | ||
An easy to use prototypal inheritance model and generator. All generations inherit from Generator. | ||
@@ -45,4 +47,4 @@ ### Install: | ||
this.name = name || 'no-name'; | ||
this.age = age || 0; | ||
this.sex = sex || 'unknown'; | ||
this.age = age || 0; | ||
this.sex = sex || 'unknown'; | ||
} | ||
@@ -74,3 +76,3 @@ ); | ||
var Generator = require('generate-js'), | ||
events = require('events'); | ||
events = require('events'); | ||
@@ -179,4 +181,4 @@ var EventEmitter = Generator.toGenerator(events.EventEmitter); | ||
jim.sayHello(); // prints out: 'Hello, my name is Jim. What is yours?' | ||
jim.sayBye(); // prints out: 'Goodbye.' | ||
jim.sayHello(); // prints out: 'Hello, my name is Jim. What is yours?' | ||
jim.sayBye(); // prints out: 'Goodbye.' | ||
@@ -218,5 +220,5 @@ ``` | ||
sarah.name // 'Sarah' | ||
sarah.age // 17 | ||
sarah.sex // 'female' | ||
sarah.name // 'Sarah' | ||
sarah.age // 17 | ||
sarah.sex // 'female' | ||
sarah.studentId // 'A0123456789' | ||
@@ -290,2 +292,16 @@ | ||
<a name="creation-get-proto"></a> | ||
## Creation.getProto() | ||
* *return*: `Object` Prototype of *this* Creation. | ||
Returns the prototype of *this* Creation. | ||
<a name="creation-get-super"></a> | ||
## Creation.getSuper() | ||
* *return*: `Object` super Prototype of *this* Creation. | ||
Returns the super prototype of *this* Creation. | ||
## Author: | ||
@@ -292,0 +308,0 @@ Michaelangelo Jong |
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
35321
6
684
328
1