A simpler and safer way to manipulate javascript objects with a defined set of properties
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.travis.yml
README.md
package.json

README.md

strict-object

A utility that makes it safer and prettier to deal with javascript objects that have a defined set of properties.

Status: Build Status

Installing

strict-object is compatable with npm, browsers, windows script host,and probably more.

npm install strict-object

When minified, strict-object.js ends up less than 400 bytes.

What

// Create a new person type
var Person = StrictObject.define(['name', 'age', 'country']);

// Instatiate a Person
var pete = new Person();

// set properties using functions
pete.name('Peter');

// get properties using functions
console.log(pete.name());

// property setters return the target object and are thus chainable
pete.name('Peter')
    .age(26)
    .country('Uzbekistan');

// retrieve the properties as an object
console.log(pete._toObject()); // {name: 'Peter', age: 26, country: 'Uzbekistan'}

// Instatiate a Person with initial values
var pete = new Person({
	name: "Peter",
	age: 26,
	country: "Uzbekawhat?"
});

Running Tests

npm test

Why

Because typos are too easy with basic objects.

var thing = {};
thing.descriptivePropertyName = 'value';
console.log(thing.descriptvePropertyName); // null -- because of a typo.

Syntax is ugly with basic objects

var thing = {
	key: "value",
	key2: "value2"
};

Cleaner and safer with strict-object

thing = new Thing()
	.key("value")
	.key2("value2");

License

No License, the content of this repository is in the public domain, no attribution is necessary.