react / wstein / node_modules / browserify / node_modules / module-deps / node_modules / detective / node_modules / escodegen / node_modules / optionator / node_modules / levn / lib / cast.js
80690 views// Generated by LiveScript 1.2.01(function(){2var parsedTypeCheck, types, toString$ = {}.toString;3parsedTypeCheck = require('type-check').parsedTypeCheck;4types = {5'*': function(value, options){6switch (toString$.call(value).slice(8, -1)) {7case 'Array':8return typeCast(value, {9type: 'Array'10}, options);11case 'Object':12return typeCast(value, {13type: 'Object'14}, options);15default:16return {17type: 'Just',18value: typesCast(value, [19{20type: 'Undefined'21}, {22type: 'Null'23}, {24type: 'NaN'25}, {26type: 'Boolean'27}, {28type: 'Number'29}, {30type: 'Date'31}, {32type: 'RegExp'33}, {34type: 'Array'35}, {36type: 'Object'37}, {38type: 'String'39}40], (options.explicit = true, options))41};42}43},44Undefined: function(it){45if (it === 'undefined' || it === void 8) {46return {47type: 'Just',48value: void 849};50} else {51return {52type: 'Nothing'53};54}55},56Null: function(it){57if (it === 'null') {58return {59type: 'Just',60value: null61};62} else {63return {64type: 'Nothing'65};66}67},68NaN: function(it){69if (it === 'NaN') {70return {71type: 'Just',72value: NaN73};74} else {75return {76type: 'Nothing'77};78}79},80Boolean: function(it){81if (it === 'true') {82return {83type: 'Just',84value: true85};86} else if (it === 'false') {87return {88type: 'Just',89value: false90};91} else {92return {93type: 'Nothing'94};95}96},97Number: function(it){98return {99type: 'Just',100value: +it101};102},103Int: function(it){104return {105type: 'Just',106value: parseInt(it)107};108},109Float: function(it){110return {111type: 'Just',112value: parseFloat(it)113};114},115Date: function(value, options){116var that;117if (that = /^\#([\s\S]*)\#$/.exec(value)) {118return {119type: 'Just',120value: new Date(+that[1] || that[1])121};122} else if (options.explicit) {123return {124type: 'Nothing'125};126} else {127return {128type: 'Just',129value: new Date(+value || value)130};131}132},133RegExp: function(value, options){134var that;135if (that = /^\/([\s\S]*)\/([gimy]*)$/.exec(value)) {136return {137type: 'Just',138value: new RegExp(that[1], that[2])139};140} else if (options.explicit) {141return {142type: 'Nothing'143};144} else {145return {146type: 'Just',147value: new RegExp(value)148};149}150},151Array: function(value, options){152return castArray(value, {153of: [{154type: '*'155}]156}, options);157},158Object: function(value, options){159return castFields(value, {160of: {}161}, options);162},163String: function(it){164var that;165if (toString$.call(it).slice(8, -1) !== 'String') {166return {167type: 'Nothing'168};169}170if (that = it.match(/^'([\s\S]*)'$/)) {171return {172type: 'Just',173value: that[1].replace(/\\'/g, "'")174};175} else if (that = it.match(/^"([\s\S]*)"$/)) {176return {177type: 'Just',178value: that[1].replace(/\\"/g, '"')179};180} else {181return {182type: 'Just',183value: it184};185}186}187};188function castArray(node, type, options){189var typeOf, element;190if (toString$.call(node).slice(8, -1) !== 'Array') {191return {192type: 'Nothing'193};194}195typeOf = type.of;196return {197type: 'Just',198value: (function(){199var i$, ref$, len$, results$ = [];200for (i$ = 0, len$ = (ref$ = node).length; i$ < len$; ++i$) {201element = ref$[i$];202results$.push(typesCast(element, typeOf, options));203}204return results$;205}())206};207}208function castTuple(node, type, options){209var result, i, i$, ref$, len$, types, cast;210if (toString$.call(node).slice(8, -1) !== 'Array') {211return {212type: 'Nothing'213};214}215result = [];216i = 0;217for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {218types = ref$[i$];219cast = typesCast(node[i], types, options);220if (toString$.call(cast).slice(8, -1) !== 'Undefined') {221result.push(cast);222}223i++;224}225if (node.length <= i) {226return {227type: 'Just',228value: result229};230} else {231return {232type: 'Nothing'233};234}235}236function castFields(node, type, options){237var typeOf, key, value;238if (toString$.call(node).slice(8, -1) !== 'Object') {239return {240type: 'Nothing'241};242}243typeOf = type.of;244return {245type: 'Just',246value: (function(){247var ref$, results$ = {};248for (key in ref$ = node) {249value = ref$[key];250results$[typesCast(key, [{251type: 'String'252}], options)] = typesCast(value, typeOf[key] || [{253type: '*'254}], options);255}256return results$;257}())258};259}260function typeCast(node, typeObj, options){261var type, structure, castFunc, ref$;262type = typeObj.type, structure = typeObj.structure;263if (type) {264castFunc = ((ref$ = options.customTypes[type]) != null ? ref$.cast : void 8) || types[type];265if (!castFunc) {266throw new Error("Type not defined: " + type + ".");267}268return castFunc(node, options, typesCast);269} else {270switch (structure) {271case 'array':272return castArray(node, typeObj, options);273case 'tuple':274return castTuple(node, typeObj, options);275case 'fields':276return castFields(node, typeObj, options);277}278}279}280function typesCast(node, types, options){281var i$, len$, type, ref$, valueType, value;282for (i$ = 0, len$ = types.length; i$ < len$; ++i$) {283type = types[i$];284ref$ = typeCast(node, type, options), valueType = ref$.type, value = ref$.value;285if (valueType === 'Nothing') {286continue;287}288if (parsedTypeCheck([type], value, {289customTypes: options.customTypes290})) {291return value;292}293}294throw new Error("Value " + JSON.stringify(node) + " does not type check against " + JSON.stringify(types) + ".");295}296module.exports = typesCast;297}).call(this);298299300