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-37: Add escapeCqlValueAllowAsterisk to only escape " \ ^ ? and not escape *. #73

Closed
wants to merge 1 commit into from
Closed
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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* 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.
* Add `escapeCqlValueAllowAsterisk` to only escape `" \ ^ ?` and not escape `*`. Refs STUTL-37.

## [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
10 changes: 10 additions & 0 deletions lib/escapeCqlValueAllowAsterisk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Escape quote ("), backslash (\), caret(^), question mark (?) characters in a string
* by pre-pending them with a single backslash. Don't escape asterisk (*).
*
* @param string a string
* @return string the input string with the four special CQL characters masked
*/
export default function escapeCqlValueAllowAsterisk(str) {
return str.replace(/["\\^?]/g, c => '\\' + c);
}
14 changes: 14 additions & 0 deletions lib/escapeCqlValueAllowAsterisk.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, test } from '@jest/globals';

import escapeCqlValueAllowAsterisk from './escapeCqlValueAllowAsterisk';

describe('escapeCqlValueAllowAsterisk only escapes four CQL special characters and does not escape asterisk', () => {
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(escapeCqlValueAllowAsterisk(raw)).toEqual(expected);
});
});