diff --git a/TODO.md b/TODO.md index ceb83c7b..e5b99dc4 100644 --- a/TODO.md +++ b/TODO.md @@ -72,8 +72,6 @@ https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore - _set - _omit - _intersection -- _cloneDeepWith -- _mapValues - flatten => flat ### Dose class diff --git a/src/module-system/table-module.js b/src/module-system/table-module.js index 90ef30d3..0cde1dbd 100644 --- a/src/module-system/table-module.js +++ b/src/module-system/table-module.js @@ -1,5 +1,4 @@ const { convertExcelSync } = require('../xlsx-connector'); -const _cloneDeepWith = require('lodash/cloneDeepWith'); /** * To initialize a Heta module of the "table" type. @@ -26,15 +25,15 @@ function tableLoader(fileContent, _options){ let parsed = rawData .filter((x) => x.on) // ignore rows - .map((x) => { + .map((x) => { let cleaned = _cloneDeepWith(x, (value) => { - if (value != null && typeof value.valueOf() === 'string') { + if (typeof value?.valueOf() === 'string') { return clean(value); - } - if (Array.isArray(value)) { - return value - .map((y) => clean(y)) + } else if (Array.isArray(value)) { + return value.map((y) => clean(y)) .filter((y) => y !== ''); // removes empty strings from array + } else { + return value; } }); @@ -76,4 +75,23 @@ function forceBool(x) { } } -module.exports = tableLoader; \ No newline at end of file +// clone all own properties and arrays +function _cloneDeepWith(o, handler = (x) => x) { + if (o instanceof Object) { + var clone; + if (o instanceof Array) { + clone = o.map((key) => _cloneDeepWith(key, handler)); + } else { + clone = {}; + Object.entries(o).forEach(([key, value]) => { + clone[key] = _cloneDeepWith(value, handler); + }); + } + + return handler(clone); + } else { + return handler(o); + } +} + +module.exports = tableLoader;