Conner 249182ea9f
Document client.escapeIdentifier and client.escapeLiteral (#2954)
* Document client.escapeIdentifier and client.escapeLiteral

Per #1978 it seems that these client APIs are undocumented. Added documentation for these functions along with some examples and relevant links.

* Fix typos in new docs

* Migrate escapeIdentifier and escapeLiteral from Client to PG

These are standalone utility functions, they do not need a client instance to function.

Changes made:
- Refactored escapeIdentifer and escapeLiteral from client class to functions in utils
- Update PG to export  escapeIdentifier and escapeLiteral
- Migrated tests for Client.escapeIdentifier and Client.escapeLiteral to tests for utils
- Updated documentation, added a "utilities" page where these helpers are discussed

**note** this is a breaking change. Users who used these functions (previously undocumented) on instances of Client, or via Client.prototype.

* Export escapeIdentifier and escapeLiteral from PG

These are standalone utility functions, they should not depend on a client instance.

Changes made:
- Refactored escapeIdentifer and escapeLiteral from client class to functions in utils
- Re-exported functions on client for backwards compatibility
- Update PG to export  escapeIdentifier and escapeLiteral
- Updated tests to validate the newly exported functions from both entry points
- Updated documentation, added a "utilities" page where these helpers are discussed

* Ensure escape functions work via Client.prototype

Updated changes such that escapeIdentifier and escapeLiteral are usable via the client prototype
Updated tests to check for both entry points in client
2023-05-02 07:55:59 -05:00

89 lines
2.9 KiB
JavaScript

'use strict'
var helper = require('./test-helper')
var utils = require('../../../lib/utils')
function createClient(callback) {
var client = new Client(helper.config)
client.connect(function (err) {
return callback(client)
})
}
var testLit = function (testName, input, expected) {
test(testName, function () {
var client = new Client(helper.config)
var actual = client.escapeLiteral(input)
assert.equal(expected, actual)
})
test('Client.prototype.' + testName, function () {
var actual = Client.prototype.escapeLiteral(input)
assert.equal(expected, actual)
})
test('utils.' + testName, function () {
var actual = utils.escapeLiteral(input)
assert.equal(expected, actual)
})
}
var testIdent = function (testName, input, expected) {
test(testName, function () {
var client = new Client(helper.config)
var actual = client.escapeIdentifier(input)
assert.equal(expected, actual)
})
test('Client.prototype.' + testName, function () {
var actual = Client.prototype.escapeIdentifier(input)
assert.equal(expected, actual)
})
test('utils.' + testName, function () {
var actual = utils.escapeIdentifier(input)
assert.equal(expected, actual)
})
}
testLit('escapeLiteral: no special characters', 'hello world', "'hello world'")
testLit('escapeLiteral: contains double quotes only', 'hello " world', "'hello \" world'")
testLit('escapeLiteral: contains single quotes only', "hello ' world", "'hello '' world'")
testLit('escapeLiteral: contains backslashes only', 'hello \\ world', " E'hello \\\\ world'")
testLit('escapeLiteral: contains single quotes and double quotes', 'hello \' " world', "'hello '' \" world'")
testLit('escapeLiteral: contains double quotes and backslashes', 'hello \\ " world', " E'hello \\\\ \" world'")
testLit('escapeLiteral: contains single quotes and backslashes', "hello \\ ' world", " E'hello \\\\ '' world'")
testLit(
'escapeLiteral: contains single quotes, double quotes, and backslashes',
'hello \\ \' " world',
" E'hello \\\\ '' \" world'"
)
testIdent('escapeIdentifier: no special characters', 'hello world', '"hello world"')
testIdent('escapeIdentifier: contains double quotes only', 'hello " world', '"hello "" world"')
testIdent('escapeIdentifier: contains single quotes only', "hello ' world", '"hello \' world"')
testIdent('escapeIdentifier: contains backslashes only', 'hello \\ world', '"hello \\ world"')
testIdent('escapeIdentifier: contains single quotes and double quotes', 'hello \' " world', '"hello \' "" world"')
testIdent('escapeIdentifier: contains double quotes and backslashes', 'hello \\ " world', '"hello \\ "" world"')
testIdent('escapeIdentifier: contains single quotes and backslashes', "hello \\ ' world", '"hello \\ \' world"')
testIdent(
'escapeIdentifier: contains single quotes, double quotes, and backslashes',
'hello \\ \' " world',
'"hello \\ \' "" world"'
)