1 |
|
2 | var coerceLiteral, coerceNestedLiteral, detectSuccessful;
|
3 |
|
4 | detectSuccessful = require('./utils').detectSuccessful;
|
5 |
|
6 | coerceLiteral = 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 |
|
26 | coerceNestedLiteral = 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 |
|
35 | module.exports = {
|
36 | coerceLiteral: coerceLiteral,
|
37 | coerceNestedLiteral: coerceNestedLiteral
|
38 | };
|