Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd63e71

Browse files
committed
update for node >4
1 parent c79c1e2 commit fd63e71

File tree

3 files changed

+74
-70
lines changed

3 files changed

+74
-70
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
language: node_js
22
node_js:
3-
- iojs
3+
- '4'
4+
- 'lts/*'
5+
- node

index.js

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,92 @@ exports.parse = function (source, transform) {
44
return new ArrayParser(source, transform).parse()
55
}
66

7-
function ArrayParser (source, transform) {
8-
this.source = source
9-
this.transform = transform || identity
10-
this.position = 0
11-
this.entries = []
12-
this.recorded = []
13-
this.dimension = 0
14-
}
7+
class ArrayParser {
8+
constructor (source, transform) {
9+
this.source = source
10+
this.transform = transform || identity
11+
this.position = 0
12+
this.entries = []
13+
this.recorded = []
14+
this.dimension = 0
15+
}
1516

16-
ArrayParser.prototype.isEof = function () {
17-
return this.position >= this.source.length
18-
}
17+
isEof () {
18+
return this.position >= this.source.length
19+
}
1920

20-
ArrayParser.prototype.nextCharacter = function () {
21-
var character = this.source[this.position++]
22-
if (character === '\\') {
21+
nextCharacter () {
22+
var character = this.source[this.position++]
23+
if (character === '\\') {
24+
return {
25+
value: this.source[this.position++],
26+
escaped: true
27+
}
28+
}
2329
return {
24-
value: this.source[this.position++],
25-
escaped: true
30+
value: character,
31+
escaped: false
2632
}
2733
}
28-
return {
29-
value: character,
30-
escaped: false
31-
}
32-
}
3334

34-
ArrayParser.prototype.record = function (character) {
35-
this.recorded.push(character)
36-
}
35+
record (character) {
36+
this.recorded.push(character)
37+
}
3738

38-
ArrayParser.prototype.newEntry = function (includeEmpty) {
39-
var entry
40-
if (this.recorded.length > 0 || includeEmpty) {
41-
entry = this.recorded.join('')
42-
if (entry === 'NULL' && !includeEmpty) {
43-
entry = null
39+
newEntry (includeEmpty) {
40+
var entry
41+
if (this.recorded.length > 0 || includeEmpty) {
42+
entry = this.recorded.join('')
43+
if (entry === 'NULL' && !includeEmpty) {
44+
entry = null
45+
}
46+
if (entry !== null) entry = this.transform(entry)
47+
this.entries.push(entry)
48+
this.recorded = []
4449
}
45-
if (entry !== null) entry = this.transform(entry)
46-
this.entries.push(entry)
47-
this.recorded = []
4850
}
49-
}
5051

51-
ArrayParser.prototype.consumeDimensions = function () {
52-
if (this.source[0] === '[') {
53-
while (!this.isEof()) {
54-
var char = this.nextCharacter()
55-
if (char.value === '=') break
52+
consumeDimensions () {
53+
if (this.source[0] === '[') {
54+
while (!this.isEof()) {
55+
var char = this.nextCharacter()
56+
if (char.value === '=') break
57+
}
5658
}
5759
}
58-
}
5960

60-
ArrayParser.prototype.parse = function (nested) {
61-
var character, parser, quote
62-
this.consumeDimensions()
63-
while (!this.isEof()) {
64-
character = this.nextCharacter()
65-
if (character.value === '{' && !quote) {
66-
this.dimension++
67-
if (this.dimension > 1) {
68-
parser = new ArrayParser(this.source.substr(this.position - 1), this.transform)
69-
this.entries.push(parser.parse(true))
70-
this.position += parser.position - 2
71-
}
72-
} else if (character.value === '}' && !quote) {
73-
this.dimension--
74-
if (!this.dimension) {
61+
parse (nested) {
62+
var character, parser, quote
63+
this.consumeDimensions()
64+
while (!this.isEof()) {
65+
character = this.nextCharacter()
66+
if (character.value === '{' && !quote) {
67+
this.dimension++
68+
if (this.dimension > 1) {
69+
parser = new ArrayParser(this.source.substr(this.position - 1), this.transform)
70+
this.entries.push(parser.parse(true))
71+
this.position += parser.position - 2
72+
}
73+
} else if (character.value === '}' && !quote) {
74+
this.dimension--
75+
if (!this.dimension) {
76+
this.newEntry()
77+
if (nested) return this.entries
78+
}
79+
} else if (character.value === '"' && !character.escaped) {
80+
if (quote) this.newEntry(true)
81+
quote = !quote
82+
} else if (character.value === ',' && !quote) {
7583
this.newEntry()
76-
if (nested) return this.entries
84+
} else {
85+
this.record(character.value)
7786
}
78-
} else if (character.value === '"' && !character.escaped) {
79-
if (quote) this.newEntry(true)
80-
quote = !quote
81-
} else if (character.value === ',' && !quote) {
82-
this.newEntry()
83-
} else {
84-
this.record(character.value)
8587
}
88+
if (this.dimension !== 0) {
89+
throw new Error('array dimension not balanced')
90+
}
91+
return this.entries
8692
}
87-
if (this.dimension !== 0) {
88-
throw new Error('array dimension not balanced')
89-
}
90-
return this.entries
9193
}
9294

9395
function identity (value) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"url": "bendrucker.me"
1212
},
1313
"engines": {
14-
"node": ">=0.10.0"
14+
"node": ">=4"
1515
},
1616
"scripts": {
1717
"test": "standard && tape test.js"
@@ -25,7 +25,7 @@
2525
"dependencies": {},
2626
"devDependencies": {
2727
"ap": "^0.2.0",
28-
"standard": "^4.0.0",
28+
"standard": "^12.0.1",
2929
"tape": "^4.0.0"
3030
},
3131
"files": [

0 commit comments

Comments
 (0)