diff --git a/index.js b/index.js index aa93257..3383d2b 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,14 @@ const NULL_STRING = 'NULL' function makeParseArrayWithTransform (transform) { const haveTransform = transform != null return function parseArray (str) { + const rbraceIndex = str.length - 1 + if (rbraceIndex === 1) { + return [] + } + if (str[rbraceIndex] !== RBRACE) { + throw new Error('Invalid array text - must end with }') + } + // If starts with `[`, it is specifying the index boundas. Skip past first `=`. let position = 0 if (str[position] === LBRACKET) { @@ -29,17 +37,12 @@ function makeParseArrayWithTransform (transform) { if (str[position++] !== LBRACE) { throw new Error('Invalid array text - must start with {') } - const rbraceIndex = str.length - 1 - if (str[rbraceIndex] !== RBRACE) { - throw new Error('Invalid array text - must end with }') - } const output = [] let current = output const stack = [] let currentStringStart = position - const currentStringParts = [] - let hasStringParts = false + let currentString = '' let expectValue = true for (; position < rbraceIndex; ++position) { @@ -56,8 +59,7 @@ function makeParseArrayWithTransform (transform) { while (backSlash !== -1 && backSlash < dquot) { position = backSlash const part = str.slice(currentStringStart, position) - currentStringParts.push(part) - hasStringParts = true + currentString += part currentStringStart = ++position if (dquot === position++) { // This was an escaped doublequote; find the next one! @@ -68,14 +70,9 @@ function makeParseArrayWithTransform (transform) { } position = dquot const part = str.slice(currentStringStart, position) - if (hasStringParts) { - const final = currentStringParts.join('') + part - current.push(haveTransform ? transform(final) : final) - currentStringParts.length = 0 - hasStringParts = false - } else { - current.push(haveTransform ? transform(part) : part) - } + currentString += part + current.push(haveTransform ? transform(currentString) : currentString) + currentString = '' expectValue = false } else if (char === LBRACE) { const newArray = [] diff --git a/package.json b/package.json index 10ba04c..4945dea 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,13 @@ { "name": "postgres-array", "main": "index.js", - "version": "3.0.3", + "version": "3.0.4", "description": "Parse postgres array columns", "license": "MIT", - "repository": "bendrucker/postgres-array", + "repository": { + "type": "git", + "url": "git+https://github.com/bendrucker/postgres-array.git" + }, "author": { "name": "Ben Drucker", "email": "bvdrucker@gmail.com",