UNPKG

1.19 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.7.1
2var coerceLiteral, coerceNestedLiteral, detectSuccessful;
3
4detectSuccessful = require('./utils').detectSuccessful;
5
6coerceLiteral = function(literal, typeName, cb) {
7 switch (typeName) {
8 case 'number':
9 if (isNaN(literal)) {
10 return cb(new Error("Literal '" + literal + "' is not a number."));
11 }
12 return cb(null, parseFloat(literal, 10));
13 case 'boolean':
14 if (literal !== 'true' && literal !== 'false') {
15 return cb(new Error("Literal '" + literal + "' is not 'true' or 'false'."));
16 }
17 return cb(null, literal === 'true');
18 default:
19 if (typeName === 'string' || !typeName) {
20 return cb(null, literal);
21 }
22 return cb(new Error("Literal '" + literal + "' can't have type '" + typeName + "'."));
23 }
24};
25
26coerceNestedLiteral = function(literal, typeNames, cb) {
27 if (!(typeNames != null ? typeNames.length : void 0)) {
28 typeNames = [void 0];
29 }
30 return detectSuccessful(typeNames, function(typeName, next) {
31 return coerceLiteral(literal, typeName, next);
32 }, cb);
33};
34
35module.exports = {
36 coerceLiteral: coerceLiteral,
37 coerceNestedLiteral: coerceNestedLiteral
38};