-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
683b33f
commit c2eef4d
Showing
7 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
|
||
const AppError = require(path.resolve('./lib/helpers/AppError')); | ||
|
||
const objectDeepKeys = (obj) => Object.keys(obj).filter((key) => obj[key] instanceof Object).map((key) => objectDeepKeys(obj[key]).map((k) => `${key}.${k}`)).reduce((x, y) => x.concat(y), Object.keys(obj)); | ||
|
||
const prepareFormat = (object, schema) => { | ||
const result = {}; | ||
const keys = objectDeepKeys(schema); | ||
keys.forEach((k) => { | ||
const p = k.split('.'); | ||
if (!(/^\d+$/.test(p[p.length - 1])) && !Array.isArray(_.get(schema, k))) { | ||
const targetPath = _.get(schema, k); | ||
const targetValue = _.get(object, targetPath); | ||
if (!targetValue) throw new AppError('Mapping : one of your path value is wrong.', { code: 'HELPERS_ERROR' }); | ||
if (!Array.isArray(targetValue) && targetValue) { | ||
_.set(result, k, targetValue); | ||
} else { | ||
targetValue.forEach((v, i) => { | ||
const generatePath = p; | ||
generatePath[generatePath.length - 2] = i; | ||
_.set(result, generatePath.join('.'), v); | ||
}); | ||
_.set(result, k, targetValue[0]); | ||
} | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
|
||
exports.mapping = (json, schema) => { | ||
if (Array.isArray(json) && Array.isArray(schema)) { | ||
return json.map((j) => prepareFormat(j, schema[0])); | ||
} if (typeof example === 'object' && typeof data === 'object') { | ||
return prepareFormat(json, schema); | ||
} | ||
throw new AppError('Typing & scrap data return aren\'t arrays or objects', { code: 'HELPERS_ERROR' }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
const moment = require('moment'); | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
|
||
const AppError = require(path.resolve('./lib/helpers/AppError')); | ||
|
||
|
||
/** | ||
* @desc t | ||
* @param {Object} p - params | ||
* @return {Object} result - params generated | ||
*/ | ||
const objectDeepKeys = (obj) => Object.keys(obj).filter((key) => obj[key] instanceof Object).map((key) => objectDeepKeys(obj[key]).map((k) => `${key}.${k}`)).reduce((x, y) => x.concat(y), Object.keys(obj)); | ||
|
||
const format = (data, action, object) => { | ||
const func = action.split('(')[0]; | ||
const param = action.split('(')[1].slice(0, -1); | ||
|
||
switch (func) { | ||
case 'DATE': | ||
try { | ||
return moment(new Date(data)).format(); | ||
} catch (err) { | ||
throw new AppError('Typing : format DATE error', { code: 'HELPERS_ERROR' }); | ||
} | ||
case 'NUMBER': | ||
try { | ||
data = String(data).replace(/,/g, '.'); // switch , to . | ||
data = String(data).replace(/[^\d.-]/g, ''); // remove if it's not letters | ||
return Number(data); | ||
} catch (err) { | ||
throw new AppError('Typing : format NUMBER error', { code: 'HELPERS_ERROR' }); | ||
} | ||
case 'STRING': | ||
try { | ||
return String(data); | ||
} catch (err) { | ||
throw new AppError('Typing : format STRING error', { code: 'HELPERS_ERROR' }); | ||
} | ||
case 'HOUR': | ||
try { | ||
data = String(data).replace(/h/g, ':'); | ||
data = String(data).replace(/s/g, ':'); | ||
if (param) data = `${_.get(object, param)} ${data}`; | ||
return moment(new Date(data)).format(); | ||
} catch (err) { | ||
throw new AppError('Typing : format HOURS error', { code: 'HELPERS_ERROR' }); | ||
} | ||
default: | ||
throw new AppError('Typing : format unknown', { code: 'HELPERS_ERROR' }); | ||
} | ||
}; | ||
|
||
const prepareFormat = (object, schema) => { | ||
const result = {}; | ||
const keys = objectDeepKeys(object); // get all keys | ||
keys.forEach((k) => { | ||
if (!Array.isArray(_.get(object, k))) { | ||
const p = k.split('.'); | ||
let func = ''; | ||
if (/^\d+$/.test(p[p.length - 1])) func = _.get(schema, p.slice(0, -1).join('.'))[0]; | ||
else func = _.get(schema, p.join('.')); | ||
_.set(result, k, format(_.get(object, k), func, object)); | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
|
||
exports.typing = (json, schema) => { | ||
if (Array.isArray(json) && Array.isArray(schema)) { | ||
return json.map((j) => prepareFormat(j, schema[0])); | ||
} if (typeof example === 'object' && typeof data === 'object') { | ||
return prepareFormat(json, schema); | ||
} | ||
throw new AppError('Typing : Typing & scrap data return aren\'t arrays or objects', { code: 'HELPERS_ERROR' }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters