10
0

utils.js 754 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. function createMap(values, ignoreCase) {
  3. var map = {};
  4. values.forEach(function(value) {
  5. map[value] = 1;
  6. });
  7. return ignoreCase ? function(value) {
  8. return map[value.toLowerCase()] === 1;
  9. } : function(value) {
  10. return map[value] === 1;
  11. };
  12. }
  13. async function replaceAsync(str, regex, asyncFn) {
  14. const promises = [];
  15. str.replace(regex, (match, ...args) => {
  16. const promise = asyncFn(match, ...args);
  17. promises.push(promise);
  18. });
  19. const data = await Promise.all(promises);
  20. return str.replace(regex, () => data.shift());
  21. }
  22. exports.createMap = createMap;
  23. exports.createMapFromString = function(values, ignoreCase) {
  24. return createMap(values.split(/,/), ignoreCase);
  25. };
  26. exports.replaceAsync = replaceAsync;