package/package.json 000644 000765 000024 0000001036 12642773515 013031 0 ustar 00 000000 000000 { "name": "de-indent", "version": "1.0.2", "description": "remove extra indent from a block of code", "main": "index.js", "scripts": { "test": "mocha" }, "repository": { "type": "git", "url": "git+https://github.com/yyx990803/de-indent.git" }, "keywords": [ "deindent" ], "author": "Evan You", "license": "MIT", "bugs": { "url": "https://github.com/yyx990803/de-indent/issues" }, "homepage": "https://github.com/yyx990803/de-indent#readme", "devDependencies": { "mocha": "^2.3.4" } } package/.npmignore 000644 000765 000024 0000000027 12642544735 012541 0 ustar 00 000000 000000 node_modules .DS_Store package/index.js 000644 000765 000024 0000001600 12642773501 012200 0 ustar 00 000000 000000 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 } package/test.js 000644 000765 000024 0000001340 12642773336 012057 0 ustar 00 000000 000000 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