asksuite-core
Version:
60 lines (50 loc) • 1.84 kB
JavaScript
const Promise = require('bluebird');
const _ = require('lodash');
const shouldIgnorePath = (fullPath, ignorePaths) => {
return _.some(ignorePaths, path => {
return pathToString(fullPath).endsWith(pathToString(path));
});
};
const pathToString = path => _.join(path, '.');
const asyncMapDeep = (array, key, mapFn, ignoreBranches, ignorePaths, delay = 0, concurrency = 50, path = []) =>
Promise.map(
array,
value =>
Promise.delay(delay).then(() => _.isObject(value) || _.isArray(value)
? asyncMapValuesDeep(value, mapFn, ignoreBranches, ignorePaths, delay, concurrency, path )
: mapFn(value, key))
,
{ concurrency },
);
const asyncMapValuesDeep = (object, mapFn, ignoreBranches, ignorePaths, delay = 0, concurrency = 50, path = []) =>
Promise.props(
_.mapValues(object, (value, key) => {
const currentPath = _.clone(path);
currentPath.push(key);
if (_.includes(ignoreBranches, key) || shouldIgnorePath(currentPath, ignorePaths)) {
return value;
}
return _.isArray(value)
? asyncMapDeep(value, key, mapFn, ignoreBranches, ignorePaths, delay, concurrency, currentPath)
: _.isObject(value)
? asyncMapValuesDeep(value, mapFn, ignoreBranches, ignorePaths, delay, concurrency, currentPath)
: mapFn(value, key);
}),
);
const removePreventTranslationMarkers = text => {
if (!text || !_.isString(text)) {
return text;
}
const markerRegex = /\[\[[^\[\]]*\]\]/g;
const markers = text.match(markerRegex);
if (markers && markers.length) {
markers.forEach(marker => {
text = text.replace(marker, marker.replace('[[', '').replace(']]', ''));
});
}
return text;
};
module.exports = {
asyncMapValuesDeep,
removePreventTranslationMarkers,
};