Created
August 31, 2013 12:43
-
-
Save phadej/6397957 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var typify = require("typify"); | |
function checklog(type, v, expected) { | |
var result = typify.check(type,v); | |
if (result !== expected) { | |
console.error(type, v, result, expected); | |
} | |
} | |
var aNumber = 123; | |
var anArray = [123, null, false, {"foo": "bar"}]; | |
var anArrayOfInts = [1, 2, 3, 4, 5]; | |
var anArrayOfThreeBooleans = [false, false, true]; | |
var anObject = {"a": 123, "b": true}; | |
typify.type("integer", function (v) { | |
return v === (v|0); | |
}); | |
typify.type("nat", function (v) { | |
return v === (v|0) && v >= 0; | |
}); | |
typify.type("tuple", function (v) { | |
if (!Array.isArray(v)) { return false; } | |
var args = Array.prototype.slice.call(arguments, 1); | |
if (args.length !== v.length) { return false; } | |
for (var i = 0; i < args.length; i++) { | |
if (!args[i](v[i])) return false; | |
} | |
return true; | |
}); | |
typify.type("object", function (v) { | |
return new Object(v) === v; | |
}); | |
checklog("number", aNumber, true); | |
// checklog("123", 123, true); | |
checklog("number", "123", false); | |
checklog("number", anArray, false); | |
checklog("array", [], true); | |
checklog("array", {}, false); | |
checklog("array integer", anArrayOfInts, true); | |
checklog("array number", anArrayOfInts, true); | |
checklog("array boolean", anArrayOfInts, false); | |
checklog("array boolean", anArrayOfThreeBooleans, true); | |
checklog("tuple boolean boolean boolean", anArrayOfThreeBooleans, true); | |
checklog("tuple boolean boolean", anArrayOfThreeBooleans, false); | |
checklog("object", anObject, true); | |
checklog("object", {}, true); | |
// checklog("{ a : integer }", anObject, true); | |
// checklog("{ b : integer }", anObject, false); | |
// ----------------------------------------------------------------------------- | |
typify.type("foo", function (v) { | |
return v === "foo"; | |
}); | |
typify.record("nested", { | |
"a": "foo?" | |
}); | |
typify.record("complicated", { | |
"bool": "boolean", | |
"missing": "string | null", | |
"arrays": "array (array integer)", | |
"nested": "nested", | |
}); | |
checklog("complicated", { | |
"bool": true, | |
"missing": null, | |
"arrays": [[0], [], [1, 2]], | |
"nested": { | |
"a": "foo", | |
"b": "bar" | |
} | |
}, true); | |
checklog("complicated", { | |
"bool": true, | |
"arrays": [[0], [null], [1, 2]], | |
"nested": { | |
"a": "bar", | |
} | |
}, false); | |
// ----------------------------------------------------------------------------- | |
var people = {"people": [ | |
{"name": "Adam", "age": 10, "pet": "dog"}, | |
{"name": "Beth", "age": 11}, | |
{"name": "Charles", "age": 12, "pet": null} | |
]}; | |
typify.record("person", { | |
"name": "string", | |
"age": "nat", // non-negative integer | |
"pet": "string? | null", | |
}); | |
typify.record("people", { | |
"people": "array person", | |
}); | |
checklog("people", people, true) | |
/* No support for literals or inline-records yet | |
match({"people": [ | |
{ | |
"name": string, | |
"age": function (years) { return years % 2 == 0; }, | |
"pet": or("cat", "dog", null) | |
} | |
]}, people).result; //=> false | |
match({"people": [ | |
{ | |
"name": string, | |
"age": function (years) { return years % 2 == 0; }, | |
"pet": or("cat", "dog", null) | |
} | |
]}, people).log(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment