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

Revert "STUTL-33: escapeCqlValue for " \ ^ * ?" #74

Merged
merged 1 commit into from
Jul 24, 2023
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
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* Add `getHeaderWithCredentials` for leverage cookie-based authentication in all API requests. Refs STUTL-32.
* Add `getSourceSuppressor` to build action suppressor based on an entry sources. Refs STUTL-34.
* *BREAKING* Bump `react` to `v18`. Refs STUTL-35.
* *BREAKING* `escapeCqlValue` escapes `" \ ^ * ?`. Refs STUTL-33.

## [5.2.1](https://github.com/folio-org/stripes-util/tree/v5.2.1) (2023-01-30)
[Full Changelog](https://github.com/folio-org/stripes-util/compare/v5.2.0...v5.2.1)
Expand Down
9 changes: 4 additions & 5 deletions lib/escapeCqlValue.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/**
* Escape quote ("), backslash (\), caret(^), star (*) and question mark (?)
* characters in a string by pre-pending them with a single backslash.
* See https://www.loc.gov/standards/sru/cql/contextSets/theCqlContextSet.html
* Escape quote (") and backslash (\) characters in a string by pre-pending
* them with a single backslash.
*
* @param string a string
* @return string the input string with the five special CQL characters masked
* @return string the input string with quotes and backslashes escaped
*/
export default function escapeCqlValue(str) {
return str.replace(/["\\^*?]/g, c => '\\' + c);
return str.replace(/"|\\/g, c => '\\' + c);
}
22 changes: 14 additions & 8 deletions lib/escapeCqlValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import { describe, expect, test } from '@jest/globals';

import escapeCqlValue from './escapeCqlValue';

describe('escapeCqlValue masks all five CQL special characters', () => {
test.each([
['', ''],
['foo_bar baz%', 'foo_bar baz%'],
['f"o\\o^b*a?r', 'f\\"o\\\\o\\^b\\*a\\?r'],
['?*^\\"??**^^\\\\""', '\\?\\*\\^\\\\\\"\\?\\?\\*\\*\\^\\^\\\\\\\\\\"\\"'],
])('escapeCqlValue(%p) should be %p', (raw, expected) => {
expect(escapeCqlValue(raw)).toEqual(expected);
describe('correctly escapes CQL special characters', () => {
test('does not modify non-special strings', () => {
const str = 'abc';
expect(escapeCqlValue(str)).toEqual(str);
});

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

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