Skip to content

Commit

Permalink
Yay everything is working
Browse files Browse the repository at this point in the history
  • Loading branch information
imlucas committed Jul 22, 2013
1 parent 799e5da commit 5661ed5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,30 @@ util.inherits(StringField, Field);

function NumberField(opts){
NumberField.super_.call(this, opts);
this.filters = ['toInt'];
}
util.inherits(NumberField, Field);

function BooleanField(opts){
BooleanField.super_.call(this, opts);
this.filters = ['toBoolean'];
}
util.inherits(BooleanField, Field);

// Cast a field to a proper Date object.
// Doesn't matter if its a string format or epoch.
function DateField(opts){
DateField.super_.call(this, opts);
this.filters = [this.castDate.bind(this)];
}
util.inherits(DateField, Field);

DateField.prototype.set = function(val){
this.value = new Date(Number(val));
return this;
DateField.prototype.castDate = function(val){
if(val){
var ms = parseInt(val, 10);
return new Date((isNaN(ms) ? val : ms));
}
return val;
};

module.exports = exports;
Expand Down
50 changes: 47 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,53 @@ describe('forro', function(){
assert.equal(form.val('username'), 'a');
});

it('should correctly cast Date fields from strings');
it('should cast numbers', function(){
var PizzaForm = forro({
'quantity': forro.number()
}), form;
form = new PizzaForm({'quantity': ' 1 '}).validate();
assert.equal(form.val('quantity'), 1);
});

it('should cast booleans', function(){
var PizzaForm = forro({
'hasExtraToppings': forro.boolean(),
'wantItNow': forro.boolean(),
'breadsticks': forro.boolean()
}), form;

form = new PizzaForm({
'hasExtraToppings': ' 1 ',
'wantItNow': 'oh yeah',
'breadsticks': 'false'
}).validate();

assert.deepEqual(form.val('hasExtraToppings'), true);
assert.deepEqual(form.val('wantItNow'), true);
assert.deepEqual(form.val('breadsticks'), false);
});

it('should correctly cast Date fields from timestamps as strings');
it('should correctly cast Date fields from strings', function(){
var PizzaForm = forro({
'orderPlaced': forro.date()
}), form, when = new Date();

it('should not automatically supply a default value if none specified');
form = new PizzaForm({
'orderPlaced': when.toUTCString()
}).validate();

assert.equal(form.val('orderPlaced').toUTCString(), when.toUTCString());
});

it('should correctly cast Date fields from timestamps as strings', function(){
var PizzaForm = forro({
'orderPlaced': forro.date()
}), form, when = new Date();

form = new PizzaForm({
'orderPlaced': when.getTime()
}).validate();

assert.equal(form.val('orderPlaced').toUTCString(), when.toUTCString());
});
});

0 comments on commit 5661ed5

Please sign in to comment.