index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.dedent = void 0;
  4. function dedent(templ) {
  5. var values = [];
  6. for (var _i = 1; _i < arguments.length; _i++) {
  7. values[_i - 1] = arguments[_i];
  8. }
  9. var strings = Array.from(typeof templ === 'string' ? [templ] : templ);
  10. strings[strings.length - 1] = strings[strings.length - 1].replace(/\r?\n([\t ]*)$/, '');
  11. var indentLengths = strings.reduce(function (arr, str) {
  12. var matches = str.match(/\n([\t ]+|(?!\s).)/g);
  13. if (matches) {
  14. return arr.concat(matches.map(function (match) { var _a, _b; return (_b = (_a = match.match(/[\t ]/g)) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0; }));
  15. }
  16. return arr;
  17. }, []);
  18. if (indentLengths.length) {
  19. var pattern_1 = new RegExp("\n[\t ]{" + Math.min.apply(Math, indentLengths) + "}", 'g');
  20. strings = strings.map(function (str) { return str.replace(pattern_1, '\n'); });
  21. }
  22. strings[0] = strings[0].replace(/^\r?\n/, '');
  23. var string = strings[0];
  24. values.forEach(function (value, i) {
  25. var endentations = string.match(/(?:^|\n)( *)$/);
  26. var endentation = endentations ? endentations[1] : '';
  27. var indentedValue = value;
  28. if (typeof value === 'string' && value.includes('\n')) {
  29. indentedValue = String(value)
  30. .split('\n')
  31. .map(function (str, i) {
  32. return i === 0 ? str : "" + endentation + str;
  33. })
  34. .join('\n');
  35. }
  36. string += indentedValue + strings[i + 1];
  37. });
  38. return string;
  39. }
  40. exports.dedent = dedent;
  41. exports.default = dedent;
  42. //# sourceMappingURL=index.js.map