8889841cindex.js 0000666 00000001600 15051445644 0006222 0 ustar 00 var splitRE = /\r?\n/g var emptyRE = /^\s*$/ var needFixRE = /^(\r?\n)*[\t\s]/ module.exports = function deindent (str) { if (!needFixRE.test(str)) { return str } var lines = str.split(splitRE) var min = Infinity var type, cur, c for (var i = 0; i < lines.length; i++) { var line = lines[i] if (!emptyRE.test(line)) { if (!type) { c = line.charAt(0) if (c === ' ' || c === '\t') { type = c cur = count(line, type) if (cur < min) { min = cur } } else { return str } } else { cur = count(line, type) if (cur < min) { min = cur } } } } return lines.map(function (line) { return line.slice(min) }).join('\n') } function count (line, type) { var i = 0 while (line.charAt(i) === type) { i++ } return i } .npmignore 0000666 00000000027 15051445644 0006556 0 ustar 00 node_modules .DS_Store test.js 0000666 00000001340 15051445644 0006073 0 ustar 00 var assert = require('assert') var deindent = require('./index') describe('de-indent', function () { it('0 indent', function () { var str = '\nabc\n bcd\n cde\nefg' var res = deindent(str) assert.equal(str, res) }) it('non-0 indent', function () { var str = ' abc\n bcd\n cde\n efg' var res = deindent(str) assert.equal(res, 'abc\n bcd\ncde\n efg') }) it('tabs', function () { var str = '\tabc\n\t\tbcd\n\tcde\n\t\tefg' var res = deindent(str) assert.equal(res, 'abc\n\tbcd\ncde\n\tefg') }) it('single line', function () { var str = '\n