Skip to content

Commit

Permalink
renaming default into def for as.format
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Mar 2, 2019
1 parent 147f119 commit e2ddbfc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
12 changes: 6 additions & 6 deletions lib/formatting.js
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
}
}
20 changes: 10 additions & 10 deletions test/formatSpec.js
Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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');
});
});

Expand Down Expand Up @@ -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,
Expand All @@ -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\'');
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/typescript/formatting.ts
Expand Up @@ -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(() => {
});
Expand Down
4 changes: 2 additions & 2 deletions typescript/pg-promise.d.ts
Expand Up @@ -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
Expand Down Expand Up @@ -36,7 +36,7 @@ declare namespace pgPromise {

type TFormattingOptions = {
partial?: boolean
default?: any
def?: any
};

interface ILostContext {
Expand Down

0 comments on commit e2ddbfc

Please sign in to comment.