Skip to content

Commit

Permalink
feat(helpers): add typing map functions ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 24, 2020
1 parent b84dea1 commit 18a7d66
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
39 changes: 32 additions & 7 deletions lib/helpers/montaineType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const AppError = require(path.resolve('./lib/helpers/AppError'));
const tricks = require(path.resolve('./lib/helpers/tricks'));
const typingDates = require(path.resolve('./lib/helpers/typing/dates'));
const typingTypes = require(path.resolve('./lib/helpers/typing/types'));
const typingMap = require(path.resolve('./lib/helpers/typing/map'));

/**
* @desc Function to format data
Expand All @@ -29,31 +30,55 @@ const format = (data, action, object) => {
try {
return typingDates.DATE(data);
} catch (err) {
throw new AppError('Typing : format DATE error', { code: 'HELPERS_ERROR', details: err });
throw new AppError(`Typing : format DATE error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
case 'DATE_NEXT_DAY':
try {
return typingDates.DATE_NEXT_DAY(data);
} catch (err) {
throw new AppError('Typing : format DATE_NEXT_DAY error', { code: 'HELPERS_ERROR', details: err });
throw new AppError(`Typing : format DATE_NEXT_DAY error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
case 'HOUR':
try {
return typingDates.HOUR(data, params, object);
} catch (err) {
throw new AppError(`Typing : format HOURS error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
case 'NUMBER':
try {
return typingTypes.NUMBER(data);
} catch (err) {
throw new AppError('Typing : format NUMBER error', { code: 'HELPERS_ERROR', details: err });
throw new AppError(`Typing : format NUMBER error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
case 'STRING':
try {
return typingTypes.STRING(data);
} catch (err) {
throw new AppError('Typing : format STRING error', { code: 'HELPERS_ERROR', details: err });
throw new AppError(`Typing : format STRING error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
case 'HOUR':
case 'STRING_MAP_TO_STRING':
try {
return typingDates.HOUR(data, params, object);
return typingMap.STRING_MAP_TO_STRING(data, params);
} catch (err) {
throw new AppError(`Typing : format STRING_MAP_TO_STRING error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
case 'STRING_MAP_TO_STRING_CAMEL':
try {
return typingMap.STRING_MAP_TO_STRING_CAMEL(data, params);
} catch (err) {
throw new AppError(`Typing : format STRING_MAP_TO_STRING_CAMEL error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
case 'STRING_MAP_TO_NUMBER':
try {
return typingMap.STRING_MAP_TO_NUMBER(data, params);
} catch (err) {
throw new AppError(`Typing : format STRING_MAP_TO_NUMBER error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
case 'STRING_MAP_TO_NUMBER_CAMEL':
try {
return typingMap.STRING_MAP_TO_NUMBER_CAMEL(data, params);
} catch (err) {
throw new AppError('Typing : format HOURS error', { code: 'HELPERS_ERROR', details: err });
throw new AppError(`Typing : format STRING_MAP_TO_NUMBER_CAMEL error on ${data}`, { code: 'HELPERS_ERROR', details: { data, params } });
}
default:
throw new AppError('Typing : format unknown', { code: 'HELPERS_ERROR' });
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/typing/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.DATE = (data) => {
if (date === 'Invalid date') {
const dateSplit = data.split('-');
if (dateSplit.length === 3) date = moment(`${dateSplit[0]}-${dateSplit[1]}`).add(Number(dateSplit[2]) - 1, 'days').format(); // Hot Fix if date containe more dats than a month
else throw new AppError(`Typing : date invalid ${data}`, { code: 'HELPERS_ERROR', details: date });
else throw new AppError('Typing : DATE');
}
return date;
};
Expand Down
45 changes: 45 additions & 0 deletions lib/helpers/typing/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require('path');
const _ = require('lodash');

const AppError = require(path.resolve('./lib/helpers/AppError'));

/**
* @desc STRING_MAP_TO_STRING
*/
exports.STRING_MAP_TO_STRING = (data, params) => {
const choices = params.map((value) => value.split('=')[0]);
const find = choices.indexOf(data);
if (find > -1) return String(params[find].split('=')[1]);
throw new AppError('Typing : STRING_MAP_TO_STRING');
};

/**
* @desc STRING_MAP_TO_STRING_CAMEL
*/
exports.STRING_MAP_TO_STRING_CAMEL = (data, params) => {
const choices = params.map((value) => _.camelCase(value.split('=')[0]));
const find = choices.indexOf(_.camelCase(data));
if (find > -1) return String(params[find].split('=')[1]);
throw new AppError('Typing : STRING_MAP_TO_STRING_CAMEL');
};


/**
* @desc STRING_MAP_TO_NUMBER
*/
exports.STRING_MAP_TO_NUMBER = (data, params) => {
const choices = params.map((value) => value.split('=')[0]);
const find = choices.indexOf(data);
if (find > -1) return Number(params[find].split('=')[1]);
throw new AppError('Typing : STRING_MAP_TO_NUMBER');
};

/**
* @desc STRING_MAP_TO_NUMBER_CAMEL
*/
exports.STRING_MAP_TO_NUMBER_CAMEL = (data, params) => {
const choices = params.map((value) => _.camelCase(value.split('=')[0]));
const find = choices.indexOf(_.camelCase(data));
if (find > -1) return Number(params[find].split('=')[1]);
throw new AppError('Typing : STRING_MAP_TO_NUMBER_CAMEL');
};

0 comments on commit 18a7d66

Please sign in to comment.