forked from sequelize/sequelize
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c327355
commit 718de51
Showing
3 changed files
with
36 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,16 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
stringifyPart: function(part) { | ||
switch (typeof part) { | ||
case 'boolean': | ||
case 'number': | ||
return String(part); | ||
case 'string': | ||
return '"' + part.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"'; | ||
case 'undefined': | ||
return 'NULL'; | ||
default: | ||
if (part === null) | ||
return 'NULL'; | ||
else | ||
return '"' + JSON.stringify(part).replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"'; | ||
} | ||
}, | ||
stringifyObject: function(data) { | ||
var self = this; | ||
var hstore = require("pg-hstore")({sanitize : true}); | ||
|
||
return Object.keys(data).map(function(key) { | ||
return self.stringifyPart(key) + '=>' + self.stringifyPart(data[key]); | ||
}).join(','); | ||
}, | ||
stringifyArray: function(data) { | ||
return data.map(this.stringifyObject, this); | ||
}, | ||
module.exports = { | ||
stringify: function(data) { | ||
if (Array.isArray(data)) { | ||
return this.stringifyArray(data); | ||
} | ||
|
||
return this.stringifyObject(data); | ||
}, | ||
parsePart: function(part) { | ||
part = part.replace(/\\\\/g, '\\').replace(/\\"/g, '"'); | ||
|
||
switch (part[0]) { | ||
case '{': | ||
case '[': | ||
return JSON.parse(part); | ||
default: | ||
return part; | ||
} | ||
}, | ||
parseObject: function(string) { | ||
var self = this, | ||
object = { }; | ||
|
||
if (0 === string.length) { | ||
return object; | ||
} | ||
|
||
var rx = /\"((?:\\\"|[^"])*)\"\s*\=\>\s*((?:true|false|NULL|\d+|\d+\.\d+|\"((?:\\\"|[^"])*)\"))/g; | ||
|
||
string = string || ''; | ||
if(data === null) return null; | ||
|
||
string.replace(rx, function(match, key, value, innerValue) { | ||
switch (value) { | ||
case 'true': | ||
object[self.parsePart(key)] = true; | ||
break; | ||
case 'false': | ||
object[self.parsePart(key)] = false; | ||
break; | ||
case 'NULL': | ||
object[self.parsePart(key)] = null; | ||
break; | ||
default: | ||
object[self.parsePart(key)] = self.parsePart(innerValue || value); | ||
break; | ||
} | ||
}); | ||
|
||
return object; | ||
}, | ||
parseArray: function(string) { | ||
var matches = string.match(/{(.*)}/); | ||
var array = JSON.parse('['+ matches[1] +']'); | ||
|
||
return array.map(this.parseObject, this); | ||
return hstore.stringify(data); | ||
}, | ||
parse: function(value) { | ||
if ('string' !== typeof value) { | ||
return value; | ||
} | ||
|
||
if ('{' === value[0] && '}' === value[value.length - 1]) { | ||
return this.parseArray(value); | ||
} | ||
if(value === null) return null; | ||
|
||
return this.parseObject(value); | ||
return hstore.parse(value); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters