generate-js
Advanced tools
Comparing version 2.1.6 to 3.0.0
349
generate.js
@@ -7,10 +7,2 @@ /** | ||
(function GeneratorScope() { | ||
// Variables | ||
var Creation = {}, | ||
Generation = {}, | ||
Generator = {}; | ||
// Helper Methods | ||
/** | ||
@@ -35,3 +27,4 @@ * Assert Error function. | ||
throw new TypeError('Expected \'' + type + | ||
'\' but instead found \'' + typeof test + '\''); | ||
'\' but instead found \'' + | ||
typeof test + '\''); | ||
} | ||
@@ -125,180 +118,147 @@ } | ||
// Creation Class | ||
defineObjectProperties( | ||
Creation, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, { | ||
/** | ||
* Defines properties on this object. | ||
* @param {Object} descriptor Optional object descriptor that will be applied to all attaching properties. | ||
* @param {Object} properties An object who's properties will be attached to this object. | ||
* @return {Object} This object. | ||
*/ | ||
defineProperties: function defineProperties(descriptor, | ||
properties) { | ||
defineObjectProperties(this, descriptor, | ||
properties); | ||
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)); | ||
} | ||
} | ||
); | ||
var Creation = { | ||
/** | ||
* Defines properties on this object. | ||
* @param {Object} descriptor Optional object descriptor that will be applied to all attaching properties. | ||
* @param {Object} properties An object who's properties will be attached to this object. | ||
* @return {Object} This object. | ||
*/ | ||
defineProperties: function defineProperties(descriptor, | ||
properties) { | ||
defineObjectProperties(this, descriptor, | ||
properties); | ||
return this; | ||
}, | ||
// Generation Class | ||
defineObjectProperties( | ||
Generation, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, { | ||
name: 'Generation', | ||
/** | ||
* returns the prototype of `this` Creation. | ||
* @return {Object} Prototype of `this` Creation. | ||
*/ | ||
getProto: function getProto() { | ||
return Object.getPrototypeOf(this); | ||
}, | ||
proto: Creation, | ||
/** | ||
* returns the prototype of `this` super Creation. | ||
* @return {Object} Prototype of `this` super Creation. | ||
*/ | ||
getSuper: function getSuper() { | ||
return Object.getPrototypeOf(this.constructor.prototype); | ||
} | ||
}; | ||
/** | ||
* Creates a new instance of this Generator. | ||
* @return {Generator} Instance of this Generator. | ||
*/ | ||
create: function create() { | ||
var _ = this, | ||
newObj = Object.create(_.proto); | ||
var Generation = { | ||
/** | ||
* Returns true if 'generator' was generated by this Generator. | ||
* @param {Generator} generator A Generator. | ||
* @return {Boolean} true or false. | ||
*/ | ||
isGeneration: function isGeneration(generator) { | ||
assertTypeError(generator, 'function'); | ||
_.__supercreate(newObj, arguments); | ||
var _ = this; | ||
return newObj; | ||
}, | ||
return _.prototype.isPrototypeOf(generator.prototype); | ||
}, | ||
__supercreate: function __supercreate(newObj, args) { | ||
var _ = this, | ||
superGenerator = Object.getPrototypeOf(_), | ||
supercreateCalled = false; | ||
/** | ||
* Returns true if 'object' was created by this Generator. | ||
* @param {Object} object An Object. | ||
* @return {Boolean} true or false. | ||
*/ | ||
isCreation: function isCreation(object) { | ||
var _ = this; | ||
return object instanceof _; | ||
}, | ||
/** | ||
* 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(construct) { | ||
assertTypeError(construct, 'function'); | ||
newObj.supercreate = function supercreate() { | ||
var _ = this; | ||
supercreateCalled = true; | ||
defineObjectProperties( | ||
construct, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, { | ||
prototype: Object.create(_.prototype) | ||
} | ||
); | ||
if (Generation.isGeneration(superGenerator)) { | ||
superGenerator.__supercreate(newObj, | ||
arguments); | ||
} | ||
}; | ||
defineObjectProperties( | ||
construct, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
Generation | ||
); | ||
_.__create.apply(newObj, args); | ||
if (!supercreateCalled) { | ||
newObj.supercreate(); | ||
defineObjectProperties( | ||
construct.prototype, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, { | ||
constructor: construct, | ||
generator: construct, | ||
} | ||
); | ||
delete newObj.supercreate; | ||
}, | ||
return construct; | ||
}, | ||
__create: function () {}, | ||
/** | ||
* Defines shared properties for all objects created by this generator. | ||
* @param {Object} descriptor Optional object descriptor that will be applied to all attaching properties. | ||
* @param {Object} properties An object who's properties will be attached to this generator's prototype. | ||
* @return {Generator} This generator. | ||
*/ | ||
definePrototype: function definePrototype(descriptor, | ||
properties) { | ||
defineObjectProperties(this.prototype, | ||
descriptor, | ||
properties); | ||
return this; | ||
} | ||
}; | ||
/** | ||
* 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) { | ||
var _ = this; | ||
function Generator() {} | ||
assertError(Generation.isGeneration(_) || _ === | ||
Generation, | ||
'Cannot call method \'generate\' on non-Generations.' | ||
); | ||
assertTypeError(create, 'function'); | ||
defineObjectProperties( | ||
Generator, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, { | ||
prototype: Generator.prototype | ||
} | ||
); | ||
var newGenerator = Object.create(_), | ||
newProto = Object.create(_.proto); | ||
defineObjectProperties( | ||
Generator.prototype, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
Creation | ||
); | ||
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; | ||
}, | ||
/** | ||
* Returns true if 'generator' was generated by this Generator. | ||
* @param {Generator} generator A Generator. | ||
* @return {Boolean} true or false. | ||
*/ | ||
isGeneration: function isGeneration(generator) { | ||
var _ = this; | ||
return _.isPrototypeOf(generator); | ||
}, | ||
/** | ||
* Returns true if 'object' was created by this Generator. | ||
* @param {Object} object An Object. | ||
* @return {Boolean} true or false. | ||
*/ | ||
isCreation: function isCreation(object) { | ||
var _ = this; | ||
return _.proto.isPrototypeOf(object); | ||
}, | ||
/** | ||
* Defines shared properties for all objects created by this generator. | ||
* @param {Object} descriptor Optional object descriptor that will be applied to all attaching properties. | ||
* @param {Object} properties An object who's properties will be attached to this generator's prototype. | ||
* @return {Generator} This generator. | ||
*/ | ||
definePrototype: function definePrototype(descriptor, | ||
properties) { | ||
defineObjectProperties(this.proto, descriptor, | ||
properties); | ||
return this; | ||
}, | ||
/** | ||
* Generator.toString method. | ||
* @return {String} A string representation of this generator. | ||
*/ | ||
toString: function toString() { | ||
return '[' + (this.name || 'generation') + | ||
' Generator]'; | ||
} | ||
} | ||
defineObjectProperties( | ||
Generator, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
Generation | ||
); | ||
// Generator Class Methods | ||
defineObjectProperties( | ||
@@ -311,12 +271,2 @@ 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) { | ||
return Generation.generate(create); | ||
}, | ||
/** | ||
* Returns true if 'generator' was generated by this Generator. | ||
@@ -327,19 +277,17 @@ * @param {Generator} generator A Generator. | ||
isGenerator: function isGenerator(generator) { | ||
return Generation.isGeneration(generator); | ||
return this.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`. | ||
* 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'. | ||
*/ | ||
toGenerator: function toGenerator(constructor) { | ||
toGenerator: function toGenerator(extendFrom, create) { | ||
assertTypeError(extendFrom, 'function'); | ||
assertTypeError(create, 'function'); | ||
assertTypeError(constructor, 'function'); | ||
var newGenerator = Object.create(Generation), | ||
newProto = Object.create(constructor.prototype); | ||
defineObjectProperties( | ||
newProto, { | ||
create, { | ||
configurable: false, | ||
@@ -349,3 +297,3 @@ enumerable: false, | ||
}, { | ||
generator: newGenerator | ||
prototype: Object.create(extendFrom.prototype), | ||
} | ||
@@ -355,3 +303,3 @@ ); | ||
defineObjectProperties( | ||
newProto, { | ||
create, { | ||
configurable: false, | ||
@@ -361,7 +309,7 @@ enumerable: false, | ||
}, | ||
Creation | ||
Generation | ||
); | ||
defineObjectProperties( | ||
newGenerator, { | ||
create.prototype, { | ||
configurable: false, | ||
@@ -371,9 +319,17 @@ enumerable: false, | ||
}, { | ||
name: getFunctionName(constructor), | ||
proto: newProto, | ||
__create: constructor | ||
constructor: construct, | ||
generator: construct, | ||
} | ||
); | ||
return newGenerator; | ||
defineObjectProperties( | ||
create.prototype, { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
Creation | ||
); | ||
return create; | ||
} | ||
@@ -383,5 +339,4 @@ } | ||
Object.freeze(Creation); | ||
Object.freeze(Generation); | ||
Object.freeze(Generator); | ||
Object.freeze(Generator.prototype); | ||
@@ -388,0 +343,0 @@ // Exports |
{ | ||
"name": "generate-js", | ||
"version": "2.1.6", | ||
"version": "3.0.0", | ||
"description": "An easy to use prototypal inheritance model and generator.", | ||
@@ -5,0 +5,0 @@ "main": "generate.js", |
@@ -1,4 +0,4 @@ | ||
# Release v2.1.6 | ||
# Release v3.0.0 | ||
# [v3.0.0](new-api.md) has *BREAKING* changes and is not compatible for inheirtance with *ANY* v2.\*.\* Generators. | ||
# v3.0.0 has *BREAKING* changes and is not compatible for inheirtance with *ANY* v2.\*.\* Generators. | ||
@@ -10,8 +10,6 @@ ## Table of Contents | ||
* [ Generator.isGenerator(test) ](#is-generator) | ||
* [ Generator.toGenerator(constructor) ](#to-generator) | ||
* [ Generator.toGenerator(constructor, create) ](#to-generator) | ||
* [ Class: Generation ](#class-generation) | ||
* [ Generation.name ](#generation-name) | ||
* [ Generation.proto ](#generation-proto) | ||
* [ new Generation() ](#generation-create) | ||
* [ Generation.definePrototype([descriptor,] properties) ](#generation-define-prototype) | ||
* [ Generation.create() ](#generation-create) | ||
* [ Generation.generate(create) ](#generation-generate) | ||
@@ -39,4 +37,4 @@ * [ Generation.isCreation(test) ](#generation-is-creation) | ||
* *create* `Function` Create method that gets called when creating a new object that inherits from [Generation.proto](#generation-proto). | ||
* *return*: `Generation` A new [Generation](#class-generation) that inherits from [Generation](#class-generation). | ||
* *create* `Function` Constructor that with inherit form [Generation](#class-generation). | ||
* *return*: `Constructor` The inputed `create` function. | ||
@@ -68,8 +66,9 @@ Returns a new [Generation](#class-generation) that inherits from [Generation](#class-generation). | ||
<a name="to-generator"></a> | ||
## Generator.toGenerator(constructor) | ||
## Generator.toGenerator(constructor, create) | ||
* *constructor* `Function` An constructor to be generatorized. | ||
* *return*: `Generation` A new [Generation](#class-generation) that who's create method is equivalent to calling `new constructor();`. | ||
* *create* `Function` Constructor that with inherit form [Generation](#class-generation) and *constructor*. | ||
* *return*: `Constructor` The inputed `create` function. | ||
Returns a new [Generation](#class-generation) that is equivalent to *constructor*. | ||
Returns a new [Generation](#class-generation) that inherits from [Generation](#class-generation) and *constructor*. | ||
@@ -84,5 +83,7 @@ *NOTE*: Some native constructors can *NOT* be generatorized. | ||
var EventEmitter = Generator.toGenerator(events.EventEmitter); | ||
var EventEmitter = Generator.toGenerator(events.EventEmitter, function EventEmitter() { | ||
events.EventEmitter.call(this); //call the super constructor | ||
}); | ||
// EventEmitter.create() same as new events.EventEmitter(); | ||
// new EventEmitter() same as new events.EventEmitter(); | ||
@@ -92,3 +93,7 @@ // new generators can inherit all the abilities of EventEmitter like so. | ||
/* create method */ | ||
function MyNewGenerator() {} | ||
function MyNewGenerator() { | ||
EventEmitter.call(this); //call the super constructor | ||
// your code | ||
} | ||
); | ||
@@ -103,14 +108,22 @@ | ||
<a name="generation-name"></a> | ||
## Generation.name | ||
* *name* `String` The name of the create method. | ||
<a name="generation-create"></a> | ||
## new Generation() | ||
Name of *this* [Generation](#class-generation). | ||
* *return*: `Creation` A new [Creation](#class-creation) that is an instance of *this* [Generation](#generation). | ||
<a name="generation-proto"></a> | ||
## Generation.proto | ||
* *proto* `Object` Prototype inheritance for [Creations](#class-creation) created by *this* [Generation](#class-generation). | ||
Creates a new [Creation](#class-creation) that is an instance of *this* [Generation](#generation). | ||
Generation.proto inherits from previous Generation's protos all the way up to Generation.proto which is equal to [Creation](class-creation). | ||
Example: | ||
```javascript | ||
var jim = new Person('Jim', 10, 'male'); | ||
jim.name // 'Jim' | ||
jim.age // 10 | ||
jim.sex // 'male' | ||
jim.sayHello(); // prints out: 'Hello, my name is Jim. What is yours?' | ||
jim.sayBye(); // prints out: 'Goodbye.' | ||
``` | ||
<a name="generation-define-prototype"></a> | ||
@@ -123,3 +136,3 @@ ## Generation.definePrototype([descriptor,] properties) | ||
* *writable* `Boolean` States weather or not properties will be *writable*, defaults to `false`. | ||
* *properties* `Object` An object who's properties will be attached to *this* [Generation's proto](#generation-proto). | ||
* *properties* `Object` An object who's properties will be attached to *this* [Generation.prototype](#generation). | ||
* *return*: `Generation` *This* [Generation](#class-generation). | ||
@@ -181,27 +194,7 @@ | ||
<a name="generation-create"></a> | ||
## Generation.create() | ||
* *return*: `Creation` A new [Creation](#class-creation) that inherits from *this* [Generation.proto](#generation-proto). | ||
Creates a new [Creation](#class-creation) that inherits from *this* [Generation.proto](#generation-proto). | ||
Example: | ||
```javascript | ||
var jim = Person.create('Jim', 10, 'male'); | ||
jim.name // 'Jim' | ||
jim.age // 10 | ||
jim.sex // 'male' | ||
jim.sayHello(); // prints out: 'Hello, my name is Jim. What is yours?' | ||
jim.sayBye(); // prints out: 'Goodbye.' | ||
``` | ||
<a name="generation-generate"></a> | ||
## Generation.generate(create) | ||
* *create* `Function` Create method that gets called when creating a new object that inherits from *this* [Generation.proto](#generation-proto). | ||
* *return*: `Generation` A new [Generation](#class-generation) that inherits from *this* [Generation](#class-generation). | ||
* *create* `Function` Constructor that with inherit form [Generation](#class-generation). | ||
* *return*: `Constructor` The inputed `create` function. | ||
@@ -215,5 +208,3 @@ Returns a new [Generation](#class-generation) that inherits from *this* [Generation](#class-generation). | ||
function Student(name, age, sex, studentId) { | ||
// 'supercreate' method is only available in this create method scope. | ||
// NOTE: if the 'supercreate' method is not called implicitly it will be called with no arguments. | ||
this.supercreate(name, age, sex); | ||
Person.call(this, name, age, sex); | ||
this.studentId = studentId || 'A0000000000'; | ||
@@ -234,3 +225,3 @@ } | ||
var sarah = Student.create('Sarah', 17, 'female', 'A0123456789'); | ||
var sarah = new Student('Sarah', 17, 'female', 'A0123456789'); | ||
@@ -261,3 +252,3 @@ sarah.name // 'Sarah' | ||
Returns `true` if *test* inherits from *this* [Generation.proto](#generation-proto), `false` otherwise. | ||
Returns `true` if *test* is an instance of *this* [Generation](#generation), `false` otherwise. | ||
@@ -267,3 +258,3 @@ <a name="class-creation"></a> | ||
A new Creation that inherits from a [Generation's proto](#generation-proto) that created it using the [Generation.create()](#generation-create) method. | ||
A instance [Generation](#generation) that created it using the [new Generation()](#generation-create). | ||
@@ -270,0 +261,0 @@ <a name="creation-define-properties"></a> |
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
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
1081698
34
896
328