Skip to content

Commit 916fa3f

Browse files
authored
Merge pull request #155 from mzpkdev/feat/expose-escape-helpers
feat: expose escapeODataStringLiteral and escapeIllegalChars
2 parents ee1c1d8 + 305b0f1 commit 916fa3f

3 files changed

Lines changed: 86 additions & 4 deletions

File tree

.changeset/eager-otters-escape.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"odata-query": minor
3+
---
4+
5+
- feat: expose `escapeODataStringLiteral` and `escapeIllegalChars` as public exports

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,12 @@ function getStringCollectionClause(lambdaParameter: string, value: any, collecti
306306
return `${propName}/${collectionOperator}(${lambdaParameter}: ${lambdaParameter} ${conditionOperator} '${value}')`;
307307
}
308308

309-
function escapeIllegalChars(string: string) {
310-
string = string.replace(/'/g, "''");
311-
return encodeURIComponent(string);
309+
export function escapeODataStringLiteral(string: string): string {
310+
return string.replace(/'/g, "''");
311+
}
312+
313+
export function escapeIllegalChars(string: string): string {
314+
return encodeURIComponent(escapeODataStringLiteral(string));
312315
}
313316

314317
function buildFilter<T>(filters: Filter<T> = {}, aliases: Alias[] = [], propPrefix = ''): string {

test/index.test.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test as it } from 'vitest'
22

3-
import buildQuery, {Expand, OrderBy, alias, json, ITEM_ROOT, decimal, TypedFilter} from '../src/index';
3+
import buildQuery, {Expand, OrderBy, alias, json, ITEM_ROOT, decimal, TypedFilter, escapeODataStringLiteral, escapeIllegalChars} from '../src/index';
44
import { Square } from './test-interface';
55

66
it('should return an empty string by default', () => {
@@ -1881,3 +1881,77 @@ describe('format', () => {
18811881
expect(actual).toEqual(expected);
18821882
});
18831883
});
1884+
1885+
describe('escapeODataStringLiteral', () => {
1886+
it('should return plain strings unchanged', () => {
1887+
const value = 'Hello';
1888+
const expected = 'Hello';
1889+
const actual = escapeODataStringLiteral(value);
1890+
expect(actual).toEqual(expected);
1891+
});
1892+
1893+
it('should return an empty string unchanged', () => {
1894+
const value = '';
1895+
const expected = '';
1896+
const actual = escapeODataStringLiteral(value);
1897+
expect(actual).toEqual(expected);
1898+
});
1899+
1900+
it('should double a single quote', () => {
1901+
const value = "O'Brien";
1902+
const expected = "O''Brien";
1903+
const actual = escapeODataStringLiteral(value);
1904+
expect(actual).toEqual(expected);
1905+
});
1906+
1907+
it('should double every quote when multiple are present', () => {
1908+
const value = "'a'b'";
1909+
const expected = "''a''b''";
1910+
const actual = escapeODataStringLiteral(value);
1911+
expect(actual).toEqual(expected);
1912+
});
1913+
1914+
it('should not URL-encode reserved characters', () => {
1915+
const value = 'a&b#c d';
1916+
const expected = 'a&b#c d';
1917+
const actual = escapeODataStringLiteral(value);
1918+
expect(actual).toEqual(expected);
1919+
});
1920+
1921+
it('should preserve unicode characters as-is', () => {
1922+
const value = 'café';
1923+
const expected = 'café';
1924+
const actual = escapeODataStringLiteral(value);
1925+
expect(actual).toEqual(expected);
1926+
});
1927+
});
1928+
1929+
describe('escapeIllegalChars', () => {
1930+
it('should not URL-encode single quotes', () => {
1931+
const value = "O'Brien";
1932+
const expected = "O''Brien";
1933+
const actual = escapeIllegalChars(value);
1934+
expect(actual).toEqual(expected);
1935+
});
1936+
1937+
it('should URL-encode reserved characters', () => {
1938+
const value = 'a&b#c d';
1939+
const expected = 'a%26b%23c%20d';
1940+
const actual = escapeIllegalChars(value);
1941+
expect(actual).toEqual(expected);
1942+
});
1943+
1944+
it('should double quotes and URL-encode reserved characters', () => {
1945+
const value = "O'Brien & co";
1946+
const expected = "O''Brien%20%26%20co";
1947+
const actual = escapeIllegalChars(value);
1948+
expect(actual).toEqual(expected);
1949+
});
1950+
1951+
it('should equal encodeURIComponent of escapeODataStringLiteral', () => {
1952+
const value = "O'Brien & co";
1953+
const expected = encodeURIComponent(escapeODataStringLiteral(value));
1954+
const actual = escapeIllegalChars(value);
1955+
expect(actual).toEqual(expected);
1956+
});
1957+
});

0 commit comments

Comments
 (0)