react / wstein / node_modules / browserify / node_modules / module-deps / node_modules / detective / node_modules / escodegen / node_modules / optionator / lib / coerce.js
80621 views// Generated by LiveScript 1.2.01(function(){2var ref$, id, reject, parsedTypeCheck, types, tokenRegex, toString$ = {}.toString;3ref$ = require('prelude-ls'), id = ref$.id, reject = ref$.reject;4parsedTypeCheck = require('type-check').parsedTypeCheck;5types = {6'*': function(it){7switch (toString$.call(it).slice(8, -1)) {8case 'Array':9return coerceType(it, {10type: 'Array'11});12case 'Object':13return coerceType(it, {14type: 'Object'15});16default:17return {18type: 'Just',19value: coerceTypes(it, [20{21type: 'Undefined'22}, {23type: 'Null'24}, {25type: 'Boolean'26}, {27type: 'Number'28}, {29type: 'Date'30}, {31type: 'RegExp'32}, {33type: 'Array'34}, {35type: 'Object'36}, {37type: 'String'38}39], {40explicit: true41})42};43}44},45Undefined: function(it){46if (it === 'undefined') {47return {48type: 'Just',49value: void 850};51} else {52return {53type: 'Nothing'54};55}56},57Null: function(it){58if (it === 'null') {59return {60type: 'Just',61value: null62};63} else {64return {65type: 'Nothing'66};67}68},69Boolean: function(it){70if (it === 'true') {71return {72type: 'Just',73value: true74};75} else if (it === 'false') {76return {77type: 'Just',78value: false79};80} else {81return {82type: 'Nothing'83};84}85},86Number: function(it){87return {88type: 'Just',89value: +it90};91},92Int: function(it){93return {94type: 'Just',95value: parseInt(it)96};97},98Float: function(it){99return {100type: 'Just',101value: parseFloat(it)102};103},104Date: function(value, options){105var that;106if (that = /^\#(.*)\#$/.exec(value)) {107return {108type: 'Just',109value: new Date(+that[1] || that[1])110};111} else if (options.explicit) {112return {113type: 'Nothing'114};115} else {116return {117type: 'Just',118value: new Date(+value || value)119};120}121},122RegExp: function(value, options){123var that;124if (that = /^\/(.*)\/([gimy]*)$/.exec(value)) {125return {126type: 'Just',127value: new RegExp(that[1], that[2])128};129} else if (options.explicit) {130return {131type: 'Nothing'132};133} else {134return {135type: 'Just',136value: new RegExp(value)137};138}139},140Array: function(it){141return coerceArray(it, {142of: [{143type: '*'144}]145});146},147Object: function(it){148return coerceFields(it, {149of: {}150});151},152String: function(it){153var that;154if (that = it.match(/^'(.*)'$/)) {155return {156type: 'Just',157value: that[1]158};159} else if (that = it.match(/^"(.*)"$/)) {160return {161type: 'Just',162value: that[1]163};164} else {165return {166type: 'Just',167value: it168};169}170}171};172function coerceArray(node, type){173var typeOf, element;174if (toString$.call(node).slice(8, -1) !== 'Array') {175return {176type: 'Nothing'177};178}179typeOf = type.of;180return {181type: 'Just',182value: (function(){183var i$, ref$, len$, results$ = [];184for (i$ = 0, len$ = (ref$ = node).length; i$ < len$; ++i$) {185element = ref$[i$];186results$.push(coerceTypes(element, typeOf));187}188return results$;189}())190};191}192function coerceTuple(node, type){193var i, types;194if (toString$.call(node).slice(8, -1) !== 'Array') {195return {196type: 'Nothing'197};198}199return {200type: 'Just',201value: (function(){202var i$, ref$, len$, results$ = [];203for (i$ = 0, len$ = (ref$ = type.of).length; i$ < len$; ++i$) {204i = i$;205types = ref$[i$];206results$.push(coerceTypes(node[i], types));207}208return results$;209}())210};211}212function coerceFields(node, type){213var typeOf, key, value;214if (toString$.call(node).slice(8, -1) !== 'Object') {215return {216type: 'Nothing'217};218}219typeOf = type.of;220return {221type: 'Just',222value: (function(){223var ref$, results$ = {};224for (key in ref$ = node) {225value = ref$[key];226results$[key] = coerceTypes(value, typeOf[key] || [{227type: '*'228}]);229}230return results$;231}())232};233}234function coerceType(node, typeObj, options){235var type, structure, coerceFunc;236type = typeObj.type, structure = typeObj.structure;237if (type) {238coerceFunc = types[type];239return coerceFunc(node, options);240} else {241switch (structure) {242case 'array':243return coerceArray(node, typeObj);244case 'tuple':245return coerceTuple(node, typeObj);246case 'fields':247return coerceFields(node, typeObj);248}249}250}251function coerceTypes(node, types, options){252var i$, len$, type, ref$, valueType, value;253options == null && (options = {});254for (i$ = 0, len$ = types.length; i$ < len$; ++i$) {255type = types[i$];256ref$ = coerceType(node, type, options), valueType = ref$.type, value = ref$.value;257if (valueType === 'Nothing') {258continue;259}260if (parsedTypeCheck([type], value)) {261return value;262}263}264throw new Error("Value '" + node + "' does not type check against " + JSON.stringify(types) + ".");265}266function consumeOp(tokens, op){267if (tokens[0] === op) {268return tokens.shift();269} else {270throw new Error("Expected '" + op + "', but got " + tokens[0] + " instead.");271}272}273function maybeConsumeOp(tokens, op){274if (tokens[0] === op) {275return tokens.shift();276}277}278function consumeList(tokens, delimiters, hasDelimiters){279var result;280if (hasDelimiters) {281consumeOp(tokens, delimiters[0]);282}283result = [];284while (tokens.length && tokens[0] !== delimiters[1]) {285result.push(consumeElement(tokens));286maybeConsumeOp(tokens, ',');287}288if (hasDelimiters) {289consumeOp(tokens, delimiters[1]);290}291return result;292}293function consumeArray(tokens, hasDelimiters){294return consumeList(tokens, ['[', ']'], hasDelimiters);295}296function consumeTuple(tokens, hasDelimiters){297return consumeList(tokens, ['(', ')'], hasDelimiters);298}299function consumeFields(tokens, hasDelimiters){300var result, key;301if (hasDelimiters) {302consumeOp(tokens, '{');303}304result = {};305while (tokens.length && (!hasDelimiters || tokens[0] !== '}')) {306key = tokens.shift();307consumeOp(tokens, ':');308result[key] = consumeElement(tokens);309maybeConsumeOp(tokens, ',');310}311if (hasDelimiters) {312consumeOp(tokens, '}');313}314return result;315}316function consumeElement(tokens){317switch (tokens[0]) {318case '[':319return consumeArray(tokens, true);320case '(':321return consumeTuple(tokens, true);322case '{':323return consumeFields(tokens, true);324default:325return tokens.shift();326}327}328function consumeTopLevel(tokens, types){329var structure, origTokens, result;330structure = types[0].structure;331if (types.length === 1 && structure) {332origTokens = tokens.slice();333result = structure === 'array'334? consumeArray(tokens, tokens[0] === '[')335: structure === 'tuple'336? consumeTuple(tokens, tokens[0] === '(')337: consumeFields(tokens, tokens[0] === '{');338if (tokens.length) {339return consumeElement(structure === 'array'340? ['['].concat(origTokens, [']'])341: ['('].concat(origTokens, [')']));342} else {343return result;344}345} else {346return consumeElement(tokens);347}348}349tokenRegex = /("(?:[^"]|\\")*")|('(?:[^']|\\')*')|(#.*#)|(\/(?:\\\/|[^\/])*\/[gimy]*)|([\[\]\(\)}{:,])|([-\.\$\w]+)|\s*/;350function coerce(types, string){351var tokens, node;352tokens = reject(function(it){353return !it || /^\s+$/.test(it);354}, string.split(tokenRegex));355node = consumeTopLevel(tokens, types);356if (!node) {357throw new Error("Error parsing " + string);358}359return coerceTypes(node, types);360}361module.exports = coerce;362/*363function log364console.log it; it365*/366}).call(this);367368369