UNPKG

dentist

Version:

NodeJS string dedenting module – make your template strings look nice!

31 lines (23 loc) 1.03 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.dedent = dedent; function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } // loosely based on https://github.com/sindresorhus/strip-indent function dedent(str) { // remove preceding newline and trailing newline followed by whitespace var trimmedStr = str.replace(/^\n/, '').replace(/\n\s*$/, ''); // match whitespace in beginning of each line var matches = trimmedStr.match(/^[ \t]*(?=\S)/gm); // nothing to do? if (!matches) return trimmedStr; // get minimum indentation level var indent = Math.min.apply(Math, _toConsumableArray(matches.map(function (match) { return match.length; }))); // no indentation that needs to be removed? if (!indent) return trimmedStr; // remove indentation return trimmedStr.replace(new RegExp('^[ \\t]{' + indent + '}', 'gm'), ''); }