Comparing version 1.1.0 to 2.0.0
116
lib/index.js
@@ -1,59 +0,67 @@ | ||
var sanitize_input = function(input) { | ||
// http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html [4.1.2.1-4.1.2.2] | ||
// single quotes (') must be replaced with double single quotes ('') | ||
input = input.replace(/'/g, '\'\''); | ||
// backslashes (\) must be replaced with double backslashes (\\) | ||
input = input.replace(/\\/g, '\\\\'); | ||
// double quotes (") must be replaced with escaped quotes (\\") | ||
input = input.replace(/"/g, '\\"'); | ||
// colons (:) must be replaced with escaped colons (\\:) | ||
input = input.replace(/:/g, '\\:'); | ||
return input; | ||
}; | ||
(function () { | ||
var _ = require('underscore'); | ||
var to_string = function(input) { | ||
switch(typeof input) { | ||
case 'boolean': | ||
case 'number': | ||
return String(input); | ||
case 'string': | ||
return sanitize_input(input); | ||
default: | ||
return ''; | ||
function sanitize_input(input) { | ||
// http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html [4.1.2.1-4.1.2.2] | ||
// single quotes (') must be replaced with double single quotes ('') | ||
input = input.replace(/'/g, '\'\''); | ||
// backslashes (\) must be replaced with double backslashes (\\) | ||
input = input.replace(/\\/g, '\\\\'); | ||
// double quotes (") must be replaced with escaped quotes (\\") | ||
input = input.replace(/"/g, '\\"'); | ||
// colons (:) must be replaced with escaped colons (\\:) | ||
input = input.replace(/:/g, '\\:'); | ||
return input; | ||
} | ||
}; | ||
module.exports = { | ||
stringify: function (data, callback) { | ||
var hstore = Object.keys(data).map(function (key) { | ||
if (data[key] === null) { | ||
return '"'+to_string(key)+'"=>NULL'; | ||
} else { | ||
return '"'+to_string(key)+'"=>"'+to_string(data[key])+'"'; | ||
} | ||
}); | ||
var joined = hstore.join(); | ||
if (!callback || callback === null) return joined; | ||
callback(joined); | ||
}, | ||
function to_string(input, sanitize) { | ||
switch(typeof input) { | ||
case 'boolean': | ||
case 'number': | ||
return String(input); | ||
case 'string': | ||
return sanitize ? sanitize_input(input) : input; | ||
default: | ||
return ''; | ||
} | ||
} | ||
parse: function(value, callback) { | ||
var result = {}, | ||
r = /(["])(?:\\\1|.)*?\1/g, | ||
matches = value.match(r), | ||
i, | ||
l, | ||
clean = function (value) { | ||
// Remove leading double quotes | ||
value = value.replace(/^\"|\"$/g, ""); | ||
// Unescape quotes | ||
return value.replace(/\\"/g, "\""); | ||
}; | ||
module.exports = function (options) { | ||
options = _.defaults({ sanitize: false }, options); | ||
for (i = 0, l = matches.length; i < l; i+= 2) { | ||
result[clean(matches[i])] = clean(matches[i + 1]); | ||
} | ||
if (!callback || callback === null) return result; | ||
callback(result); | ||
} | ||
}; | ||
return { | ||
stringify: function (data, callback) { | ||
var hstore = Object.keys(data).map(function (key) { | ||
if (data[key] === null) { | ||
return '"'+to_string(key, options.sanitize)+'"=>NULL'; | ||
} else { | ||
return '"'+to_string(key, options.sanitize)+'"=>"'+to_string(data[key], options.sanitize)+'"'; | ||
} | ||
}); | ||
var joined = hstore.join(); | ||
if (!callback || callback === null) return joined; | ||
callback(joined); | ||
}, | ||
parse: function(value, callback) { | ||
var result = {}, | ||
r = /(["])(?:\\\1|.)*?\1/g, | ||
matches = value.match(r), | ||
i, | ||
l, | ||
clean = function (value) { | ||
// Remove leading double quotes | ||
value = value.replace(/^\"|\"$/g, ""); | ||
// Unescape quotes | ||
return value.replace(/\\"/g, "\""); | ||
}; | ||
for (i = 0, l = matches.length; i < l; i+= 2) { | ||
result[clean(matches[i])] = clean(matches[i + 1]); | ||
} | ||
if (!callback || callback === null) return result; | ||
callback(result); | ||
} | ||
}; | ||
}; | ||
})(); |
@@ -7,3 +7,3 @@ { | ||
"keywords": ["pg", "postgres", "hstore"], | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"main": "lib/index.js", | ||
@@ -18,2 +18,5 @@ "homepage": "https://github.com/scarney81/pg-hstore", | ||
}, | ||
"dependencies": { | ||
"underscore": "*" | ||
}, | ||
"devDependencies": { | ||
@@ -25,3 +28,6 @@ "mocha": "1.2.x", | ||
"node": ">= 0.8.x" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha --reporter spec -u bdd --require should --recursive --timeout 10000" | ||
} | ||
} |
@@ -1,53 +0,61 @@ | ||
/*globals it, describe */ | ||
var mocha = require('mocha'), | ||
should = require('should'), | ||
hstore = require('../lib/index.js'); | ||
/*globals it, describe, beforeEach */ | ||
(function () { | ||
'use strict'; | ||
describe('pg-hstore.parse', function() { | ||
it('should hstore parse an hstore string', function(done) { | ||
var source = '"foo"=>"bar"'; | ||
hstore.parse(source, function(target) { | ||
should.exist(target); | ||
target.foo.should.equal('bar'); | ||
done(); | ||
var should = require('should'), | ||
HStore = require('../lib/index.js'), | ||
hstore; | ||
describe('pg-hstore.parse', function () { | ||
beforeEach(function () { | ||
hstore = new HStore(); | ||
}); | ||
}); | ||
it('should hstore parse an hstore string with multiple values', function(done) { | ||
var source = '"foo"=>"oof","bar"=>"rab","baz"=>"zab"'; | ||
hstore.parse(source, function(target) { | ||
should.exist(target); | ||
target.foo.should.equal('oof'); | ||
target.bar.should.equal('rab'); | ||
target.baz.should.equal('zab'); | ||
done(); | ||
it('should hstore parse an hstore string', function (done) { | ||
var source = '"foo"=>"bar"'; | ||
hstore.parse(source, function (target) { | ||
should.exist(target); | ||
target.foo.should.equal('bar'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
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 an hstore string with multiple values', function (done) { | ||
var source = '"foo"=>"oof","bar"=>"rab","baz"=>"zab"'; | ||
hstore.parse(source, function (target) { | ||
should.exist(target); | ||
target.foo.should.equal('oof'); | ||
target.bar.should.equal('rab'); | ||
target.baz.should.equal('zab'); | ||
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 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 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(); | ||
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
})(); |
@@ -1,87 +0,104 @@ | ||
/*globals it, describe */ | ||
var mocha = require('mocha'), | ||
should = require('should'), | ||
hstore = require('../lib/index.js'); | ||
/*globals it, describe, beforeEach */ | ||
(function () { | ||
'use strict'; | ||
describe('pg-hstore.stringify', function() { | ||
it('should hstore encode a string', function(done) { | ||
var source = { foo: "bar" }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"bar"'); | ||
done(); | ||
var should = require('should'), | ||
HStore = require('../lib/index.js'), | ||
hstore; | ||
describe('pg-hstore.stringify', function () { | ||
beforeEach(function () { | ||
hstore = new HStore(); | ||
}); | ||
}); | ||
it('should hstore encode a number', function(done) { | ||
var source = { foo: 1000 }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"1000"'); | ||
done(); | ||
it('should hstore encode a string', function (done) { | ||
var source = { foo: 'bar' }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"bar"'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should hstore encode a boolean', function(done) { | ||
var source = { foo: true }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"true"'); | ||
done(); | ||
it('should hstore encode a number', function (done) { | ||
var source = { foo: 1000 }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"1000"'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should hstore encode a null value', function(done) { | ||
var source = { foo: null }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>NULL'); | ||
done(); | ||
it('should hstore encode a boolean', function (done) { | ||
var source = { foo: true }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"true"'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should hstore encode a null string value', function(done) { | ||
var source = { foo: "null" }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"null"'); | ||
done(); | ||
it('should hstore encode a null value', function (done) { | ||
var source = { foo: null }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>NULL'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should hstore encode single quotes correctly', function(done) { | ||
var source = { 'foo \'quotes\'': "with \'quotes\'" }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo \'\'quotes\'\'"=>"with \'\'quotes\'\'"'); | ||
done(); | ||
it('should hstore encode a null string value', function (done) { | ||
var source = { foo: 'null' }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"null"'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should hstore encode double quotes correctly', function(done) { | ||
var source = { foo: "with \"quotes\"" }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"with \\"quotes\\""'); | ||
done(); | ||
it('should hstore encode single quotes correctly', function (done) { | ||
var source = { 'foo \'quotes\'': 'with \'quotes\'' }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo \'quotes\'"=>"with \'quotes\'"'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should hstore encode double quote keys correctly', function(done) { | ||
var source = { 'foo \"quotes\"': "with \"quotes\"" }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo \\"quotes\\""=>"with \\"quotes\\""'); | ||
done(); | ||
it('should hstore encode double quotes correctly', function (done) { | ||
var source = { foo: 'with \"quotes\"' }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"with "quotes""'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should hstore encode colon correctly', function(done) { | ||
var source = { 'foo': "with:colon" }; | ||
hstore.stringify(source, function(target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"with\\:colon"'); | ||
done(); | ||
it('should hstore encode double quote keys correctly', function (done) { | ||
var source = { 'foo \"quotes\"': 'with \"quotes\"' }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo "quotes""=>"with "quotes""'); | ||
done(); | ||
}); | ||
}); | ||
it('should hstore encode colon correctly', function (done) { | ||
var source = { 'foo': 'with:colon' }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo"=>"with:colon"'); | ||
done(); | ||
}); | ||
}); | ||
it('should not sanitize output', function (done) { | ||
var source = { 'foo\'"\\': 'bar' }; | ||
hstore.stringify(source, function (target) { | ||
should.exist(target); | ||
target.should.equal('"foo\'"\\"=>"bar"'); | ||
done(); | ||
}, true); | ||
}); | ||
}); | ||
}); | ||
})(); |
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
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
15779
10
207
1
1
+ Addedunderscore@*
+ Addedunderscore@1.13.7(transitive)