forked from watson/next-line
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (30 loc) · 805 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict'
module.exports = function (str) {
var offset = 0
str = str.toString()
return iterator
function iterator () {
var i1 = str.indexOf('\r\n', offset)
var i2 = str.indexOf('\n', offset)
var i3 = str.indexOf('\r', offset)
var indexes = [i1, i2, i3]
var index = indexes
.sort(function (a, b) {
if (a > b) return 1
if (a < b) return -1
return 0
})
.filter(function (index) {
return index !== -1
})[0]
if (index !== undefined) return extract(index, index === i1 ? 2 : 1)
var length = str.length
if (length === offset) return null
return extract(length, 0)
}
function extract (index, skip) {
var line = str.substr(offset, index - offset)
offset = index + skip
return line
}
}