index.js 1.6 KB

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