Skip to content

Instantly share code, notes, and snippets.

@karolk
Last active December 23, 2015 19:29
Show Gist options
  • Save karolk/6682494 to your computer and use it in GitHub Desktop.
Save karolk/6682494 to your computer and use it in GitHub Desktop.

Write a function Value returning an object, which purpose is to store a numeric value. The value of the object will be mutable, but also protected with min and max. This should be achieved through getters and setters.

Example usage:

v = Value();
v.value
//=> 0

v2 = Value({value: 10, min: 0, max: 100});
v2.value = 101
//=> Error: Out of bounds

The implementation should pass all tests defined in tests.js. To run tests:

runTests(tests);
function runTests(tests) {
var failed = 0;
tests.push({
expr: function() {
return tests.length-1 === i
},
"label": "Run all tests successfully"
});
for (var i=0; i<tests.length; i++) {
var runExpr = tests[i].expr();
if (!runExpr) failed++;
console.assert(runExpr, tests[i].label);
}
console.log('Passed: ' + (tests.length-failed), "Failed: " + failed);
}
var tests = [
{
expr: function() {
return Value != void 0
},
label: "Value is defined"
},
{
expr: function() {
return Object.keys(Value()).length == 0;
},
label: "Value has no enumerable properties"
},
{
expr: function() {
return Value().value == 0
},
label: "By default value should be 0"
},
{
expr: function() {
var between0And1 = Value({max:1, value:Math.random()});
return between0And1.value != void 0;
},
label: "property .value is defined"
},
{
expr: function() {
var between0And1 = Value({max:1, value:Math.random()});
return between0And1.value>=0 && between0And1.value<=1;
},
label: "value between 0 and 1"
},
{
expr: function() {
var between0And1 = Value({max:1, value:Math.random()});
try {
between0And1.value = -1
}
catch(e) {
return true
}
return false;
},
label: "Trying to set between0And1 to -1 throws an error"
},
{
expr: function () {
var between0And1 = Value({max:1, value:Math.random()});
try {
between0And1.value = 2
}
catch(e) {
return true
}
return false;
},
label: "Trying to set between0And1 to 2 throws an error"
},
{
expr: function() {
var price = Value({max: 100, min: 1, value:50});
return price.value === 50;
},
label: "value initializer works"
},
{
expr: function() {
var price = Value({max: 100, min: 1, value:50});
price.value = 100;
return price.value === 100;
},
label: "value can be set within bounds"
},
{
expr: function() {
var price = Value({max: 100, min: 1, value:50});
try {
price.value = 0;
}
catch(e) {
return true
}
return false;
},
label: "value cannot be set below min"
},
{
expr: function() {
return Value({value:1}) + Value({value:2}) === 3
},
label: "Value can be used in a number context"
},
];
@jh3y
Copy link

jh3y commented Oct 18, 2013

Hey there.

Interested to know how you get all tests to pass with this. I can get all but the number context one working here.

I haven't used the Object.defineProperty and Object.create stuff too much before but I was assuming there was some simple thing I was overlooking or maybe I am creating my object in a slightly different way.

Either way, it's interesting to learn another approach. I hadn't really gone down the get set path much with components I've written so far so this stuff is very useful to know as another approach.

Thanks

@jheytompkins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment