From 62ecedd7654c3e52028773c6f4d40ea3dc11d4e1 Mon Sep 17 00:00:00 2001
From: Dmytro-Melnyshyn <Dmytro_Melnyshyn@epam.com>
Date: Mon, 24 Jul 2023 12:46:57 +0300
Subject: [PATCH] STUTL-37: Add  to only escape  and not escape .

---
 CHANGELOG.md                            |  1 +
 lib/escapeCqlValueAllowAsterisk.js      | 10 ++++++++++
 lib/escapeCqlValueAllowAsterisk.test.js | 14 ++++++++++++++
 3 files changed, 25 insertions(+)
 create mode 100644 lib/escapeCqlValueAllowAsterisk.js
 create mode 100644 lib/escapeCqlValueAllowAsterisk.test.js

diff --git a/CHANGELOG.md b/CHANGELOG.md
index ec43480..1892ae3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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)
diff --git a/lib/escapeCqlValueAllowAsterisk.js b/lib/escapeCqlValueAllowAsterisk.js
new file mode 100644
index 0000000..5ed8767
--- /dev/null
+++ b/lib/escapeCqlValueAllowAsterisk.js
@@ -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);
+}
diff --git a/lib/escapeCqlValueAllowAsterisk.test.js b/lib/escapeCqlValueAllowAsterisk.test.js
new file mode 100644
index 0000000..dc35605
--- /dev/null
+++ b/lib/escapeCqlValueAllowAsterisk.test.js
@@ -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);
+  });
+});