Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STUTL-45 implement escapeCqlWildcards #86

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 6.2.0 IN PROGRESS

* Implement `escapeCqlWildcards` to escape ALL CQL wildcards. Refs STUTL-45, STUTL-33.

## [6.1.0](https://github.com/folio-org/stripes-util/tree/v6.1.0) (2024-03-12)
[Full Changelog](https://github.com/folio-org/stripes-util/compare/v6.0.0...v6.1.0)

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as effectiveCallNumber } from './lib/effectiveCallNumber';
export { default as escapeCqlValue } from './lib/escapeCqlValue';
export { default as escapeCqlWildcards } from './lib/escapeCqlWildcards';
export { default as exportCsv } from './lib/exportCsv';
export { default as getFullName } from './lib/getFullName';
export { default as parseJwt } from './lib/parseJwt';
Expand Down
6 changes: 5 additions & 1 deletion lib/escapeCqlValue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/**
* Escape quote (") and backslash (\) characters in a string by pre-pending
* them with a single backslash.
* them with a single backslash. This function preserves asterisk (*),
* question mark (?) and caret (^), allowing them to function as wildcards
* in the query.
*
* @see escapeCqlWildcards for a function that escapes all CQL special characters
*
* @param string a string
* @return string the input string with quotes and backslashes escaped
Expand Down
22 changes: 22 additions & 0 deletions lib/escapeCqlWildcards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Escape all CQL wildcard characters in a string by pre-pending each with
* a single backslash (\). Wildcards are given by
* https://www.loc.gov/standards/sru/cql/contextSets/theCqlContextSet.html
* (see the section "Masking"):
*
* * asterisk (*)
* * question mark (?)
* * caret (^) (sic)
* * backslash (\)
* * quote (")
*
* @see escapeCqlValue for a function that only escapes quote and backslash,
* desirable functionality when the argument wants those characters to
* function as wildcards.
*
* @param string a string
* @return string the input string with CQL's special characters escaped
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSDoc of escapeCqlWildcards and escapeCqlValue may refer to each other using @see and @link.

*/
export default function escapeCqlWildcards(str) {
return str.replace(/[*?^"\\]/g, c => '\\' + c);
}
33 changes: 33 additions & 0 deletions lib/escapeCqlWildcards.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import escapeCqlWildcards from './escapeCqlWildcards';

describe('correctly escapes CQL special characters', () => {
test('does not modify non-special strings', () => {
const str = 'abc';
expect(escapeCqlWildcards(str)).toEqual(str);
});

test('escapes asterisk (*) with a backslash', () => {
const str = 'a*b*c';
expect(escapeCqlWildcards(str)).toEqual('a\\*b\\*c');
});

test('escapes question mark (?) with a backslash', () => {
const str = 'a?b?c';
expect(escapeCqlWildcards(str)).toEqual('a\\?b\\?c');
});

test('escapes caret (^) with a backslash', () => {
const str = 'a^b^c';
expect(escapeCqlWildcards(str)).toEqual('a\\^b\\^c');
});

test('escapes quote (") with a backslash', () => {
const str = 'a"b"c';
expect(escapeCqlWildcards(str)).toEqual('a\\"b\\"c');
});

test('escapes backslash (\\) with a backslash', () => {
const str = 'a\\b\\c';
expect(escapeCqlWildcards(str)).toEqual('a\\\\b\\\\c');
});
});
Loading