From ff586a3e90b831ba5e67c582b33c5c1788a5c0db Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sat, 4 Apr 2015 05:36:30 +0100 Subject: [PATCH] fixing issue #12 --- README.md | 4 ++-- lib/formatting.js | 12 +++++++----- package.json | 2 +- test/indexSpec.js | 15 +++++++++++---- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8723df7b..f36bdb5d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/formatting.js b/lib/formatting.js index 20ab6a6d..7e88a12b 100644 --- a/lib/formatting.js +++ b/lib/formatting.js @@ -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); } @@ -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) { diff --git a/package.json b/package.json index d47b4217..ed8c42b0 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/indexSpec.js b/test/indexSpec.js index 07341958..bd76d26c 100644 --- a/test/indexSpec.js +++ b/test/indexSpec.js @@ -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,