postgres-array
Advanced tools
Comparing version
130
index.js
@@ -7,76 +7,88 @@ 'use strict' | ||
function ArrayParser (source, transform) { | ||
this.source = source | ||
this.transform = transform || identity | ||
this.position = 0 | ||
this.entries = [] | ||
this.recorded = [] | ||
this.dimension = 0 | ||
} | ||
class ArrayParser { | ||
constructor (source, transform) { | ||
this.source = source | ||
this.transform = transform || identity | ||
this.position = 0 | ||
this.entries = [] | ||
this.recorded = [] | ||
this.dimension = 0 | ||
} | ||
ArrayParser.prototype.isEof = function () { | ||
return this.position >= this.source.length | ||
} | ||
isEof () { | ||
return this.position >= this.source.length | ||
} | ||
ArrayParser.prototype.nextCharacter = function () { | ||
var character = this.source[this.position++] | ||
if (character === '\\') { | ||
nextCharacter () { | ||
var character = this.source[this.position++] | ||
if (character === '\\') { | ||
return { | ||
value: this.source[this.position++], | ||
escaped: true | ||
} | ||
} | ||
return { | ||
value: this.source[this.position++], | ||
escaped: true | ||
value: character, | ||
escaped: false | ||
} | ||
} | ||
return { | ||
value: character, | ||
escaped: false | ||
record (character) { | ||
this.recorded.push(character) | ||
} | ||
} | ||
ArrayParser.prototype.record = function (character) { | ||
this.recorded.push(character) | ||
} | ||
newEntry (includeEmpty) { | ||
var entry | ||
if (this.recorded.length > 0 || includeEmpty) { | ||
entry = this.recorded.join('') | ||
if (entry === 'NULL' && !includeEmpty) { | ||
entry = null | ||
} | ||
if (entry !== null) entry = this.transform(entry) | ||
this.entries.push(entry) | ||
this.recorded = [] | ||
} | ||
} | ||
ArrayParser.prototype.newEntry = function (includeEmpty) { | ||
var entry | ||
if (this.recorded.length > 0 || includeEmpty) { | ||
entry = this.recorded.join('') | ||
if (entry === 'NULL' && !includeEmpty) { | ||
entry = null | ||
consumeDimensions () { | ||
if (this.source[0] === '[') { | ||
while (!this.isEof()) { | ||
var char = this.nextCharacter() | ||
if (char.value === '=') break | ||
} | ||
} | ||
if (entry !== null) entry = this.transform(entry) | ||
this.entries.push(entry) | ||
this.recorded = [] | ||
} | ||
} | ||
ArrayParser.prototype.parse = function (nested) { | ||
var character, parser, quote | ||
while (!this.isEof()) { | ||
character = this.nextCharacter() | ||
if (character.value === '{' && !quote) { | ||
this.dimension++ | ||
if (this.dimension > 1) { | ||
parser = new ArrayParser(this.source.substr(this.position - 1), this.transform) | ||
this.entries.push(parser.parse(true)) | ||
this.position += parser.position - 2 | ||
} | ||
} else if (character.value === '}' && !quote) { | ||
this.dimension-- | ||
if (!this.dimension) { | ||
parse (nested) { | ||
var character, parser, quote | ||
this.consumeDimensions() | ||
while (!this.isEof()) { | ||
character = this.nextCharacter() | ||
if (character.value === '{' && !quote) { | ||
this.dimension++ | ||
if (this.dimension > 1) { | ||
parser = new ArrayParser(this.source.substr(this.position - 1), this.transform) | ||
this.entries.push(parser.parse(true)) | ||
this.position += parser.position - 2 | ||
} | ||
} else if (character.value === '}' && !quote) { | ||
this.dimension-- | ||
if (!this.dimension) { | ||
this.newEntry() | ||
if (nested) return this.entries | ||
} | ||
} else if (character.value === '"' && !character.escaped) { | ||
if (quote) this.newEntry(true) | ||
quote = !quote | ||
} else if (character.value === ',' && !quote) { | ||
this.newEntry() | ||
if (nested) return this.entries | ||
} else { | ||
this.record(character.value) | ||
} | ||
} else if (character.value === '"' && !character.escaped) { | ||
if (quote) this.newEntry(true) | ||
quote = !quote | ||
} else if (character.value === ',' && !quote) { | ||
this.newEntry() | ||
} else { | ||
this.record(character.value) | ||
} | ||
if (this.dimension !== 0) { | ||
throw new Error('array dimension not balanced') | ||
} | ||
return this.entries | ||
} | ||
if (this.dimension !== 0) { | ||
throw new Error('array dimension not balanced') | ||
} | ||
return this.entries | ||
} | ||
@@ -83,0 +95,0 @@ |
{ | ||
"name": "postgres-array", | ||
"main": "index.js", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "Parse postgres array columns", | ||
@@ -14,3 +14,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -28,4 +28,3 @@ "scripts": { | ||
"devDependencies": { | ||
"ap": "^0.2.0", | ||
"standard": "^4.0.0", | ||
"standard": "^12.0.1", | ||
"tape": "^4.0.0" | ||
@@ -32,0 +31,0 @@ }, |
@@ -18,3 +18,3 @@ # postgres-array [](https://travis-ci.org/bendrucker/postgres-array) | ||
postgresArray.parse('{1,2,3}', parseInt); | ||
postgresArray.parse('{1,2,3}', (value) => parseInt(value, 10)) | ||
//=> [1, 2, 3] | ||
@@ -21,0 +21,0 @@ ``` |
4903
4.25%2
-33.33%90
13.92%