diff --git a/lib/formatting.js b/lib/formatting.js index 48727da5..605b3099 100644 --- a/lib/formatting.js +++ b/lib/formatting.js @@ -153,8 +153,8 @@ const formatAs = { if (v.name === 'this') { return formatValue(obj, v.fm); } - if ('default' in options) { - const d = options.default, value = typeof d === 'function' ? d.call(obj, v.name, obj) : d; + if ('def' in options) { + const d = options.def, value = typeof d === 'function' ? d.call(obj, v.name, obj) : d; return formatValue(value, v.fm, obj); } if (options.partial) { @@ -176,8 +176,8 @@ const formatAs = { if (idx < array.length) { return formatValue(array[idx], v.fm); } - if ('default' in options) { - const d = options.default, value = typeof d === 'function' ? d.call(array, idx, array) : d; + if ('def' in options) { + const d = options.def, value = typeof d === 'function' ? d.call(array, idx, array) : d; return formatValue(value, v.fm); } if (options.partial) { @@ -806,7 +806,7 @@ const $as = { * * This option has no meaning when option `default` is present. * - * @param {*} [options.default] + * @param {*} [options.def] * Sets a default value for every variable that's missing, consequently preventing errors when encountering a variable * or property name that's missing within the formatting parameters. * @@ -827,7 +827,7 @@ const $as = { * The function will throw an error, if any occurs during formatting. */ format(query, values, options) { - options = npm.assertOptions(options, ['partial', 'default']); + options = npm.assertOptions(options, ['partial', 'def']); const ctf = getCTF(query); if (ctf) { query = ctf.toPostgres.call(query, query); diff --git a/package.json b/package.json index 44c8e469..c3c31b50 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,6 @@ "jasmine-node": "1.16.2", "jsdoc": "3.5.5", "pg-query-stream": "2.0.0", - "typescript": "3.3.3" + "typescript": "3.3.3333" } } diff --git a/test/formatSpec.js b/test/formatSpec.js index 2ee3098f..bc57d85d 100644 --- a/test/formatSpec.js +++ b/test/formatSpec.js @@ -778,9 +778,9 @@ describe('Method as.format', () => { describe('default', () => { it('must replace missing variables', () => { - expect(pgp.as.format('$1, $2', [1], {default: undefined})).toBe('1, null'); - expect(pgp.as.format('$1, $2', [1], {default: null})).toBe('1, null'); - expect(pgp.as.format('${present}, ${missing}', {present: 1}, {default: 2})).toBe('1, 2'); + expect(pgp.as.format('$1, $2', [1], {def: undefined})).toBe('1, null'); + expect(pgp.as.format('$1, $2', [1], {def: null})).toBe('1, null'); + expect(pgp.as.format('${present}, ${missing}', {present: 1}, {def: 2})).toBe('1, 2'); }); it('must invoke a callback correctly', () => { let value, context, param; @@ -793,13 +793,13 @@ describe('Method as.format', () => { } const arr = ['hi']; - expect(pgp.as.format('$1, $2', arr, {default: cb})).toBe('\'hi\', 123'); + expect(pgp.as.format('$1, $2', arr, {def: cb})).toBe('\'hi\', 123'); expect(context === arr).toBe(true); expect(param === arr).toBe(true); expect(value).toBe(1); const obj = {first: 'f'}; - expect(pgp.as.format('${first}, ${ second^ \t}', obj, {default: cb})).toBe('\'f\', 123'); + expect(pgp.as.format('${first}, ${ second^ \t}', obj, {def: cb})).toBe('\'f\', 123'); expect(context === obj).toBe(true); expect(param === obj).toBe(true); expect(value).toBe('second'); @@ -1030,7 +1030,7 @@ describe('Nested Named Parameters', () => { describe('default values', () => { it('must be formatted correctly', () => { - expect(pgp.as.format('${one.two.three}', {}, {'default': 123})).toBe('123'); + expect(pgp.as.format('${one.two.three}', {}, {def: 123})).toBe('123'); }); }); @@ -1074,7 +1074,7 @@ describe('Nested Named Parameters', () => { }); }); - describe('for default values', () => { + describe('for "def" values', () => { it('must represent the source object', () => { const obj = { value: 1, @@ -1085,15 +1085,15 @@ describe('Nested Named Parameters', () => { } } }; - expect(pgp.as.format('${one.two.three}', obj, {'default': (name, o) => o.value})).toBe('1'); + expect(pgp.as.format('${one.two.three}', obj, {def: (name, o) => o.value})).toBe('1'); expect(pgp.as.format('${one.two.three}', obj, { - 'default': function () { + def: function () { return this.value; } })).toBe('1'); }); it('must pass in the full property name', () => { - expect(pgp.as.format('${one.two.three}', {}, {'default': name => name})).toBe('\'one.two.three\''); + expect(pgp.as.format('${one.two.three}', {}, {def: name => name})).toBe('\'one.two.three\''); }); }); diff --git a/test/typescript/formatting.ts b/test/typescript/formatting.ts index 7e3867af..b1db0e40 100644 --- a/test/typescript/formatting.ts +++ b/test/typescript/formatting.ts @@ -38,7 +38,7 @@ class CTF { const ctf = new CTF(); -const testCTF = pgp.as.format(ctf); +const testCTF = pgp.as.format(ctf, null, {def: 1}); const testFunc1 = pgp.as.func(() => { }); diff --git a/typescript/pg-promise.d.ts b/typescript/pg-promise.d.ts index fd1fac19..15c2ee11 100644 --- a/typescript/pg-promise.d.ts +++ b/typescript/pg-promise.d.ts @@ -8,7 +8,7 @@ */ ///////////////////////////////////////// -// Requires pg-promise v8.5.3 or later. +// Requires pg-promise v8.6.0 or later. ///////////////////////////////////////// import * as XPromise from './ext-promise'; // External Promise Provider @@ -36,7 +36,7 @@ declare namespace pgPromise { type TFormattingOptions = { partial?: boolean - default?: any + def?: any }; interface ILostContext {