|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- // Generated by LiveScript 1.4.0
- (function(){
- var parsedTypeCheck, types, toString$ = {}.toString;
- parsedTypeCheck = require('type-check').parsedTypeCheck;
- types = {
- '*': function(value, options){
- switch (toString$.call(value).slice(8, -1)) {
- case 'Array':
- return typeCast(value, {
- type: 'Array'
- }, options);
- case 'Object':
- return typeCast(value, {
- type: 'Object'
- }, options);
- default:
- return {
- type: 'Just',
- value: typesCast(value, [
- {
- type: 'Undefined'
- }, {
- type: 'Null'
- }, {
- type: 'NaN'
- }, {
- type: 'Boolean'
- }, {
- type: 'Number'
- }, {
- type: 'Date'
- }, {
- type: 'RegExp'
- }, {
- type: 'Array'
- }, {
- type: 'Object'
- }, {
- type: 'String'
- }
- ], (options.explicit = true, options))
- };
- }
- },
- Undefined: function(it){
- if (it === 'undefined' || it === void 8) {
- return {
- type: 'Just',
- value: void 8
- };
- } else {
- return {
- type: 'Nothing'
- };
- }
- },
- Null: function(it){
- if (it === 'null') {
- return {
- type: 'Just',
- value: null
- };
- } else {
- return {
- type: 'Nothing'
- };
- }
- },
- NaN: function(it){
- if (it === 'NaN') {
- return {
- type: 'Just',
- value: NaN
- };
- } else {
- return {
- type: 'Nothing'
- };
- }
- },
- Boolean: function(it){
- if (it === 'true') {
- return {
- type: 'Just',
- value: true
- };
- } else if (it === 'false') {
- return {
- type: 'Just',
- value: false
- };
- } else {
- return {
- type: 'Nothing'
- };
- }
- },
- Number: function(it){
- return {
- type: 'Just',
- value: +it
- };
- },
- Int: function(it){
- return {
- type: 'Just',
- value: +it
- };
- },
- Float: function(it){
- return {
- type: 'Just',
- value: +it
- };
- },
- Date: function(value, options){
- var that;
- if (that = /^\#([\s\S]*)\#$/.exec(value)) {
- return {
- type: 'Just',
- value: new Date(+that[1] || that[1])
- };
- } else if (options.explicit) {
- return {
- type: 'Nothing'
- };
- } else {
- return {
- type: 'Just',
- value: new Date(+value || value)
- };
- }
- },
- RegExp: function(value, options){
- var that;
- if (that = /^\/([\s\S]*)\/([gimy]*)$/.exec(value)) {
- return {
- type: 'Just',
- value: new RegExp(that[1], that[2])
- };
- } else if (options.explicit) {
- return {
- type: 'Nothing'
- };
- } else {
- return {
- type: 'Just',
- value: new RegExp(value)
- };
- }
- },
- Array: function(value, options){
- return castArray(value, {
- of: [{
- type: '*'
- }]
- }, options);
- },
- Object: function(value, options){
- return castFields(value, {
- of: {}
- }, options);
- },
- String: function(it){
- var that;
- if (toString$.call(it).slice(8, -1) !== 'String') {
- return {
- type: 'Nothing'
- };
- }
- if (that = it.match(/^'([\s\S]*)'$/)) {
- return {
- type: 'Just',
- value: that[1].replace(/\\'/g, "'")
- };
- } else if (that = it.match(/^"([\s\S]*)"$/)) {
- return {
- type: 'Just',
- value: that[1].replace(/\\"/g, '"')
- };
- } else {
- return {
- type: 'Just',
- value: it
- };
- }
- }
- };
- function castArray(node, type, options){
- var typeOf, element;
- if (toString$.call(node).slice(8, -1) !== 'Array') {
- return {
- type: 'Nothing'
- };
- }
- typeOf = type.of;
- return {
- type: 'Just',
- value: (function(){
- var i$, ref$, len$, results$ = [];
- for (i$ = 0, len$ = (ref$ = node).length; i$ < len$; ++i$) {
- element = ref$[i$];
- results$.push(typesCast(element, typeOf, options));
- }
- return results$;
- }())
- };
- }
- function castTuple(node, type, options){
- var result, i, i$, ref$, len$, types, cast;
- if (toString$.call(node).slice(8, -1) !== 'Array') {
- return {
- type: 'Nothing'
- };
- }
- result = [];
- i = 0;
- for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {
- types = ref$[i$];
- cast = typesCast(node[i], types, options);
- if (toString$.call(cast).slice(8, -1) !== 'Undefined') {
- result.push(cast);
- }
- i++;
- }
- if (node.length <= i) {
- return {
- type: 'Just',
- value: result
- };
- } else {
- return {
- type: 'Nothing'
- };
- }
- }
- function castFields(node, type, options){
- var typeOf, key, value;
- if (toString$.call(node).slice(8, -1) !== 'Object') {
- return {
- type: 'Nothing'
- };
- }
- typeOf = type.of;
- return {
- type: 'Just',
- value: (function(){
- var ref$, resultObj$ = {};
- for (key in ref$ = node) {
- value = ref$[key];
- resultObj$[typesCast(key, [{
- type: 'String'
- }], options)] = typesCast(value, typeOf[key] || [{
- type: '*'
- }], options);
- }
- return resultObj$;
- }())
- };
- }
- function typeCast(node, typeObj, options){
- var type, structure, castFunc, ref$;
- type = typeObj.type, structure = typeObj.structure;
- if (type) {
- castFunc = ((ref$ = options.customTypes[type]) != null ? ref$.cast : void 8) || types[type];
- if (!castFunc) {
- throw new Error("Type not defined: " + type + ".");
- }
- return castFunc(node, options, typesCast);
- } else {
- switch (structure) {
- case 'array':
- return castArray(node, typeObj, options);
- case 'tuple':
- return castTuple(node, typeObj, options);
- case 'fields':
- return castFields(node, typeObj, options);
- }
- }
- }
- function typesCast(node, types, options){
- var i$, len$, type, ref$, valueType, value;
- for (i$ = 0, len$ = types.length; i$ < len$; ++i$) {
- type = types[i$];
- ref$ = typeCast(node, type, options), valueType = ref$.type, value = ref$.value;
- if (valueType === 'Nothing') {
- continue;
- }
- if (parsedTypeCheck([type], value, {
- customTypes: options.customTypes
- })) {
- return value;
- }
- }
- throw new Error("Value " + JSON.stringify(node) + " does not type check against " + JSON.stringify(types) + ".");
- }
- module.exports = typesCast;
- }).call(this);
|