ezobjects
Advanced tools
Comparing version 0.1.2 to 0.1.3
143
index.js
@@ -7,3 +7,4 @@ /** | ||
const table = { | ||
name: 'Person', | ||
tableName: 'people', | ||
className: 'Person', | ||
fields: [ | ||
@@ -13,2 +14,3 @@ { | ||
type: 'int', | ||
default: -1 | ||
}, | ||
@@ -41,2 +43,3 @@ { | ||
/** Figure out proper global scope between node (global) and browser (window) */ | ||
if ( typeof window !== 'undefined' ) | ||
@@ -47,22 +50,126 @@ parent = window; | ||
parent[table.name] = class { | ||
/** Create new class on global scope */ | ||
parent[table.className] = class { | ||
/** | ||
* Constructor for new object. | ||
*/ | ||
constructor(data = {}) { | ||
/** Loop through each field in the table */ | ||
table.fields.forEach((col) => { | ||
if ( col.type == 'int' ) | ||
this[col.name] = (arg) => { if ( arg === undefined ) return this[`_${col.name}`]; else if ( typeof arg == 'number' ) this[`_${col.name}`] = parseInt(arg); else throw new Error(`${table.name}.${col.name}(${typeof arg}): Invalid signature.`); return this; }; | ||
else if ( col.type == 'float' ) | ||
this[col.name] = (arg) => { if ( arg === undefined ) return this[`_${col.name}`]; else if ( typeof arg == 'number' ) this[`_${col.name}`] = parseFloat(arg); else throw new Error(`${table.name}.${col.name}(${typeof arg}): Invalid signature.`); return this; }; | ||
else if ( col.type == 'string' ) | ||
this[col.name] = (arg) => { if ( arg === undefined ) return this[`_${col.name}`]; else if ( typeof arg == 'string' ) this[`_${col.name}`] = arg.toString(); else throw new Error(`${table.name}.${col.name}(${typeof arg}): Invalid signature.`); return this; }; | ||
else | ||
this[col.name] = (arg) => { if ( arg === undefined ) return this[`_${col.name}`]; else if ( arg === null || ( typeof arg == 'object' && arg.constructor.name == col.type ) ) this[`_${col.name}`] = arg; else throw new Error(`${table.name}.${col.name}(${typeof arg}): Invalid signature.`); return this; }; | ||
/** 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; | ||
}; | ||
} | ||
}); | ||
/** Loop through each field in the table again */ | ||
table.fields.forEach((col) => { | ||
/** Initialize 'int' and 'float' types to zero */ | ||
if ( col.type == 'int' || col.type == 'float' ) | ||
this[col.name](data[col.name] || col.default || 0); | ||
/** Initialize 'string' types to empty */ | ||
else if ( col.type == 'string' ) | ||
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 */ | ||
else | ||
@@ -74,3 +181,7 @@ this[col.name](data[col.name] || col.default || null); | ||
Object.defineProperty(parent[table.name], 'name', {value: table.name}); | ||
/** | ||
* Because we're creating this object dynamically, we need to manually give it a name | ||
* attribute so we can identify it by its type when we want to. | ||
*/ | ||
Object.defineProperty(parent[table.className], 'name', {value: table.name}); | ||
} | ||
@@ -80,3 +191,7 @@ | ||
const a = new Person({ | ||
const a = new Person(); | ||
console.log(a); | ||
const b = new Person({ | ||
id: 1, | ||
@@ -90,2 +205,4 @@ firstName: 'Rich', | ||
console.log(a); | ||
console.log(b); | ||
{ | ||
"name": "ezobjects", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Easy dynamic object generation with strict typing and set chaining", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
234
README.md
# EZ Objects | ||
We're just getting started, check back later. | ||
## Example | ||
```javascript | ||
/** | ||
* @copyright 2018 Rich Lowe | ||
* @license MIT | ||
*/ | ||
const table = { | ||
tableName: 'people', | ||
className: 'Person', | ||
fields: [ | ||
{ | ||
name: 'id', | ||
type: 'int', | ||
default: -1 | ||
}, | ||
{ | ||
name: 'firstName', | ||
type: 'string' | ||
}, | ||
{ | ||
name: 'lastName', | ||
type: 'string' | ||
}, | ||
{ | ||
name: 'checkingBalance', | ||
type: 'float' | ||
}, | ||
{ | ||
name: 'permissions', | ||
type: 'Array' | ||
}, | ||
{ | ||
name: 'favoriteDay', | ||
type: 'Date' | ||
} | ||
] | ||
}; | ||
function createObject(table) { | ||
let parent; | ||
/** Figure out proper global scope between node (global) and browser (window) */ | ||
if ( typeof window !== 'undefined' ) | ||
parent = window; | ||
else | ||
parent = global; | ||
/** Create new class on global scope */ | ||
parent[table.className] = class { | ||
/** | ||
* 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; | ||
}; | ||
} | ||
}); | ||
/** Loop through each field in the table again */ | ||
table.fields.forEach((col) => { | ||
/** Initialize 'int' and 'float' types to zero */ | ||
if ( col.type == 'int' || col.type == 'float' ) | ||
this[col.name](data[col.name] || col.default || 0); | ||
/** Initialize 'string' types to empty */ | ||
else if ( col.type == 'string' ) | ||
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 */ | ||
else | ||
this[col.name](data[col.name] || col.default || null); | ||
}); | ||
} | ||
} | ||
/** | ||
* Because we're creating this object dynamically, we need to manually give it a name | ||
* attribute so we can identify it by its type when we want to. | ||
*/ | ||
Object.defineProperty(parent[table.className], 'name', {value: table.name}); | ||
} | ||
createObject(table); | ||
const a = new Person(); | ||
console.log(a); | ||
const b = new Person({ | ||
id: 1, | ||
firstName: 'Rich', | ||
lastName: 'Lowe', | ||
checkingBalance: 4.87, | ||
permissions: [1, 2, 3], | ||
favoriteDay: new Date('01-01-2018') | ||
}); | ||
console.log(b); | ||
``` | ||
## Output | ||
```json | ||
{ id: [Function], | ||
firstName: [Function], | ||
lastName: [Function], | ||
checkingBalance: [Function], | ||
permissions: [Function], | ||
favoriteDay: [Function], | ||
_id: -1, | ||
_firstName: '', | ||
_lastName: '', | ||
_checkingBalance: 0, | ||
_permissions: [], | ||
_favoriteDay: null } | ||
{ id: [Function], | ||
firstName: [Function], | ||
lastName: [Function], | ||
checkingBalance: [Function], | ||
permissions: [Function], | ||
favoriteDay: [Function], | ||
_id: 1, | ||
_firstName: 'Rich', | ||
_lastName: 'Lowe', | ||
_checkingBalance: 4.87, | ||
_permissions: [ 1, 2, 3 ], | ||
_favoriteDay: 2018-01-01T06:00:00.000Z } | ||
``` |
13385
166
237