Comparing version 1.0.1 to 1.0.2
@@ -34,8 +34,28 @@ var sanitize_input = function(input) { | ||
var result = {}; | ||
var hstore = value.split(',').forEach(function(pair) { | ||
var split = pair.split('=>'); | ||
var key = split[0].replace(/"/g, '').replace(/^\s\s*/, '').replace(/\s\s*$/, ''); | ||
var value = split[1].replace(/"/g, ''); | ||
result[key] = value; | ||
}); | ||
var ke = value.length, | ||
vs = value.length, | ||
ve = value.length, | ||
key, val; | ||
var buf = ''; | ||
var i = value.length; | ||
//work backwards through string | ||
while (i > -1) { | ||
buf = value.slice(i - 2, i); | ||
if (buf == '=>' || i === 0) { | ||
vs = i; | ||
ve = (value.lastIndexOf(',') == -1) ? value.length : value.lastIndexOf(','); | ||
if (i === 0) { | ||
ke = value.indexOf('=>'); | ||
ve = -1; | ||
} | ||
if (val) { | ||
key = value.slice(ve + 1, ke).trim().slice(1, - 1); | ||
value = value.slice(0, ve); | ||
result[key] = val; | ||
} | ||
val = value.slice(vs, value.length).trim().slice(1, - 1); | ||
ke = i - 2; | ||
} | ||
i--; | ||
} | ||
if (!callback || callback === null) return result; | ||
@@ -42,0 +62,0 @@ callback(result); |
@@ -7,3 +7,3 @@ { | ||
"keywords": ["pg", "postgres", "hstore"], | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"main": "lib/index.js", | ||
@@ -10,0 +10,0 @@ "homepage": "https://github.com/scarney81/pg-hstore", |
@@ -8,3 +8,5 @@ pg-hstore | ||
```$ npm install pg-hstore``` | ||
```bash | ||
$ npm install pg-hstore | ||
``` | ||
@@ -14,18 +16,22 @@ ## Usage | ||
var hstore = require('pg-hstore'); | ||
var source = { foo: "oof", bar: "rab", baz: "zab" }; | ||
hstore.stringify(source, function(result) { | ||
... | ||
// result = '"foo"=>"oof", "bar"=>"rab", "baz"=>"zab"' | ||
... | ||
}); | ||
```javascript | ||
var hstore = require('pg-hstore'); | ||
var source = { foo: "oof", bar: "rab", baz: "zab" }; | ||
hstore.stringify(source, function(result) { | ||
... | ||
// result = '"foo"=>"oof", "bar"=>"rab", "baz"=>"zab"' | ||
... | ||
}); | ||
``` | ||
### parse | ||
var hstore = require('pg-hstore'); | ||
var source = '"foo"=>"oof", "bar"=>"rab", "baz"=>"zab"'; | ||
hstore.stringify(source, function(result) { | ||
... | ||
// result = { foo: "oof", bar: "rab", baz: "zab" } | ||
... | ||
}); | ||
```javascript | ||
var hstore = require('pg-hstore'); | ||
var source = '"foo"=>"oof", "bar"=>"rab", "baz"=>"zab"'; | ||
hstore.parse(source, function(result) { | ||
... | ||
// result = { foo: "oof", bar: "rab", baz: "zab" } | ||
... | ||
}); | ||
``` |
@@ -26,2 +26,29 @@ /*globals it, describe */ | ||
}); | ||
it('should hstore parse an escaped quoted string with quotes', function(done) { | ||
var source = '"foo"=>"\"bar\""'; | ||
hstore.parse(source, function(target) { | ||
should.exist(target); | ||
target.foo.should.equal('"bar"'); | ||
done(); | ||
}); | ||
}); | ||
it('should hstore parse a string with commas', function(done) { | ||
var source = '"foo"=>"bar,foo,bar"'; | ||
hstore.parse(source, function(target) { | ||
should.exist(target); | ||
target.foo.should.equal('bar,foo,bar'); | ||
done(); | ||
}); | ||
}); | ||
it('should hstore parse a string with advanced types', function(done) { | ||
var source = '"foo"=>"{"key":"value","key2":"value"}"'; | ||
hstore.parse(source, function(target) { | ||
should.exist(target); | ||
target.foo.should.equal('{"key":"value","key2":"value"}'); | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5895
144
35