ezobjects
Advanced tools
Comparing version 0.2.9 to 0.5.1
const ezobjects = require('./index'); | ||
/** Create our customized object complete with constructor/init/getters/setters! */ | ||
/** Create a customized object on the global (node) or window (browser) namespace, complete with constructor/init/getters/setters */ | ||
ezobjects({ | ||
tableName: 'people', | ||
className: 'DatabaseRecord', | ||
fields: [ | ||
{ name: 'id', type: 'int' } | ||
] | ||
}); | ||
/** Create another customized object that extends the first one */ | ||
ezobjects({ | ||
className: 'Person', | ||
extends: DatabaseRecord, | ||
fields: [ | ||
{ name: 'id', type: 'int', default: -1 }, | ||
{ name: 'firstName', type: 'string' }, | ||
@@ -17,3 +24,3 @@ { name: 'lastName', type: 'string' }, | ||
/** Example new object initialized to defaults */ | ||
/** Example of the extended object newly instansiated */ | ||
const a = new Person(); | ||
@@ -23,3 +30,3 @@ | ||
/** Example new object initialized using `data` object passed to constructor */ | ||
/** Example of the extended object instansiated and initialized using object passed to constructor */ | ||
const b = new Person({ | ||
@@ -36,3 +43,3 @@ id: 1, | ||
/** Example new object initialized to defaults, then loaded with data using setter methods */ | ||
/** Example of the extended object instansiated, then loaded with data using setter methods */ | ||
const c = new Person(); | ||
@@ -49,3 +56,3 @@ | ||
/** Example retrieving data from object using getter methods */ | ||
/** Example of the extended object's properties being accessed using getter methods */ | ||
console.log(`ID: ${c.id()}`); | ||
@@ -57,1 +64,42 @@ console.log(`First Name: ${c.firstName()}`); | ||
console.log(`Favorite Day: ${c.favoriteDay().toString()}`); | ||
/** Adding capability to the generated object's prototype */ | ||
DatabaseRecord.prototype.table = function (arg) { | ||
if ( arg === undefined ) | ||
return this._table; | ||
this._table = arg; | ||
}; | ||
/** Yuck, now I have to manually override the init() call */ | ||
DatabaseRecord.prototype.init = function (data = {}) { | ||
this.id(data.id || 0); | ||
this.table(data.table || ''); | ||
}; | ||
const d = new DatabaseRecord(); | ||
console.log(d); | ||
/** These objects can be extended */ | ||
class DatabaseRecord2 extends DatabaseRecord { | ||
constructor(data = {}) { | ||
super(data); | ||
} | ||
init(data = {}) { | ||
super.init(data); | ||
this.test('Test'); | ||
} | ||
test(arg) { | ||
if ( arg === undefined ) | ||
return this._test; | ||
this._test = arg; | ||
} | ||
} | ||
const e = new DatabaseRecord2(); | ||
console.log(e); |
244
index.js
@@ -15,113 +15,14 @@ /** | ||
/** Create new class on global scope */ | ||
parent[table.className] = class { | ||
parent[table.className] = class extends (table.extends || Object) { | ||
/** Constructor for new object. */ | ||
constructor(data = {}) { | ||
/** Loop through each field in the table */ | ||
table.fields.forEach((col) => { | ||
/** For 'int' type fields */ | ||
if ( col.type == 'int' ) { | ||
this[col.name] = (arg) => { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'number' ) | ||
this[`_${col.name}`] = parseInt(arg); | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For 'float' type fields */ | ||
else if ( col.type == 'float' ) { | ||
this[col.name] = (arg) => { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'number' ) | ||
this[`_${col.name}`] = parseFloat(arg); | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For 'string' type fields */ | ||
else if ( col.type == 'string' ) { | ||
this[col.name] = (arg) => { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'string' ) | ||
this[`_${col.name}`] = arg.toString(); | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For 'Array' type fields */ | ||
else if ( col.type == 'Array' ) { | ||
this[col.name] = (arg) => { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'object' && arg.constructor.name == col.type ) | ||
this[`_${col.name}`] = arg; | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For all other field types */ | ||
else { | ||
this[col.name] = (arg) => { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( arg === null || ( typeof arg == 'object' && arg.constructor.name == col.type ) ) | ||
this[`_${col.name}`] = arg; | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
}); | ||
super(data); | ||
this.init(data); | ||
} | ||
/** Initialize object with any provided `data` */ | ||
init(data = {}) { | ||
if ( typeof super.init === 'function' ) | ||
super.init(data); | ||
/** Loop through each field in the table */ | ||
@@ -132,2 +33,6 @@ table.fields.forEach((col) => { | ||
this[col.name](data[col.name] || col.default || 0); | ||
/** Initialize 'boolean' types to false */ | ||
else if ( col.type == 'boolean' ) | ||
this[col.name](data[col.name] || col.default || false); | ||
@@ -137,7 +42,7 @@ /** Initialize 'string' types to empty */ | ||
this[col.name](data[col.name] || col.default || ''); | ||
/** Initialize 'Array' types to empty */ | ||
else if ( col.type == 'Array' ) | ||
this[col.name](data[col.name] || col.default || []); | ||
/** Initialize all other types to null */ | ||
@@ -150,2 +55,125 @@ else | ||
/** Loop through each field in the table */ | ||
table.fields.forEach((col) => { | ||
/** For 'int' type fields */ | ||
if ( col.type == 'int' ) { | ||
parent[table.className].prototype[col.name] = function (arg) { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'number' ) | ||
this[`_${col.name}`] = parseInt(arg); | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For 'float' type fields */ | ||
else if ( col.type == 'float' ) { | ||
parent[table.className].prototype[col.name] = function (arg) { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'number' ) | ||
this[`_${col.name}`] = parseFloat(arg); | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For 'boolean' type fields */ | ||
if ( col.type == 'boolean' ) { | ||
parent[table.className].prototype[col.name] = function (arg) { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'boolean' ) | ||
this[`_${col.name}`] = arg; | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For 'string' type fields */ | ||
else if ( col.type == 'string' ) { | ||
parent[table.className].prototype[col.name] = function (arg) { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'string' ) | ||
this[`_${col.name}`] = arg.toString(); | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For 'Array' type fields */ | ||
else if ( col.type == 'Array' ) { | ||
parent[table.className].prototype[col.name] = function (arg) { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( typeof arg == 'object' && arg.constructor.name == col.type ) | ||
this[`_${col.name}`] = arg; | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
/** For all other field types */ | ||
else { | ||
parent[table.className].prototype[col.name] = function (arg) { | ||
/** Getter */ | ||
if ( arg === undefined ) | ||
return this[`_${col.name}`]; | ||
/** Setter */ | ||
else if ( arg === null || ( typeof arg == 'object' && arg.constructor.name == col.type ) ) | ||
this[`_${col.name}`] = arg; | ||
/** Handle type errors */ | ||
else | ||
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`); | ||
/** Return this object for set call chaining */ | ||
return this; | ||
}; | ||
} | ||
}); | ||
/** | ||
@@ -155,3 +183,3 @@ * Because we're creating this object dynamically, we need to manually give it a name | ||
*/ | ||
Object.defineProperty(parent[table.className], 'name', { value: table.name }); | ||
Object.defineProperty(parent[table.className], 'name', { value: table.className }); | ||
} |
{ | ||
"name": "ezobjects", | ||
"version": "0.2.9", | ||
"version": "0.5.1", | ||
"description": "Easy dynamic object generation with strict typing and set chaining", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
123
README.md
@@ -1,5 +0,34 @@ | ||
# EZ Objects | ||
# EZ Objects v0.5.1 | ||
We're just getting started, check back later. | ||
Under development, but completely useable. | ||
## Principles of Operation | ||
This module, when required, is a function that takes a single object argument. At present, that object can have the | ||
following keys: | ||
* className - A string containing the name of the desired class object | ||
* extends - An object that you wish the class to extend from (optional, note this is the class itself, not the name) | ||
* fields - An array of fields (properties) that the class will have getters/setters/initialization for | ||
Each field in the array is an object that can have the following keys: | ||
* name - The name of the field | ||
* type - The type of the field (string, int, float, boolean, Array, or any other object name) | ||
* default - The default initialized value | ||
Default defaults are: | ||
* string - '' | ||
* int - 0 | ||
* float - 0 | ||
* boolean - false | ||
* Array - [] | ||
* Any others - null | ||
Note that the created objects are added to the global space, being `global` (node) or `window` (browser). They can | ||
have other properties/methods added using the prototype, though note that if you want prototype-added properties to be | ||
initialized, you'll have to rewrite the init() function manually. Alternatively, you can just extend the | ||
class and init the parent with `super`. See examples below. | ||
## Example | ||
@@ -10,8 +39,15 @@ | ||
/** Create our customized object complete with constructor/init/getters/setters! */ | ||
/** Create a customized object on the global (node) or window (browser) namespace, complete with constructor/init/getters/setters */ | ||
ezobjects({ | ||
tableName: 'people', | ||
className: 'DatabaseRecord', | ||
fields: [ | ||
{ name: 'id', type: 'int' } | ||
] | ||
}); | ||
/** Create another customized object that extends the first one */ | ||
ezobjects({ | ||
className: 'Person', | ||
extends: DatabaseRecord, | ||
fields: [ | ||
{ name: 'id', type: 'int', default: -1 }, | ||
{ name: 'firstName', type: 'string' }, | ||
@@ -25,3 +61,3 @@ { name: 'lastName', type: 'string' }, | ||
/** Example new object initialized to defaults */ | ||
/** Example of the extended object newly instansiated */ | ||
const a = new Person(); | ||
@@ -31,3 +67,3 @@ | ||
/** Example new object initialized using `data` object passed to constructor */ | ||
/** Example of the extended object instansiated and initialized using object passed to constructor */ | ||
const b = new Person({ | ||
@@ -44,3 +80,3 @@ id: 1, | ||
/** Example new object initialized to defaults, then loaded with data using setter methods */ | ||
/** Example of the extended object instansiated, then loaded with data using setter methods */ | ||
const c = new Person(); | ||
@@ -57,3 +93,3 @@ | ||
/** Example retrieving data from object using getter methods */ | ||
/** Example of the extended object's properties being accessed using getter methods */ | ||
console.log(`ID: ${c.id()}`); | ||
@@ -65,2 +101,43 @@ console.log(`First Name: ${c.firstName()}`); | ||
console.log(`Favorite Day: ${c.favoriteDay().toString()}`); | ||
/** Adding capability to the generated object's prototype */ | ||
DatabaseRecord.prototype.table = function (arg) { | ||
if ( arg === undefined ) | ||
return this._table; | ||
this._table = arg; | ||
}; | ||
/** Yuck, now I have to manually override the init() call */ | ||
DatabaseRecord.prototype.init = function (data = {}) { | ||
this.id(data.id || 0); | ||
this.table(data.table || ''); | ||
}; | ||
const d = new DatabaseRecord(); | ||
console.log(d); | ||
/** These objects can be extended */ | ||
class DatabaseRecord2 extends DatabaseRecord { | ||
constructor(data = {}) { | ||
super(data); | ||
} | ||
init(data = {}) { | ||
super.init(data); | ||
this.test('Test'); | ||
} | ||
test(arg) { | ||
if ( arg === undefined ) | ||
return this._test; | ||
this._test = arg; | ||
} | ||
} | ||
const e = new DatabaseRecord2(); | ||
console.log(e); | ||
``` | ||
@@ -71,10 +148,4 @@ | ||
``` | ||
{ id: [Function], | ||
firstName: [Function], | ||
lastName: [Function], | ||
checkingBalance: [Function], | ||
permissions: [Function], | ||
favoriteDay: [Function], | ||
_id: -1, | ||
Person { | ||
_id: 0, | ||
_firstName: '', | ||
@@ -85,8 +156,3 @@ _lastName: '', | ||
_favoriteDay: null } | ||
{ id: [Function], | ||
firstName: [Function], | ||
lastName: [Function], | ||
checkingBalance: [Function], | ||
permissions: [Function], | ||
favoriteDay: [Function], | ||
Person { | ||
_id: 1, | ||
@@ -98,8 +164,3 @@ _firstName: 'Rich', | ||
_favoriteDay: 2018-01-01T06:00:00.000Z } | ||
{ id: [Function], | ||
firstName: [Function], | ||
lastName: [Function], | ||
checkingBalance: [Function], | ||
permissions: [Function], | ||
favoriteDay: [Function], | ||
Person { | ||
_id: 2, | ||
@@ -117,2 +178,4 @@ _firstName: 'Bert', | ||
Favorite Day: Thu Jun 01 2017 00:00:00 GMT-0500 (CDT) | ||
``` | ||
DatabaseRecord { _id: 0, _table: '' } | ||
DatabaseRecord2 { _id: 0, _table: '', _test: 'Test' } | ||
``` |
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
13870
225
172