trim-leading-lines
Advanced tools
Comparing version 0.1.0 to 0.1.1
19
index.js
'use strict'; | ||
var isWhitespace = require('is-whitespace'); | ||
module.exports = function(str) { | ||
@@ -7,8 +9,17 @@ if (typeof str !== 'string') { | ||
} | ||
if (!str) return str; | ||
var lines = str.split('\n'); | ||
var idx = 0; | ||
while (/^\s+$/.test(lines[idx]) || !lines[idx]) { | ||
idx++; | ||
if (lines.length === 1) { | ||
return str; | ||
} | ||
return lines.slice(idx).join('\n'); | ||
var start = 0; | ||
for (var i = 0; i < lines.length; i++) { | ||
var line = lines[i]; | ||
if (isWhitespace(line) || line === '') { | ||
continue; | ||
} | ||
start = i; | ||
break; | ||
} | ||
return lines.slice(start).join('\n'); | ||
}; |
{ | ||
"name": "trim-leading-lines", | ||
"description": "Trim leading lines from a string when they are 100% whitespace or empty.", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/jonschlinkert/trim-leading-lines", | ||
@@ -46,3 +46,6 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
] | ||
}, | ||
"dependencies": { | ||
"is-whitespace": "^0.3.0" | ||
} | ||
} |
@@ -23,2 +23,10 @@ 'use strict'; | ||
it('should return empty strings', function() { | ||
assert.equal(trimLeadingLines(''), ''); | ||
}); | ||
it('should return original string if no newlines', function() { | ||
assert.equal(trimLeadingLines('foo'), 'foo'); | ||
}); | ||
it('should trim leading whitespace lines', function() { | ||
@@ -25,0 +33,0 @@ var fixture = [ |
13606
385
1
+ Addedis-whitespace@^0.3.0
+ Addedis-whitespace@0.3.0(transitive)