react / wstein / node_modules / browserify / node_modules / module-deps / node_modules / detective / node_modules / escodegen / node_modules / optionator / node_modules / levn / lib / coerce.js
80690 views// Generated by LiveScript 1.2.01(function(){2var parsedTypeCheck, types, toString$ = {}.toString;3parsedTypeCheck = require('type-check').parsedTypeCheck;4types = {5'*': function(it){6switch (toString$.call(it).slice(8, -1)) {7case 'Array':8return coerceType(it, {9type: 'Array'10});11case 'Object':12return coerceType(it, {13type: 'Object'14});15default:16return {17type: 'Just',18value: coerceTypes(it, [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], {41explicit: true42})43};44}45},46Undefined: function(it){47if (it === 'undefined' || it === void 8) {48return {49type: 'Just',50value: void 851};52} else {53return {54type: 'Nothing'55};56}57},58Null: function(it){59if (it === 'null') {60return {61type: 'Just',62value: null63};64} else {65return {66type: 'Nothing'67};68}69},70NaN: function(it){71if (it === 'NaN') {72return {73type: 'Just',74value: NaN75};76} else {77return {78type: 'Nothing'79};80}81},82Boolean: function(it){83if (it === 'true') {84return {85type: 'Just',86value: true87};88} else if (it === 'false') {89return {90type: 'Just',91value: false92};93} else {94return {95type: 'Nothing'96};97}98},99Number: function(it){100return {101type: 'Just',102value: +it103};104},105Int: function(it){106return {107type: 'Just',108value: parseInt(it)109};110},111Float: function(it){112return {113type: 'Just',114value: parseFloat(it)115};116},117Date: function(value, options){118var that;119if (that = /^\#(.*)\#$/.exec(value)) {120return {121type: 'Just',122value: new Date(+that[1] || that[1])123};124} else if (options.explicit) {125return {126type: 'Nothing'127};128} else {129return {130type: 'Just',131value: new Date(+value || value)132};133}134},135RegExp: function(value, options){136var that;137if (that = /^\/(.*)\/([gimy]*)$/.exec(value)) {138return {139type: 'Just',140value: new RegExp(that[1], that[2])141};142} else if (options.explicit) {143return {144type: 'Nothing'145};146} else {147return {148type: 'Just',149value: new RegExp(value)150};151}152},153Array: function(it){154return coerceArray(it, {155of: [{156type: '*'157}]158});159},160Object: function(it){161return coerceFields(it, {162of: {}163});164},165String: function(it){166var that;167if (toString$.call(it).slice(8, -1) !== 'String') {168return {169type: 'Nothing'170};171}172if (that = it.match(/^'(.*)'$/)) {173return {174type: 'Just',175value: that[1]176};177} else if (that = it.match(/^"(.*)"$/)) {178return {179type: 'Just',180value: that[1]181};182} else {183return {184type: 'Just',185value: it186};187}188}189};190function coerceArray(node, type){191var typeOf, element;192if (toString$.call(node).slice(8, -1) !== 'Array') {193return {194type: 'Nothing'195};196}197typeOf = type.of;198return {199type: 'Just',200value: (function(){201var i$, ref$, len$, results$ = [];202for (i$ = 0, len$ = (ref$ = node).length; i$ < len$; ++i$) {203element = ref$[i$];204results$.push(coerceTypes(element, typeOf));205}206return results$;207}())208};209}210function coerceTuple(node, type){211var result, i$, ref$, len$, i, types, that;212if (toString$.call(node).slice(8, -1) !== 'Array') {213return {214type: 'Nothing'215};216}217result = [];218for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {219i = i$;220types = ref$[i$];221if (that = coerceTypes(node[i], types)) {222result.push(that);223}224}225return {226type: 'Just',227value: result228};229}230function coerceFields(node, type){231var typeOf, key, value;232if (toString$.call(node).slice(8, -1) !== 'Object') {233return {234type: 'Nothing'235};236}237typeOf = type.of;238return {239type: 'Just',240value: (function(){241var ref$, results$ = {};242for (key in ref$ = node) {243value = ref$[key];244results$[key] = coerceTypes(value, typeOf[key] || [{245type: '*'246}]);247}248return results$;249}())250};251}252function coerceType(node, typeObj, options){253var type, structure, coerceFunc;254type = typeObj.type, structure = typeObj.structure;255if (type) {256coerceFunc = types[type];257return coerceFunc(node, options);258} else {259switch (structure) {260case 'array':261return coerceArray(node, typeObj);262case 'tuple':263return coerceTuple(node, typeObj);264case 'fields':265return coerceFields(node, typeObj);266}267}268}269function coerceTypes(node, types, options){270var i$, len$, type, ref$, valueType, value;271for (i$ = 0, len$ = types.length; i$ < len$; ++i$) {272type = types[i$];273ref$ = coerceType(node, type, options), valueType = ref$.type, value = ref$.value;274if (valueType === 'Nothing') {275continue;276}277if (parsedTypeCheck([type], value)) {278return value;279}280}281throw new Error("Value " + JSON.stringify(node) + " does not type check against " + JSON.stringify(types) + ".");282}283module.exports = coerceTypes;284}).call(this);285286287