Skip to content

Commit

Permalink
Merge pull request #24 from CodingCarlos/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
CodingCarlos authored Apr 14, 2018
2 parents fc758ec + c9a26f4 commit 11344ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
22 changes: 13 additions & 9 deletions lib/modifiers/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,37 @@
*/

function modify(data, model) {
if (!model.type) {
return data;
} else if(model.typeStrict == true) {
if (!model.type || !data || model.typeStrict == true) {
return data;
}

let parsed = data;

switch (model.type.toLowerCase()) {
case 'string':
parsed = (data) ? String(data) : '';
if (typeof data === 'object') {
parsed = JSON.stringify(data);
} else {
parsed = String(data);
}
break;
case 'number':
if (!isNaN(Number(data))) {
parsed = Number(data);
} else {
parsed = undefined;
}
break;
case 'boolean':
parsed = String(data).toLowerCase() === 'true';
break;
case 'object':
parsed = (typeof data === 'object') ? data : undefined;
break;
case 'array':
parsed = (Array.isArray(data)) ? data : undefined;
if(typeof data === 'string') {
try {
data = JSON.parse(data)
} catch (e) {
console.error('Tried to parse invalid json string to ' + model.type.toLowerCase());
}
}
break;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modelate",
"version": "0.2.8",
"version": "0.2.10",
"description": "A simple data modeling tool for NodeJS",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 7 additions & 4 deletions spec/modifiers/type.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var valid = require('../../lib/modifiers/type');

const str = 'Hello world';
const str = '42';
const obj = {hello: 'world'};
const num = 42;
const bool = true;
Expand Down Expand Up @@ -29,20 +29,23 @@ describe(' - Type modifier', () => {
for (let validType in typesKeys) {
let model = {type: typesKeys[validType]};
for (let check in typesKeys) {

// ToDo: ReWrite tests to clarify the behaviour in each use-case

it('shall only validate '+ typesKeys[validType] +' when model set, and '+ typesKeys[check] +' given', () => {
const modelated = valid(types[typesKeys[check]], model);
let shallBeValid = (typeof modelated === typesKeys[validType]);

if (typesKeys[validType] === 'object' && typeof modelated === 'undefined') {
if (typesKeys[validType] === 'object') { // && typeof modelated === 'undefined') {
shallBeValid = true;
}

if ((typesKeys[validType] === 'array' && Array.isArray(modelated))
|| (typesKeys[validType] === 'array' && typeof modelated === 'undefined')) {
|| (typesKeys[validType] === 'array')) { // && typeof modelated === 'undefined')) {
shallBeValid = true;
}

if (typesKeys[validType] === 'number' && typeof modelated === 'undefined') {
if (typesKeys[validType] === 'number') { // && typeof modelated === 'undefined') {
shallBeValid = true;
}

Expand Down

0 comments on commit 11344ac

Please sign in to comment.