Skip to content

Commit

Permalink
feat($types): implemented env.types.String
Browse files Browse the repository at this point in the history
  • Loading branch information
FGRibreau committed Nov 4, 2015
1 parent fc0582e commit 9da7886
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
19 changes: 17 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';
const _ = require('lodash');

module.exports = {
seems: {
Integer: seemsInteger,
Boolean: seemsBoolean
Boolean: seemsBoolean,
String: seemsString
},
convert: {
Integer: Integer,
Boolean: Boolean
Boolean: Boolean,
String: String,
}
};

Expand Down Expand Up @@ -36,3 +39,15 @@ function seemsBoolean(mixed) {
var v = String(mixed).toLowerCase();
return v === 'true' || v === 'false';
}

function String(mixed){
if(!seemsString(mixed)){
throw new Error('`' + mixed + '` is not an string, could not convert it.');
}

return mixed;
}

function seemsString(mixed){
return _.isString(mixed);
}
60 changes: 31 additions & 29 deletions test/getOrElseAll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,17 @@ describe('.getOrElseAll', function () {
converter: env.types.Boolean,
val:'oskdoskd',
converted:Error
}, {
converter: env.types.String,
val:'string',
converted:'string'
},{
converter: env.types.String,
val:undefined,
converted:Error
},
// {
// converter: 'STRING',
// env:'string',
// converted:'string'
// },{
// converter: 'String',
// converter: env.types.Array(env.types.String),
// env:'a,b,c,d'
// converted:['a', 'b', 'c', 'd']
// }
Expand All @@ -255,41 +259,39 @@ describe('.getOrElseAll', function () {
_.forEach(tests, (v) => delete process.env[v.varName]);
});



_.forEach(tests, (v) => {

describe(v.converter.name + ' (e.g. '+v.val+')', () => {
describe('converter ' + v.converter.name + ' with value '+ JSON.stringify(v.val), () => {
it('should be defined as a function', () => {
t.ok(_.isFunction(v.converter), v.converter.name + ' should be a function');
});
});

if (v.converted === Error) {
it('should throw an error when the converter ' + v.converter.name + ' does not receive a good value (e.g. with ' + v.val + ')', () => {
if (v.converted === Error) {
it('should throw an error when the converter ' + v.converter.name + ' does not receive a good value (e.g. with ' + v.val + ')', () => {

t.throws(() => {
env.getOrElseAll({
a: {
$type: v.converter,
$aliases: [v.varName]
}
t.throws(() => {
env.getOrElseAll({
a: {
$type: v.converter,
$aliases: [v.varName]
}
});
});
});
});
return;
}
return;
}

it('should handle ' + v.converter.name + ' converter as $type (e.g. with ' + JSON.stringify(v.val) + ')', () => {
var config = env.getOrElseAll({
a: {
$type: v.converter,
$aliases: [v.varName]
}
});
it('should handle ' + v.converter.name + ' converter as $type (e.g. with ' + JSON.stringify(v.val) + ')', () => {
var config = env.getOrElseAll({
a: {
$type: v.converter,
$aliases: [v.varName]
}
});

t.strictEqual(config.a, v.converted);
t.strictEqual(config.a, v.converted);
});
});

});
});
});
Expand Down

0 comments on commit 9da7886

Please sign in to comment.