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

Skip to content

Commit 8970fdb

Browse files
committed
Init
0 parents  commit 8970fdb

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- iojs

index.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict'
2+
3+
exports.parse = function (source, transform) {
4+
return new ArrayParser(source, transform).parse()
5+
}
6+
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+
}
15+
16+
ArrayParser.prototype.isEof = function () {
17+
return this.position >= this.source.length
18+
}
19+
20+
ArrayParser.prototype.nextCharacter = function () {
21+
var character = this.source[this.position++]
22+
if (character === '\\') {
23+
return {
24+
value: this.source[this.position++],
25+
escaped: true
26+
}
27+
}
28+
return {
29+
value: character,
30+
escaped: false
31+
}
32+
}
33+
34+
ArrayParser.prototype.record = function (character) {
35+
this.recorded.push(character)
36+
}
37+
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
44+
}
45+
if (entry !== null) entry = this.transform(entry)
46+
this.entries.push(entry)
47+
this.recorded = []
48+
}
49+
}
50+
51+
ArrayParser.prototype.parse = function (nested) {
52+
var character, parser, quote
53+
while (!this.isEof()) {
54+
character = this.nextCharacter()
55+
if (character.value === '{' && !quote) {
56+
this.dimension++
57+
if (this.dimension > 1) {
58+
parser = new ArrayParser(this.source.substr(this.position - 1), this.transform)
59+
this.entries.push(parser.parse(true))
60+
this.position += parser.position - 2
61+
}
62+
} else if (character.value === '}' && !quote) {
63+
this.dimension--
64+
if (!this.dimension) {
65+
this.newEntry()
66+
if (nested) return this.entries
67+
}
68+
} else if (character.value === '"' && !character.escaped) {
69+
if (quote) this.newEntry(true)
70+
quote = !quote
71+
} else if (character.value === ',' && !quote) {
72+
this.newEntry()
73+
} else {
74+
this.record(character.value)
75+
}
76+
}
77+
if (this.dimension !== 0) {
78+
throw new Error('array dimension not balanced')
79+
}
80+
return this.entries
81+
}
82+
83+
function identity (value) {
84+
return value
85+
}

license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Ben Drucker <[email protected]> (bendrucker.me)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "postgres-array",
3+
"main": "index.js",
4+
"version": "0.0.0",
5+
"description": "Parse postgres array columns",
6+
"license": "MIT",
7+
"repository": "bendrucker/postgres-array",
8+
"author": {
9+
"name": "Ben Drucker",
10+
"email": "[email protected]",
11+
"url": "bendrucker.me"
12+
},
13+
"engines": {
14+
"node": ">=0.10.0"
15+
},
16+
"scripts": {
17+
"test": "standard && tape test.js"
18+
},
19+
"keywords": [
20+
"postgres",
21+
"array",
22+
"parser"
23+
],
24+
"dependencies": {},
25+
"devDependencies": {
26+
"tape": "^4.0.0",
27+
"standard": "^4.0.0"
28+
},
29+
"files": [
30+
"index.js",
31+
"readme.md"
32+
]
33+
}

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
var test = require('tape')
4+
var array = require('./')
5+
6+
test(function (t) {
7+
var string = array.parse
8+
t.deepEqual(string('{}'), [], 'empty')
9+
t.deepEqual(string('{""}'), [''], 'empty string')
10+
t.deepEqual(string('{1,2,3}'), ['1', '2', '3'], 'numerics')
11+
t.deepEqual(string('{a,b,c}'), ['a', 'b', 'c'], 'strings')
12+
t.deepEqual(string('{"\\"\\"\\"","\\\\\\\\\\\\"}'), ['"""', '\\\\\\'], 'escaped')
13+
t.deepEqual(string('{NULL,NULL}'), [null, null], 'null')
14+
15+
t.end()
16+
})

0 commit comments

Comments
 (0)