Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Refactor and rework http coercion." #269

Merged
merged 1 commit into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 32 additions & 59 deletions lib/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Dynamic(val, ctx) {
* Object containing converter functions.
*/

Dynamic.converters = {};
Dynamic.converters = [];

/**
* Define a named type conversion. The conversion is used when a
Expand All @@ -46,7 +46,7 @@ Dynamic.converters = {};

Dynamic.define = function(name, converter) {
converter.typeName = name;
this.converters[name] = converter;
this.converters.unshift(converter);
};

/**
Expand All @@ -63,36 +63,19 @@ Dynamic.canConvert = function(type) {
/**
* Get converter by type name.
*
* If passed an array, will return an array, all coerced to the single
* item in the array. More than one type in an array is not supported.
*
* @param {String|Array} type
* @param {String} type
* @returns {Function}
*/

Dynamic.getConverter = function(type) {
if (Array.isArray(type) && this.converters[type[0]]) {
if (type.length !== 1) {
throw new Error('Coercing to an array type with more than 1 value is unsupported.');
var converters = this.converters;
var converter;
for (var i = 0; i < converters.length; i++) {
converter = converters[i];
if (converter.typeName === type) {
return converter;
}
return this.getArrayConverter.bind(this, this.converters[type[0]]);
}
return this.converters[type];
};

/**
* If the type passed is an array, get a converter that returns an array.
* Type coercion will be one layer deep: ['a', 2, ['c', 4]] with type
* ['string'] coerces to ['a', '2', '[c,4]'].
* @param {Function} converter Non-array converter fn.
* @param {*} val The value object
* @param {Context} ctx The Remote Context
* @return {Function} Converter
*/
Dynamic.getArrayConverter = function(converter, val, ctx) {
if (!Array.isArray(val)) val = this.converters.array(val, ctx);
return val.map(function(v) {
return converter(v, ctx);
});
};

/**
Expand All @@ -112,39 +95,29 @@ Dynamic.prototype.to = function(type) {
* Built in type converters...
*/

Dynamic.define('boolean', function convertToBoolean(val) {
if (val == null) return val;
return Boolean(val);
Dynamic.define('boolean', function convertBoolean(val) {
switch (typeof val) {
case 'string':
switch (val) {
case 'false':
case 'undefined':
case 'null':
case '0':
case '':
return false;
default:
return true;
}
break;
case 'number':
return val !== 0;
default:
return Boolean(val);
}
});

Dynamic.define('number', function convertToNumber(val) {
if (val == null) return val;
Dynamic.define('number', function convertNumber(val) {
if (val === 0) return val;
if (!val) return val;
return Number(val);
});

Dynamic.define('integer', function convertToInteger(val) {
if (val == null) return val;
return Math.floor(Dynamic.getConverter('number')(val));
});

Dynamic.define('string', function convertToString(val) {
if (val == null) return val;
if (typeof val === 'string') return val;
if (typeof val.toString === 'function' &&
val.toString !== Object.prototype.toString) return val.toString();
if (val && typeof val === 'object') return JSON.stringify(val);
throw new Error('Could not properly convert ' + val + ' to a string.');
});

Dynamic.define('array', function convertToArray(val, ctx) {
if (val == null || val === '') return [];
if (Array.isArray(val)) return val;

// This is not a sloppy conversion, so just wrap in array if it isn't already one.
return [val];
});

// Defined so we can use a type like ['any']
Dynamic.define('any', function noop(val) {
return val;
});
104 changes: 0 additions & 104 deletions lib/http-coerce.js

This file was deleted.

Loading