Skip to content

Commit

Permalink
fixing issue #12
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Apr 4, 2015
1 parent 6c545b9 commit ff586a3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ now can also be an object whose properties can be referred to by name from withi
Since all variables in this case are property names of the object-parameter, standard javascript
variable naming convention applies here:

* a valid variable starts with a letter or underscore symbol, followed by any combination of letters,
digits or underscores;
* a valid variable starts with a letter, underscore or `$` symbol, followed by any combination
of letters, digits, underscores and `$`;
* leading and trailing white spaces surrounding variables are ignored;
* variable names are case-sensitive.

Expand Down
12 changes: 7 additions & 5 deletions lib/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ function formatCSV(values) {
//
// Variables are defined using syntax "${varName}", following
// the javascript variable naming convention:
// - a valid variable starts with a letter or underscore symbol,
// followed by any combination of letters, digits or underscores.
// - a valid variable starts with a letter, underscore or '$' symbol,
// followed by any combination of letters, digits, underscores and '$'.
// - leading and trailing spaces around the variable are ignored.
function enumVars(txt) {
var v, names = [];
var reg = /\$\{\s*[a-zA-Z_][a-zA-Z0-9_]*\s*}/g;
var reg = /\$\{\s*[a-zA-Z\$_][a-zA-Z0-9\$_]*\s*}/g;
while (v = reg.exec(txt)) {
var svn = v[0].replace(/[\$\{\s}]/g, ''); // stripped variable name;
var svn = v[0].replace(/^\$\{\s*|\s*}$/g, ''); // stripped variable name;
if (names.indexOf(svn) === -1) {
names.push(svn);
}
Expand Down Expand Up @@ -185,7 +185,9 @@ function $formatQuery(query, values) {
// error: not a simple value;
throw new Error("Cannot convert type '" + typeof(prop) + "' of property '" + name + "'");
}
query = query.replace(new RegExp("\\$\\{\\s*" + names[i] + "\\s*}", 'g'), value);
// Replacing all occurrences of the 'name' variable with the 'value',
// while changing any '$' in 'name' to '\$', for reg-ex compliance:
query = query.replace(new RegExp("\\$\\{\\s*" + name.replace(/\$/g, '\\$') + "\\s*}", 'g'), value);
}
} else {
if (values !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "0.8.1",
"version": "0.8.2",
"description": "PG + Promises/A+, with transactions.",
"main": "lib/index.js",
"scripts": {
Expand Down
15 changes: 11 additions & 4 deletions test/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,25 @@ describe("Method as.format", function () {

it("must correctly format named parameters or throw an error", function () {
// - correctly handles leading and trailing spaces;
// - supports underscores and digits in names;
// - supports underscores, digits and '$' in names;
// - can join variables values next to each other;
// - converts all simple types correctly;
// - replaces undefined variables with null;
// - variables are case-sensitive;
expect(pgp.as.format("${ NamE_},${d_o_b },${ _active__},${__Balance}", {
NamE_: "John O'Connor",
expect(pgp.as.format("${ $Nam$E_},${d_o_b },${ _active__},${_$_Balance}", {
$Nam$E_: "John O'Connor",
d_o_b: dateSample,
_active__: true,
__Balance: -123.45
_$_Balance: -123.45
})).toBe("'John O''Connor','" + dateSample.toUTCString() + "',TRUE,-123.45");

// test that even one-symbol, special-named properties work correctly;
expect(pgp.as.format("${$}${_}${a}",{
$: 1,
_: 2,
a: 3
})).toBe("123");

// Both null and undefined properties are formatted as null;
expect(pgp.as.format("${empty1}, ${empty2}", {
empty1: null,
Expand Down

0 comments on commit ff586a3

Please sign in to comment.