Skip to content

Commit 9493fb8

Browse files
fixed issue with parsing json (#31)
* fixed issue with parsing json * improved parsing logic
1 parent 53252d2 commit 9493fb8

File tree

6 files changed

+26
-27
lines changed

6 files changed

+26
-27
lines changed

CHANGELOG.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to the "@qavajs/memory" will be documented in this file.
44

55
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
66

7+
## [1.10.1]
8+
- :beetle: fixed issue with parsing json
9+
710
## [1.10.0]
811
- :rocket: added export of Memory type
912
- :beetle: fixed issue with nested curly braces

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/memory",
3-
"version": "1.10.0",
3+
"version": "1.10.1",
44
"description": "memory package for @qavajs framework",
55
"main": "index.js",
66
"scripts": {

src/memory.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ class Memory {
6464
*/
6565
@readonly
6666
getString(str: string): any {
67-
const matches = this.extractExpressions(str).filter(match => PARSE_STRING_REGEXP.exec(match));
68-
if (!matches) return str;
69-
return matches
70-
.map((match: string) => match.replace(/(^{|}$)/g, ``))
71-
.reduce((str: string, variable: any) => str.replace(`{${variable}}`, this.getKey(variable)), str)
72-
.replace(UNESCAPE_DOLLAR_REGEXP, '$');
67+
const getFunction = new Function(
68+
`return \`${str.replaceAll(`{$`, '${this.')}\``
69+
.replace(UNESCAPE_DOLLAR_REGEXP, '$')
70+
);
71+
try {
72+
return getFunction.apply(this.storage);
73+
} catch (err: any) {
74+
throw new MemoryError(err);
75+
}
7376
}
7477

7578
/**
@@ -93,7 +96,7 @@ class Memory {
9396
@readonly
9497
getKey(str: string): any {
9598
const getFunction = new Function(
96-
`return ${str.replace(/\$/g, 'this.')}`
99+
`return ${str.replaceAll('$', 'this.')}`
97100
.replace(UNESCAPE_DOLLAR_REGEXP, '$')
98101
);
99102
try {
@@ -120,22 +123,6 @@ class Memory {
120123
this.logger = logger;
121124
}
122125

123-
private extractExpressions(text: string) {
124-
const braces = [];
125-
const stack = [];
126-
127-
for (let i = 0; i < text.length; i++) {
128-
if (text[i] === '{') {
129-
stack.push(i);
130-
} else if (text[i] === '}' && stack.length > 0) {
131-
const start = stack.pop();
132-
braces.push(text.substring(start as number, i + 1));
133-
}
134-
}
135-
136-
return braces;
137-
}
138-
139126
}
140127

141128
export default new Memory();

tests/memory.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ test('correct error message', () => {
314314
expect(() => memory.getValue('$x()')).toThrow('$x is not a function');
315315
});
316316

317+
test('correct error message in template string', () => {
318+
expect(() => memory.getValue('{$x()}')).toThrow('$x is not a function');
319+
});
320+
317321
test('getter', () => {
318322
let closure = 1;
319323
memory.register({
@@ -328,6 +332,11 @@ test('escape curly braces', () => {
328332
expect(memory.getValue('template {$fn("{inner value}")}')).to.equal('template inner value');
329333
});
330334

335+
test('resolve json', () => {
336+
memory.setValue('randomName', (text: string) => 'random');
337+
const json = `{"email": "{$randomName(6)}+{$randomName(6)}"}`
338+
expect(memory.getValue(json)).to.equal('{"email": "random+random"}');
339+
});
331340

332341

333342

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"test/**/*.spec.ts"
77
],
88
"compilerOptions": {
9-
"target": "es2017",
9+
"target": "es2021",
1010
"module": "node16",
1111
"moduleResolution": "node16",
1212
"outDir": "./lib",

0 commit comments

Comments
 (0)