forked from sitepodmatt/node-postgres-hstore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (21 loc) · 690 Bytes
/
index.js
File metadata and controls
31 lines (21 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module.exports.parse = function(val) {
var pattern, re, result, match, key, value;
pattern = '("(?:\\\\\"|[^"])*?")\\s*=>\\s*((?:"(?:\\\\\"|[^"])*?")|NULL)';
re = new RegExp(pattern,'gi');
result = {};
match = null;
while((match = re.exec(val)) != null) {
key = JSON.parse(match[1]);
value = match[2] == "NULL" ? null : JSON.parse(match[2]);
result[key] = value;
}
return result;
}
module.exports.stringify = function(val) {
var result = Object.keys(val).map(function(key) {
var value = val[key];
value = value === null ? 'NULL' : JSON.stringify(value.toString());
return '"' + key + '" => ' + value;
}).join(', ');
return result;
}