Skip to content

Commit 19776db

Browse files
committed
improving _TN + adding tests
1 parent 8802545 commit 19776db

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

lib/helpers/table-name.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ function _TN(a, ...args) {
159159
a = a.map((b, i) => b + (i < args.length ? args[i] : '')).join('');
160160
} // else 'a' is a string
161161
const [schema, table] = a.split('.');
162-
return table === undefined ? {table: schema} : {schema, table};
162+
if(table === undefined) {
163+
return {table: schema};
164+
}
165+
return schema ? {schema, table} : {table};
163166
}
164167

165168
module.exports = {TableName, _TN};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pg-promise",
3-
"version": "11.7.5",
3+
"version": "11.7.6",
44
"description": "PostgreSQL interface for Node.js",
55
"main": "lib/index.js",
66
"typings": "typescript/pg-promise.d.ts",

test/help.spec.js

+57
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,60 @@ describe('method \'concat\'', () => {
927927
});
928928
});
929929
});
930+
931+
describe('_TN', () => {
932+
describe('for strings', () => {
933+
it('must support empty', () => {
934+
expect(helpers._TN('')).toEqual({table: ''});
935+
expect(helpers._TN('.')).toEqual({table: ''});
936+
expect(helpers._TN('...')).toEqual({table: ''});
937+
});
938+
it('must ignore extras', () => {
939+
expect(helpers._TN('a.b.c')).toEqual({schema: 'a', table: 'b'});
940+
});
941+
it('must support full names', () => {
942+
expect(helpers._TN('ss.tt')).toEqual({schema: 'ss', table: 'tt'});
943+
});
944+
it('must support table-only', () => {
945+
expect(helpers._TN('t1')).toEqual({table: 't1'});
946+
expect(helpers._TN('.t2')).toEqual({table: 't2'});
947+
});
948+
it('must support schema-only', () => {
949+
expect(helpers._TN('ss.')).toEqual({schema: 'ss', table: ''});
950+
});
951+
});
952+
describe('for templates', () => {
953+
it('must support empty', () => {
954+
expect(helpers._TN``).toEqual({table: ''});
955+
expect(helpers._TN`.`).toEqual({table: ''});
956+
expect(helpers._TN`...`).toEqual({table: ''});
957+
});
958+
it('must ignore extras', () => {
959+
expect(helpers._TN`a.b.c`).toEqual({schema: 'a', table: 'b'});
960+
});
961+
it('must support full names', () => {
962+
expect(helpers._TN`ss.tt`).toEqual({schema: 'ss', table: 'tt'});
963+
});
964+
it('must support table-only', () => {
965+
expect(helpers._TN`t1`).toEqual({table: 't1'});
966+
expect(helpers._TN`.t2`).toEqual({table: 't2'});
967+
});
968+
it('must support schema-only', () => {
969+
expect(helpers._TN`ss.`).toEqual({schema: 'ss', table: ''});
970+
});
971+
it('must support schema-only parameter', () => {
972+
const schema = 'ss';
973+
expect(helpers._TN`${schema}.`).toEqual({schema: 'ss', table: ''});
974+
});
975+
it('must support table-only parameter', () => {
976+
const table = 'tt';
977+
expect(helpers._TN`${table}`).toEqual({table: 'tt'});
978+
expect(helpers._TN`.${table}`).toEqual({table: 'tt'});
979+
});
980+
it('must support all parameters', () => {
981+
const schema = 'ss', table = 'tt';
982+
expect(helpers._TN`${schema}.${table}`).toEqual({schema: 'ss', table: 'tt'});
983+
expect(helpers._TN`${schema}.${table}.`).toEqual({schema: 'ss', table: 'tt'});
984+
});
985+
});
986+
});

0 commit comments

Comments
 (0)