github-pages-deploy-action/node_modules/graphql/jsutils/dedent.mjs
2020-03-06 22:45:40 -05:00

44 lines
1.1 KiB
JavaScript

/**
* An ES6 string tag that fixes indentation. Also removes leading newlines
* and trailing spaces and tabs, but keeps trailing newlines.
*
* Example usage:
* const str = dedent`
* {
* test
* }
* `;
* str === "{\n test\n}\n";
*/
export default function dedent(strings) {
var str = '';
for (var i = 0; i < strings.length; ++i) {
str += strings[i];
if (i < (arguments.length <= 1 ? 0 : arguments.length - 1)) {
/* istanbul ignore next (ignore else inside Babel generated code) */
var value = i + 1 < 1 || arguments.length <= i + 1 ? undefined : arguments[i + 1];
str += value; // interpolation
}
}
var trimmedStr = str.replace(/^\n*/m, '') // remove leading newline
.replace(/[ \t]*$/, ''); // remove trailing spaces and tabs
// fixes indentation by removing leading spaces and tabs from each line
var indent = '';
for (var _i2 = 0; _i2 < trimmedStr.length; _i2++) {
var char = trimmedStr[_i2];
if (char !== ' ' && char !== '\t') {
break;
}
indent += char;
}
return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
}