From 382ea5b59d6fc76d37bfb5dd67aaa1816558b34a Mon Sep 17 00:00:00 2001 From: Benjie Date: Thu, 6 Mar 2025 16:17:09 +0000 Subject: [PATCH 1/3] Even faster array parsing (#20) --- index.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) 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 = [] From 3d215dbec63d9f799cfda1438a07b657bb51748b Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Thu, 6 Mar 2025 08:20:28 -0800 Subject: [PATCH 2/3] 3.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 10ba04c..31ff477 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "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", From 3dc88058957d4525134364a2ba682eec43fb6094 Mon Sep 17 00:00:00 2001 From: Ben Drucker Date: Thu, 6 Mar 2025 08:21:53 -0800 Subject: [PATCH 3/3] `npm pkg fix` --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 31ff477..4945dea 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,10 @@ "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",