Skip to content

Commit 6dd316d

Browse files
committed
Freeze Context object
1 parent 4548864 commit 6dd316d

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Tiny utilities shared by the [`regex`](https://github.com/slevithan/regex) libra
1010

1111
### `Context`
1212

13-
Regex syntax context object with the following properties:
13+
Frozen object with the following properties for tracking regex syntax context:
1414

1515
- `DEFAULT` - Base context.
1616
- `CHAR_CLASS` - Character class context.

spec/utilities-spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import {Context, execUnescaped, forEachUnescaped, getGroupContents, hasUnescaped, replaceUnescaped} from '../src/index.js';
22

3+
describe('Context', () => {
4+
it('should not allow modifying property values', () => {
5+
const original = Context.DEFAULT;
6+
try {
7+
Context.DEFAULT = null;
8+
} catch (e) {}
9+
expect(Context.DEFAULT).toBe(original);
10+
});
11+
12+
it('should not allow adding properties', () => {
13+
try {
14+
Context.NEW = 'NEW';
15+
} catch (e) {}
16+
expect('NEW' in Context).toBeFalse();
17+
});
18+
});
19+
320
describe('replaceUnescaped', () => {
421
it('should replace all with string replacement in all contexts', () => {
522
expect(replaceUnescaped(String.raw`.\.\\.\\\.[[\.].].`, '\\.', '~')).toBe(String.raw`~\.\\~\\\.[[\.]~]~`);

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
export const Context = {
1+
// Constant properties for tracking regex syntax context
2+
export const Context = Object.freeze({
23
DEFAULT: 'DEFAULT',
34
CHAR_CLASS: 'CHAR_CLASS',
4-
};
5+
});
56

67
/**
78
Replaces the given pattern only when it's unescaped and in the given context.

0 commit comments

Comments
 (0)