Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #27 from XadillaX/feature/boolean
Feature/boolean
  • Loading branch information
XadillaX committed Nov 2, 2015
2 parents 3ae37d2 + 60a95d2 commit fca5f18
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 219 deletions.
45 changes: 45 additions & 0 deletions lib/fieldType/boolean.js
@@ -0,0 +1,45 @@
/**
* XadillaX created at 2015-11-02 10:40:53 With ♥
*
* Copyright (c) 2015 Souche.com, all rights
* reserved.
*/
"use strict";

var _Boolean = {};
_Boolean.name = "_Boolean";
_Boolean.needQuotes = false;

/**
* restore
* @param {_Boolean} parsed the parsed integer
* @return {Number} the integer restored
*/
_Boolean.restore = function(parsed) {
return (parsed ^ 0) & 1;
};

/**
* parse
* @param {Number} orig the original integer
* @return {_Boolean} the boolean value
*/
_Boolean.parse = function(orig) {
return !!orig;
};

_Boolean.defaultValue = 0;

/**
* equal
* @param {Number|Boolean} a integer I
* @param {Number|Boolean} b integer II
* @return {Boolean} whether they are equal
*/
_Boolean.equal = function(a, b) {
a = !!a;
b = !!b;
return a === b;
};

module.exports = _Boolean;
2 changes: 2 additions & 0 deletions lib/fieldType/index.js
Expand Up @@ -7,13 +7,15 @@
"use strict";

var _String = require("./string");
var _Boolean = require("./boolean");
var Integer = require("./integer");
var Float = require("./float");
var Json = require("./json");
var Datetime = require("./datetime");

module.exports = {
String : _String,
Boolean : _Boolean,
Integer : Integer,
Float : Float,
Json : Json,
Expand Down
219 changes: 0 additions & 219 deletions npm-shrinkwrap.json

This file was deleted.

57 changes: 57 additions & 0 deletions test/field.js
Expand Up @@ -48,6 +48,63 @@ describe("Field",function () {
});
});
});

describe("Boolean type", function () {
var _Boolean = field.Boolean;
describe("#restore", function () {
it("arg Integer", function () {
(_Boolean.restore(1)).should.equal(1);
(_Boolean.restore(-1)).should.equal(1);
});
it("arg Float", function () {
(_Boolean.restore(1.1)).should.equal(1);
(_Boolean.restore(-1.1)).should.equal(1);
});
it("arg string", function () {
(_Boolean.restore("1.441")).should.equal(1);
(_Boolean.restore("-1.1")).should.equal(1);
(_Boolean.restore("-1")).should.equal(1);
(_Boolean.restore("1")).should.equal(1);
});
it("arg boolean", function() {
(_Boolean.restore(true)).should.equal(1);
(_Boolean.restore(false)).should.equal(0);
});
});
describe("#parse", function () {
it("arg Integer", function () {
(_Boolean.parse(1)).should.equal(true);
(_Boolean.parse(-1)).should.equal(true);
(_Boolean.parse(0)).should.equal(false);
});
it("arg Float", function () {
(_Boolean.parse(1.1)).should.equal(true);
(_Boolean.parse(-1.1)).should.equal(true);
});
it("arg string", function () {
(_Boolean.parse("1.441")).should.equal(true);
(_Boolean.parse("-1.1")).should.equal(true);
(_Boolean.parse("-1")).should.equal(true);
});
});
describe("#equal", function () {
it("should return true", function () {
(_Boolean.equal("123.1", 123)).should.be.true;
(_Boolean.equal(123.1, 123)).should.be.true;
(_Boolean.equal(0, 0)).should.be.true;
(_Boolean.equal("-1", -1)).should.be.true;
(_Boolean.equal(-1.1, -1)).should.be.true;
});
it("should return false", function () {
(_Boolean.equal("124.1", false)).should.not.be.true;
(_Boolean.equal(124.1, "")).should.not.be.true;
(_Boolean.equal(3, 0)).should.not.be.true;
(_Boolean.equal(true, false)).should.not.be.true;
(_Boolean.equal(true, 0)).should.not.be.true;
});
});
});

describe("Integer type", function () {
var Integer = field.Integer;
describe("#restore", function () {
Expand Down

0 comments on commit fca5f18

Please sign in to comment.