dedent.js 610 B

12345678910111213141516171819202122232425262728
  1. var makeString = require('./helper/makeString');
  2. function getIndent(str) {
  3. var matches = str.match(/^[\s\\t]*/gm);
  4. var indent = matches[0].length;
  5. for (var i = 1; i < matches.length; i++) {
  6. indent = Math.min(matches[i].length, indent);
  7. }
  8. return indent;
  9. }
  10. module.exports = function dedent(str, pattern) {
  11. str = makeString(str);
  12. var indent = getIndent(str);
  13. var reg;
  14. if (indent === 0) return str;
  15. if (typeof pattern === 'string') {
  16. reg = new RegExp('^' + pattern, 'gm');
  17. } else {
  18. reg = new RegExp('^[ \\t]{' + indent + '}', 'gm');
  19. }
  20. return str.replace(reg, '');
  21. };