generate-js
Advanced tools
Comparing version 1.1.2 to 2.0.0
206
generate.js
// generator.js | ||
// Author: Michaelangelo Jong | ||
(function GeneratorScope() { | ||
// Variables | ||
@@ -10,2 +12,6 @@ var Generator = {}, | ||
// Helper Methods | ||
function toString() { | ||
return '[' + (this.name || 'generator') + ' Generator]'; | ||
} | ||
/** | ||
@@ -33,10 +39,10 @@ * Returns the name of function 'func'. | ||
var keys, length; | ||
if (obj instanceof Object) { | ||
if (typeof obj === 'object') { | ||
keys = Object.getOwnPropertyNames(obj).sort(); | ||
length = keys.length; | ||
if ((length === 1 && (keys[0] === 'get' && obj.get instanceof Function || | ||
keys[0] === 'set' && obj.set instanceof Function)) || | ||
(length === 2 && (keys[0] === 'get' && obj.get instanceof Function && | ||
keys[1] === 'set' && obj.set instanceof Function))) { | ||
if ((length === 1 && (keys[0] === 'get' && typeof obj.get === 'function' || | ||
keys[0] === 'set' && typeof obj.set === 'function')) || | ||
(length === 2 && (keys[0] === 'get' && typeof obj.get === 'function' && | ||
keys[1] === 'set' && typeof obj.set === 'function'))) { | ||
return true; | ||
@@ -61,7 +67,7 @@ } | ||
if (!descriptor || !(descriptor instanceof Object)) { | ||
if (!descriptor || typeof descriptor !== 'object') { | ||
descriptor = {}; | ||
} | ||
if (!properties || !(properties instanceof Object)) { | ||
if (!properties || typeof properties !== 'object') { | ||
properties = descriptor; | ||
@@ -102,3 +108,3 @@ descriptor = {}; | ||
*/ | ||
function GeneratorFunc(ParentGenerator, create, init) { | ||
function GeneratorFunc(ParentGenerator, create) { | ||
ParentGenerator = Generator.isGenerator(ParentGenerator) ? ParentGenerator : Generator; | ||
@@ -110,3 +116,2 @@ var proto = Object.create(ParentGenerator.proto), | ||
create = typeof create === 'function' ? create : Generator.proto.__create; | ||
init = typeof init === 'function' ? init : Generator.proto.__init; | ||
@@ -137,3 +142,2 @@ defineObjectProperties( | ||
__create: create, | ||
__init: init | ||
} | ||
@@ -158,2 +162,60 @@ ); | ||
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 | ||
}, | ||
{ | ||
proto: Generator.proto.prototypeProperties, | ||
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, | ||
__create: constructor, | ||
} | ||
); | ||
defineObjectProperties( | ||
generator, | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
name: getFunctionName(constructor), | ||
proto: proto | ||
} | ||
); | ||
return generator; | ||
} | ||
// Generator Instance Inheritance | ||
@@ -196,18 +258,22 @@ defineObjectProperties( | ||
create: function create() { | ||
var newObj = Object.create(this.proto.prototypeProperties); | ||
var _ = this, | ||
newObj = Object.create(_.proto.prototypeProperties), | ||
supercreateCalled = false; | ||
newObj.defineProperties( | ||
{ | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}, | ||
{ | ||
proto: this.proto.prototypeProperties | ||
newObj.supercreate = function supercreate() { | ||
supercreateCalled = true; | ||
if (typeof _.proto.proto === 'object' && _.proto.proto !== Generator.proto){ | ||
_.proto.proto.__create.apply(this, Array.prototype.slice.call(arguments)); | ||
} | ||
); | ||
}; | ||
this.init(newObj); | ||
_.__create.apply(newObj, Array.prototype.slice.call(arguments)); | ||
this.__create.apply(newObj, Array.prototype.slice.call(arguments)); | ||
if (!supercreateCalled) { | ||
newObj.supercreate(); | ||
} | ||
delete newObj.supercreate; | ||
return newObj; | ||
@@ -226,20 +292,2 @@ }, | ||
/** | ||
* Inits any data stores on 'obj' needed by prototypal methods. | ||
* @param {Object} obj A Generator instance. | ||
* @return {undefined} undefined. | ||
*/ | ||
init: function init(obj) { | ||
var me = this, | ||
inits = [], | ||
i; | ||
while (me.proto) { | ||
inits.push(me.proto.__init); | ||
me = me.proto; | ||
} | ||
for (i = inits.length - 1; i >= 0; i--) { | ||
inits[i].call(obj); | ||
} | ||
}, | ||
__init: function() {}, | ||
/** | ||
* Returns true if 'generator' was generated by this Generator. | ||
@@ -251,3 +299,3 @@ * @param {Generator} generator A Generator. | ||
var _ = this; | ||
if (generator instanceof Object && _ !== generator) { | ||
if (typeof generator === 'object' && _ !== generator) { | ||
while (typeof generator.proto === 'object') { | ||
@@ -268,53 +316,7 @@ generator = generator.proto; | ||
isCreation: function isCreation(object) { | ||
var _ = this; | ||
if (object instanceof Object && _ !== object) { | ||
while (typeof object.proto === 'object') { | ||
object = object.proto; | ||
if (_.proto.prototypeProperties === object) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
var _ = this, | ||
generator = typeof object === 'object' ? object.generator : 0; | ||
return generator === _ || _.isGeneration(generator); | ||
}, | ||
/** | ||
* Defines methods on this' prototype. | ||
* @param {Array} methods An array of named methods. | ||
* @return {Generator} This Generator. | ||
*/ | ||
definePrototypeMethods: function definePrototypeMethods(descriptor, methods) { | ||
var setMethods = {}, | ||
i; | ||
if (!methods || !(methods instanceof Object)) { | ||
methods = descriptor; | ||
descriptor = {}; | ||
} | ||
if (!descriptor || !(descriptor instanceof Object)) { | ||
descriptor = {}; | ||
} | ||
if (methods instanceof Array) { | ||
for (i = 0; i < methods.length; i++) { | ||
if (typeof methods[i] === 'function') { | ||
setMethods[getFunctionName(methods[i])] = methods[i]; | ||
} | ||
} | ||
} | ||
this.definePrototype(descriptor, setMethods); | ||
console.warn('Generator.definePrototypeMethods is deprecated, please use \'definePrototype\' instead.'); | ||
return this; | ||
}, | ||
/** | ||
* Defines shared properties for all instances of this. | ||
* @param {Object} properties Object keys => descriptors. | ||
* @return {Generator} This Generator. | ||
*/ | ||
definePrototypeProperties: function definePrototypeProperties(descriptor, properties) { | ||
this.definePrototype(descriptor, properties); | ||
console.warn('Generator.definePrototypeProperties is deprecated, please use \'definePrototype\' instead.'); | ||
return this; | ||
}, | ||
/** | ||
* Defines shared properties for all objects created by this generator. | ||
@@ -328,3 +330,4 @@ * @param {Object} descriptor Optional object descriptor that will be applied to all attaching properties. | ||
return this; | ||
} | ||
}, | ||
toString: toString | ||
} | ||
@@ -342,6 +345,8 @@ ); | ||
{ | ||
name: 'Generator', | ||
name: 'Generator', | ||
proto: GeneratorProto, | ||
generate: GeneratorProto.generate, | ||
isGenerator: GeneratorProto.isGeneration | ||
isGenerator: GeneratorProto.isGeneration, | ||
toGenerator: toGenerator, | ||
toString: toString | ||
} | ||
@@ -351,2 +356,15 @@ ); | ||
// Exports | ||
module.exports = Generator; | ||
if (typeof define === 'function' && define.amd) { | ||
// AMD | ||
define(function() { | ||
return Generator; | ||
}); | ||
} else if (typeof module === 'object') { | ||
// Node/CommonJS | ||
module.exports = Generator; | ||
} else { | ||
// Browser global | ||
window.Generator = Generator; | ||
} | ||
}()); |
{ | ||
"name": "generate-js", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "An easy to use prototypal inheritance model and generator.", | ||
@@ -5,0 +5,0 @@ "main": "generate.js", |
125
README.md
## Table of Contents | ||
* [ Generator ](#generator) | ||
* [ Generator.generate(create, init) ](#generate) | ||
* [ Generator.generate(create) ](#generate) | ||
* [ Generator.isGenerator(test) ](#is-generator) | ||
* [ Generator.toGenerator(constructor) ](#to-generator) | ||
* [ Class: Generation ](#class-generation) | ||
* [ Generation.name ](#generation-name) | ||
* [ Generation.definePrototype([descriptor], properties) ](#generation-define-prototype) | ||
* [ Generation.definePrototype([descriptor,] properties) ](#generation-define-prototype) | ||
* [ Generation.create() ](#generation-create) | ||
* [ Generation.generate(create, init) ](#generation-generate) | ||
* [ Generation.generate(create) ](#generation-generate) | ||
* [ Generation.isCreation(test) ](#generation-is-creation) | ||
* [ Generation.isGeneration(test) ](#generation-is-generation) | ||
* [ Class: Creation ](#class-creation) | ||
* [ Creation.defineProperties([descriptor], properties) ](#creation-define-properties) | ||
* [ Creation.defineProperties([descriptor,] properties) ](#creation-define-properties) | ||
@@ -28,6 +29,5 @@ <a name="generator"></a> | ||
<a name="generate"></a> | ||
## Generator.generate(create, init) | ||
## Generator.generate(create) | ||
* *create* `Function` Create method that gets called when creating a new object that inherits from *this* generator. | ||
* *init* `Function` Init method that gets called to initialize any data stores needed by prototypal methods. | ||
* *return*: `Generator` A new generator that inherits from *this* generator. | ||
@@ -43,12 +43,6 @@ | ||
/* create method */ | ||
function (name, age, sex) { | ||
this.name = name; | ||
this.age = age; | ||
this.sex = sex; | ||
}, | ||
/* init method */ | ||
function () { | ||
this.name = 'bob'; | ||
this.age = 0; | ||
this.sex = 'male'; | ||
function Person(name, age, sex) { | ||
this.name = name || 'no-name'; | ||
this.age = age || 0; | ||
this.sex = sex || 'unknown'; | ||
} | ||
@@ -66,6 +60,34 @@ ); | ||
<a name="to-generator"></a> | ||
## Generator.toGenerator(constructor) | ||
* *constructor* `Function` An constructor to be generatorized. | ||
* *return*: `Generator` A new generator that who's create method is equivalent to calling `new constructor();`. | ||
Returns a new generator that is equivalent to *constructor*. | ||
*NOTE*: Most native constructors can *NOT* be generatorized. | ||
Example: | ||
```javascript | ||
// generatorize NodeJS' EventEmitter | ||
var Generator = require('generate-js'), | ||
events = require('events'); | ||
var EventEmitter = Generator.toGenerator(events.EventEmitter); | ||
// EventEmitter.create() same as new events.EventEmitter(); | ||
// new generators can inherit all the abilities of EventEmitter like so. | ||
var MyNewGenerator = EventEmitter.generate( | ||
/* create method */ | ||
function MyNewGenerator() {} | ||
); | ||
``` | ||
<a name="class-generation"></a> | ||
## Class: Generation | ||
A new generator that inherits from the generator that generated it using the [ Generation.generate(create, init) ](#generation-generate) method. | ||
A new generator that inherits from the generator that generated it using the [ Generation.generate(create) ](#generation-generate) method. | ||
@@ -79,3 +101,3 @@ <a name="generation-name"></a> | ||
<a name="generation-define-prototype"></a> | ||
## Generation.definePrototype([descriptor], properties) | ||
## Generation.definePrototype([descriptor,] properties) | ||
@@ -94,3 +116,3 @@ * *descriptor* `Object` Optional object descriptor that will be applied to all attaching properties. | ||
/* | ||
* Defining prototype properties that can be overwritten by generations. | ||
* Defining prototype properties that can be overwritten by creations using the `=` sign. | ||
*/ | ||
@@ -109,3 +131,3 @@ Person.definePrototype( | ||
/* | ||
* Defining prototype properties that can NOT be overwritten by generations. | ||
* Defining prototype properties that can NOT be overwritten by creations using the `=` sign. | ||
*/ | ||
@@ -125,17 +147,15 @@ Person.definePrototype( | ||
* Defining prototype properties that use getters and setters. | ||
* NOTE: getter/setter prototype properties can NOT be overwritten by generations. | ||
* NOTE: getter/setter prototype properties can NOT be overwritten by creations using the `=` sign. | ||
*/ | ||
(function(){ | ||
var money = 0; | ||
var something = 'something'; | ||
Person.definePrototype({ | ||
money: { | ||
something: { | ||
get: function() { | ||
return money; | ||
return something; | ||
}, | ||
set: function (newMoney) { | ||
if (typeof newMoney === 'number') { | ||
money = newMoney; | ||
} | ||
return money; | ||
set: function (newSomething) { | ||
something = newSomething; | ||
return something; | ||
} | ||
@@ -169,6 +189,5 @@ } | ||
<a name="generation-generate"></a> | ||
## Generation.generate(create, init) | ||
## Generation.generate(create) | ||
* *create* `Function` Create method that gets called when creating a new object that inherits from *this* generator. | ||
* *init* `Function` Init method that gets called to initialize any data stores needed by prototypal methods. | ||
* *return*: `Generator` A new generator that inherits from *this* generator. | ||
@@ -182,11 +201,7 @@ | ||
/* create method */ | ||
function (name, age, sex, studentId) { | ||
this.name = name; | ||
this.age = age; | ||
this.sex = sex; | ||
this.studentId = studentId; | ||
}, | ||
/* init method */ | ||
function () { | ||
this.studentId = 'A0000000000'; | ||
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); | ||
this.studentId = studentId || 'A0000000000'; | ||
} | ||
@@ -240,3 +255,3 @@ ); | ||
<a name="creation-define-properties"></a> | ||
## Creation.defineProperties([descriptor], properties) | ||
## Creation.defineProperties([descriptor,] properties) | ||
@@ -279,1 +294,29 @@ * *descriptor* `Object` Optional object descriptor that will be applied to all attaching properties. | ||
``` | ||
## Author: | ||
Michaelangelo Jong | ||
## License: | ||
The MIT License (MIT) | ||
Copyright (c) 2014-2015 Michaelangelo Jong | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
Sorry, the diff of this file is not supported yet
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
21886
322
312