value.js is an attempt at creating strict, immutable value objects in JavaScript. It will work in both the browser or by requiring with node.
I am not a javascript or node expert. This is likely a very naive and greasy solution and may likely cause javascript and node purists to nerdrage. In specific, the equality algorithm is likely naive and broken in edge cases. Please contribute!
Define a value object type and use it!
var Person = Value.define("firstName", "lastName", "age");
var john = new Person({firstName: "John", lastName: "Smith", age: 40});
// john.firstName == "John"
// john.lastName == "Smith"
// john.age == 40
You cannot modify the attributes!
var john = new Person({firstName: "John", lastName: "Smith", age: 40});
john.firstName = "Bob"
The above setter will either do nothing, leaving the original value of firstName set to "John", or if you enable JavaScript's strict mode it will throw a TypeError
Value objects must be constructed with all values
var john = new Person({firstName: "John", lastName: "Smith", age: 40}); // Succeeds!
var bob = new Person({firstName: "Bob"}); // thows a TypeError!
Value objects allow for simple calculated properties. You should not abuse this to make your value objects do anything complex!
var BetterPerson = Value.define("firstName", "lastName", {
fullName: function() {
return this.firstName + " " + this.lastName;
}
});
var john = new Person({firstName: "John", lastName: "Smith"});
// john.fullName == "John Smith";
Values objects are considered equal if all their types and attribute values are equal. See the tests for specific example.