Browse Source

extract truthy sanitizer to own file

pull/248/head
Stephen Hess 9 years ago
parent
commit
55e31af238
  1. 10
      sanitiser/_details.js
  2. 9
      sanitiser/_truthy.js
  3. 1
      test/unit/run.js
  4. 31
      test/unit/sanitiser/_truthy.js

10
sanitiser/_details.js

@ -1,4 +1,5 @@
var isObject = require('is-object');
var isTruthy = require('./_truthy');
// validate inputs, convert types and apply defaults
function sanitize( req, default_value ){
@ -27,13 +28,4 @@ function sanitize( req, default_value ){
}
function isTruthy(val) {
if (typeof val === 'string') {
return ['true', '1', 'yes', 'y'].indexOf(val) !== -1;
}
return val === 1 || val === true;
}
// export function
module.exports = sanitize;

9
sanitiser/_truthy.js

@ -0,0 +1,9 @@
function isTruthy(val) {
if (typeof val === 'string') {
return ['true', '1', 'yes', 'y'].indexOf(val) !== -1;
}
return val === 1 || val === true;
}
module.exports = isTruthy;

1
test/unit/run.js

@ -10,6 +10,7 @@ var tests = [
require('./service/search'),
require('./sanitiser/_details'),
require('./sanitiser/_source'),
require('./sanitiser/_truthy'),
require('./sanitiser/search'),
require('./sanitiser/reverse'),
require('./sanitiser/place'),

31
test/unit/sanitiser/_truthy.js

@ -0,0 +1,31 @@
var isTruthy = require('../../../sanitiser/_truthy');
module.exports.tests = {};
module.exports.tests.sanitize_truthy = function(test, common) {
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(value) {
test('truthy value ' + value, function(t) {
t.equal(isTruthy(value), true, 'returns true');
t.end();
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n', null, -1, 123, NaN, 'abc'];
valid_false_values.forEach(function(value) {
test('falsey value ' + value, function(t) {
t.equal(isTruthy(value), false, 'returns false');
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _truthy ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
Loading…
Cancel
Save