Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
ezobjects
Advanced tools
EZ Objects is a Node.js module (that can also be usefully browserify'd) that aims to save you lots of time writing class objects that are strictly typed in JavaScript. All you have to do is create simple configurations for each of your objects and then create them using the createClass() function.
EZ Object's MySQL capability has been shifted to a separate package called ezobjects-mysql
. The v3.0
branch for that package should be released shortly. In the meantime, you can keep an eye on it
here: ezobjects-mysql
npm install --save ezobjects
const ezobjects = require(`ezobjects`);
/** Configure a basic EZ Object example */
const configBasicExample = {
className: 'BasicExample',
properties: [
{ name: 'name', type: 'string' }
]
};
/** Create the `BasicExample` class */
ezobjects.createClass(configBasicExample);
/** Create a new instance of the `BasicExample` class */
const basicExample1 = new BasicExample();
/** Give it a name using the auto-generated setter */
basicExample1.name('Basic Example 1');
/** Output the instance to console */
console.log(basicExample1);
/**
* Create another instance of the `BasicExample` class, but this time
* initialize it with a plain object passed to the constructor.
*/
const basicExample2 = new BasicExample({
name: 'Basic Example 2'
});
/**
* Configure a full EZ Object example demonstrating all supported types,
* including the ability to extend other objects.
*/
const configFullExample = {
className: 'FullExample',
extends: BasicExample,
properties: [
{ name: 'exampleInt', type: 'int' },
{ name: 'exampleFloat', type: 'float' },
{ name: 'exampleString', type: 'string' },
{ name: 'exampleBoolean', type: 'boolean' },
{ name: 'exampleFunction', type: 'function' },
{ name: 'exampleDate', type: 'date' },
{ name: 'exampleBuffer', type: 'buffer' },
{ name: 'exampleSet', type: 'set' },
{ name: 'exampleOtherObj', type: 'BasicExample' },
{ name: 'exampleIntArray', type: 'array', arrayOf: { type: 'int' } },
{ name: 'exampleFloatArray', type: 'array', arrayOf: { type: 'float' } },
{ name: 'exampleStringArray', type: 'array', arrayOf: { type: 'string' } },
{ name: 'exampleBooleanArray', type: 'array', arrayOf: { type: 'boolean' } },
{ name: 'exampleFunctionArray', type: 'array', arrayOf: { type: 'function' } },
{ name: 'exampleDateArray', type: 'array', arrayOf: { type: 'date' } },
{ name: 'exampleBufferArray', type: 'array', arrayOf: { type: 'buffer' } },
{ name: 'exampleSetArray', type: 'array', arrayOf: { type: 'set' } },
{ name: 'exampleOtherObjArray', type: 'array', arrayOf: { type: 'BasicExample' } }
]
};
/** Create the `FullExample` class */
ezobjects.createClass(configFullExample);
/** Create a new instance of the `FullExample` class */
const fullExample = new FullExample({
name: `Full Example`,
exampleInt: 293,
exampleFloat: 194.13489,
exampleString: `What's up, doc?`,
exampleBoolean: true,
exampleFunction: arg => `Hello, ${arg}!`,
exampleDate: new Date('1776-07-04'),
exampleBuffer: Buffer.from([0x04, 0x7F, 0x93, 0x38]),
exampleSet: new Set(['14', 3, false]),
exampleOtherObj: basicExample1,
exampleIntArray: [293, -178, 492],
exampleFloatArray: [194.13489, -2890.25, -0.04281],
exampleStringArray: [`What's up, doc?`, `Shiver me timbers`],
exampleBooleanArray: [true, false, true],
exampleFunctionArray: [arg => `Hello, ${arg}!`, arg => `Farewell, ${arg}!`],
exampleDateArray: [new Date('1776-07-04'), new Date('1941-12-07')],
exampleBufferArray: [Buffer.from([0x04, 0x7F, 0x93, 0x38]), Buffer.from('A string instead')],
exampleSetArray: [new Set(['14', 3, false]), new Set([-14, true, 'pool'])],
exampleOtherObjArray: [basicExample1, basicExample2]
});
/** Output the instance to console */
console.log(fullExample);
/** Output one of the function responses to console */
console.log(fullExample.exampleFunctionArray()[1]('Rich'));
/** Try to set a `float` property with a `string` */
try {
/**
* This throws a TypeError since string given, not float; the same behavior
* can be expected for all other types, including arrays of types.
*/
fullExample.exampleFloat('test');
} catch ( err ) {
/** Output error message to console */
console.log(err.message);
}
BasicExample { _name: 'Basic Example 1' }
FullExample {
_name: 'Full Example',
_exampleInt: 293,
_exampleFloat: 194.13489,
_exampleString: 'What\'s up, doc?',
_exampleBoolean: true,
_exampleFunction: [Function: exampleFunction],
_exampleDate: 1776-07-04T00:00:00.000Z,
_exampleBuffer: <Buffer 04 7f 93 38>,
_exampleSet: Set { '14', 3, false },
_exampleOtherObj: BasicExample { _name: 'Basic Example 1' },
_exampleIntArray: [ 293, -178, 492 ],
_exampleFloatArray: [ 194.13489, -2890.25, -0.04281 ],
_exampleStringArray: [ 'What\'s up, doc?', 'Shiver me timbers' ],
_exampleBooleanArray: [ true, false, true ],
_exampleFunctionArray: [ [Function], [Function] ],
_exampleDateArray: [ 1776-07-04T00:00:00.000Z, 1941-12-07T00:00:00.000Z ],
_exampleBufferArray:
[ <Buffer 04 7f 93 38>,
<Buffer 41 20 73 74 72 69 6e 67 20 69 6e 73 74 65 61 64> ],
_exampleSetArray: [ [ [Set], [Set] ], [ [Set], [Set] ] ],
_exampleOtherObjArray:
[ BasicExample { _name: 'Basic Example 1' },
BasicExample { _name: 'Basic Example 2' } ] }
Farewell, Rich!
FullExample.exampleFloat(): Non-numeric value passed to 'float' setter.
These are the object method signatures that all of your EZ Objects will have, though note that you can always add other functionality by adding to the object prototype:
PlainObject
- (optional)data
. Keys can either be equal to the name of a property, or they can be have an underscore before the name of a property, as would be the case if you were to JSON.stringify() and then JSON.parse() an EZ Object. This allows for easy transferability in cases where JSON is used as the transfer medium.string
- (optional)data
. Keys can either be equal to the name of a property, or they can be have an underscore before the name of a property, as would be the case if you were to JSON.stringify() an EZ Object. This allows for easy transferability in cases where JSON is used as the transfer medium.PlainObject
data
. This is also the method used by the constructor.In addition, each property you define will have a single method that is a getter and setter, and it will have the following signatures:
mixed
mixed
TypeError
if value
is not of the correct javascript data type for myPropertyTypeError
if the javascript data type does not match the configuration, this is how the strict typing is implemented. This signature returns this
to allow for set call chaining.The EZ Objects module exports two functions:
objectConfig
, with constructor, initializer, getters, setters.obj
is an descendant of a constructor constructorName
.See the following for how to configure your EZ Objects:
string
- (required) Name of the classArray
- (required) An array of property configurations that the object should have corresponding properties formixed
- (optional) The object that the new object should be extended from [required to extend object]string
- (required) Name of the property, must conform to JavaScript rulesstring
- (optional) EZ Object type that the property must be equal to -- types can be int
, float
, string
, boolean
, date
, buffer
, set
, function
, any other valid object constructor name, or array
where arrayOf
is provided with information about the array element types. [either type or instanceOf is required]string
- (optional) JavaScript class constructor name, that the property must be an instance of [either type or instanceOf is required]mixed
- (optional) Sets the default value for the property in the class objectboolean
- (optional) Indicates the property can be null, default is that only custom object types are nullableobject
- (required for type array
) A plain object containing he EZ Object type
or instanceOf
of the elements of the array -- types can be int
, float
, string
, boolean
, date
, buffer
, set
, function
, or any other valid object constructor name (which can alternatively be used with instanceOf
instead). [either type or instanceOf is required]function
- (optional) Function that transforms and returns the property value prior to settingint
- 0
double
- 0
string
- ``boolean
- false
function
- function () { }
date
- new Date(0)
buffer
- Buffer.from([])
set
- new Set()
array
- []
null
Please open an issue on the GitHub repository if you find any broken functionality or other bugs/errors. Feature requests will also be accepted, but are not guaranteed to be implemented.
MIT Licensed
FAQs
Easy Auto-Generated Strictly-Typed JavaScript Class Objects
The npm package ezobjects receives a total of 40 weekly downloads. As such, ezobjects popularity was classified as not popular.
We found that ezobjects demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.