package/license000644 0000002127 3560116604 010627 0ustar00000000 000000 The MIT License (MIT) Copyright (c) Ben Drucker (bendrucker.me) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.js000644 0000007371 3560116604 010735 0ustar00000000 000000 'use strict' const BACKSLASH = '\\' const DQUOT = '"' const LBRACE = '{' const RBRACE = '}' const LBRACKET = '[' const EQUALS = '=' const COMMA = ',' /** When the raw value is this, it means a literal `null` */ const NULL_STRING = 'NULL' /** * Parses an array according to * https://www.postgresql.org/docs/17/arrays.html#ARRAYS-IO * * Trusts the data (mostly), so only hook up to trusted Postgres servers. */ 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) { position = str.indexOf(EQUALS) + 1 } if (str[position++] !== LBRACE) { throw new Error('Invalid array text - must start with {') } const output = [] let current = output const stack = [] let currentStringStart = position let currentString = '' let expectValue = true for (; position < rbraceIndex; ++position) { let char = str[position] // > The array output routine will put double quotes around element values if // > they are empty strings, contain curly braces, delimiter characters, double // > quotes, backslashes, or white space, or match the word NULL. Double quotes // > and backslashes embedded in element values will be backslash-escaped. if (char === DQUOT) { // It's escaped currentStringStart = ++position let dquot = str.indexOf(DQUOT, currentStringStart) let backSlash = str.indexOf(BACKSLASH, currentStringStart) while (backSlash !== -1 && backSlash < dquot) { position = backSlash const part = str.slice(currentStringStart, position) currentString += part currentStringStart = ++position if (dquot === position++) { // This was an escaped doublequote; find the next one! dquot = str.indexOf(DQUOT, position) } // Either way, find the next backslash backSlash = str.indexOf(BACKSLASH, position) } position = dquot const part = str.slice(currentStringStart, position) currentString += part current.push(haveTransform ? transform(currentString) : currentString) currentString = '' expectValue = false } else if (char === LBRACE) { const newArray = [] current.push(newArray) stack.push(current) current = newArray currentStringStart = position + 1 expectValue = true } else if (char === COMMA) { expectValue = true } else if (char === RBRACE) { expectValue = false const arr = stack.pop() if (arr === undefined) { throw new Error("Invalid array text - too many '}'") } current = arr } else if (expectValue) { currentStringStart = position while ( (char = str[position]) !== COMMA && char !== RBRACE && position < rbraceIndex ) { ++position } const part = str.slice(currentStringStart, position--) current.push( part === NULL_STRING ? null : haveTransform ? transform(part) : part ) expectValue = false } else { throw new Error('Was expecting delimeter') } } return output } } const parseArray = makeParseArrayWithTransform() exports.parse = (source, transform) => transform != null ? makeParseArrayWithTransform(transform)(source) : parseArray(source) package/package.json000644 0000001212 3560116604 011542 0ustar00000000 000000 { "name": "postgres-array", "main": "index.js", "version": "3.0.4", "description": "Parse postgres array columns", "license": "MIT", "repository": "bendrucker/postgres-array", "author": { "name": "Ben Drucker", "email": "bvdrucker@gmail.com", "url": "bendrucker.me" }, "engines": { "node": ">=12" }, "scripts": { "test": "standard && tape test.js" }, "types": "index.d.ts", "keywords": [ "postgres", "array", "parser" ], "dependencies": {}, "devDependencies": { "standard": "^17.0.0", "tape": "^5.0.0" }, "files": [ "index.js", "index.d.ts", "readme.md" ] } package/readme.md000644 0000001274 3560116604 011043 0ustar00000000 000000 # postgres-array [![tests](https://github.com/bendrucker/postgres-array/workflows/tests/badge.svg)](https://github.com/bendrucker/postgres-array/actions?query=workflow%3Atests) > Parse postgres array columns ## Install ``` npm install --save postgres-array ``` ## Usage ```js const { parse } = require('postgres-array') parse('{1,2,3}', (value) => parseInt(value, 10)) //=> [1, 2, 3] ``` ## API #### `parse(input, [transform])` -> `array` ##### input *Required* Type: `string` A Postgres array string. ##### transform Type: `function` Default: `identity` A function that transforms non-null values inserted into the array. ## License MIT © [Ben Drucker](http://bendrucker.me) package/index.d.ts000644 0000000203 3560116604 011154 0ustar00000000 000000 export function parse(source: string): string[]; export function parse(source: string, transform: (value: string) => T): T[];