JSFiddle - React, Tailwind, and code Playground
HTML
<!-- Test brief: ## Exercise the Second - JavaScript Write a function called `Value`, returning an object that stores a number. The value of the returned object will be mutable but protected by minimum and maximum bounds. It should expose the following interface: ``` var v=V alue() v.value //=> 0
var v2 = Value({value: 10, min: 0, max: 100})
v2.value = 101
//=> Error: Out of bounds
// START TEST SUITE
var test = require('tap').test;
var Value = require('../value');
test('Value is defined', function(t){
t.type(Value, 'function');
t.end();
});
test('Value has no enumerable properties', function(t){
t.equal(Object.keys(Value()).length, 0);
t.end();
});
test('property .value is defined', function(t){
t.notEqual(Value({max: 1, value: Math.random()}), void 0);
t.end();
});
test('.value should be 0 by default', function(t){
t.equal(Value().value, 0);
t.end();
});
test('.value can be assigned on instantiation', function(t){
t.equal(Value({value: 2}).value, 2);
t.end();
});
test('.value is between 0 and 1', function(t){
var val = Value({max: 1, value: Math.random()});
t.ok(val.value <= 1, 'should create a value less than 1');
t.end();
});
test('setting to disallowed value throws error', function(t){
var val = Value({max: 1, value: Math.random()});
try {
val.value = 2;
} catch(e){
t.equal(e.message, "Error: Out of bounds");
}
t.end();
});
test('.value can be re-set within bounds', function(t){
var val = Value({max: 5, value: 2});
val.value = 4;
t.equal(val.value, 4);
t.end();
});
test('.value cannot be set to below minimum', function(t){
var val = Value({min: 5, value: 6});
try {
val.value = 4;
} catch(e) {
t.equal(e.message, "Error: Out of bounds");
}
t.end();
});
// End test suite
JavaScript
// START TEST SUITE
var test = require('tap').test;
var Value = require('../value');
test('Value is defined', function(t){
t.type(Value, 'function');
t.end();
});
test('Value has no enumerable properties', function(t){
t.equal(Object.keys(Value()).length, 0);
t.end();
});
test('property .value is defined', function(t){
t.notEqual(Value({max: 1, value: Math.random()}), void 0);
t.end();
});
test('.value should be 0 by default', function(t){
t.equal(Value().value, 0);
t.end();
});
test('.value can be assigned on instantiation', function(t){
t.equal(Value({value: 2}).value, 2);
t.end();
});
test('.value is between 0 and 1', function(t){
var val = Value({max: 1, value: Math.random()});
t.ok(val.value <= 1, 'should create a value less than 1');
t.end();
});
test('setting to disallowed value throws error', function(t){
var val = Value({max: 1, value: Math.random()});
try {
val.value = 2;
} catch(e){
t.equal(e.message, "Error: Out of bounds");
}
t.end();
});
test('.value can be re-set within bounds', function(t){
var val = Value({max: 5, value: 2});
val.value = 4;
t.equal(val.value, 4);
t.end();
});
test('.value cannot be set to below minimum', function(t){
var val = Value({min: 5, value: 6});
try {
val.value = 4;
} catch(e) {
t.equal(e.message, "Error: Out of bounds");
}
t.end();
});
// End test suite
// Check if value is a number, otherwise throw a named error.
function isNumber(val, name) {
// Error checks, if it isn't a number...
if (typeof val !== 'number') {
// And isn't a min or max value (which can be unbounded)...
if (name === 'Min' || name === 'Max') {
if (val === undefined) {
return val;
}
}
// Throw an error!
throw ("Error: " + name + " is not a valid number");
}
// Otherwise business as usual
return val;
}
// Check if a value is within min and max bounds
function...