Skip to content

Commit 1ddcb12

Browse files
added support of top-level getters (#26)
* added support of top-level getters Breaking Change: all memory values are now stored in _storage_ property
1 parent 95d1847 commit 1ddcb12

File tree

8 files changed

+73
-33
lines changed

8 files changed

+73
-33
lines changed

CHANGELOG.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ 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.8.0]
8+
- :rocket: added support of top-level getters
9+
Breaking Change: all memory values are now stored in _storage_ property
10+
711
## [1.7.1]
812
- :rocket: improved error handling to produce more clear errors
913

index.d.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,42 @@ declare interface Logger {
33
}
44

55
declare type Memory = {
6+
/**
7+
* Register memory map
8+
* @param memoryMap
9+
* @example
10+
* memory.register({
11+
* key1: 'value1',
12+
* key2: 42
13+
* key3: {
14+
* property: 12
15+
* }
16+
* })
17+
*/
618
register(memoryMap: Object): void;
19+
/**
20+
* Set value to memory
21+
* @param key
22+
* @param value
23+
* @example
24+
* memory.setValue('key', 'value');
25+
*/
726
setValue(key: string, value: any): void;
8-
getValue(key: string): any;
27+
/**
28+
* Get value from memory
29+
* @param key
30+
* @example
31+
* const value = memory.getValue('key');
32+
*/
33+
getValue(key: any): any;
34+
/**
35+
* Set logger
36+
* @param logger
37+
*/
938
setLogger(logger: Logger): void;
1039
}
1140
declare const memory: Memory;
1241
declare module '@qavajs/memory' {
1342
export default memory
1443
}
44+
export default memory;

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.7.1",
3+
"version": "1.8.0",
44
"description": "memory package for @qavajs framework",
55
"main": "index.js",
66
"scripts": {

src/memory.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function toString(value: any): string {
2626

2727
class Memory {
2828

29-
[prop: string]: any;
29+
storage: { [key: string]: any } = {};
3030
logger: { log: (value: any) => void } = { log() {} };
3131

3232
/**
@@ -74,7 +74,7 @@ class Memory {
7474
*/
7575
@readonly
7676
setValue(key: string, value: any) {
77-
this[key] = value;
77+
this.storage[key] = value;
7878
}
7979

8080
/**
@@ -90,7 +90,7 @@ class Memory {
9090
.replace(UNESCAPE_DOLLAR_REGEXP, '$')
9191
);
9292
try {
93-
return getFunction.apply(this);
93+
return getFunction.apply(this.storage);
9494
} catch (err: any) {
9595
this.reportError(err);
9696
}
@@ -102,28 +102,18 @@ class Memory {
102102
*/
103103
@readonly
104104
register(obj: { [prop: string]: any }) {
105-
for (const prop in obj) {
106-
this[prop] = obj[prop];
105+
this.storage = obj;
106+
this.storage.js = function(expression: any) {
107+
return expression;
107108
}
108109
}
109110

110-
/**
111-
* Evaluate js expression
112-
* @param {any} expression - expression to evaluate
113-
* @example $js($val + 1)
114-
*/
115-
@readonly
116-
js(expression: any) {
117-
return expression;
118-
}
119-
120111
@readonly
121112
setLogger(logger: { log: (value: any) => void }) {
122113
this.logger = logger;
123114
}
124115

125-
@readonly
126-
reportError(err: any) {
116+
private reportError(err: any) {
127117
err.message = err.message.replace(/this\./g, '$');
128118
throw err;
129119
}

tests/dataTypes.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import {test, expect} from 'vitest';
1+
import {test, expect, beforeEach} from 'vitest';
22
import memory from '../src/memory';
33

4+
beforeEach(() => {
5+
memory.register({});
6+
});
7+
48
test('number', () => {
59
expect(memory.getValue('$js(1)')).to.equal(1);
610
});

tests/memory.spec.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
1-
import {test, expect} from 'vitest';
1+
import {beforeEach, test, expect} from 'vitest';
22
import memory from '../src/memory';
33

4+
beforeEach(() => {
5+
memory.register({});
6+
});
7+
48
test('computed without params returns value', () => {
5-
memory.computed = function () {
9+
memory.storage.computed = function () {
610
return 42
711
}
812

913
expect(memory.getValue('$computed()')).to.equal(42);
1014
});
1115

1216
test('computed with number param returns value', () => {
13-
memory.computed = function (val: any) {
17+
memory.storage.computed = function (val: any) {
1418
return val
1519
}
1620

1721
expect(memory.getValue('$computed(42)')).to.equal(42);
1822
});
1923

2024
test('computed with string param returns value', () => {
21-
memory.computed = function (val: any) {
25+
memory.storage.computed = function (val: any) {
2226
return val
2327
}
2428

2529
expect(memory.getValue('$computed("test string")')).to.equal("test string");
2630
});
2731

2832
test('computed with memory param returns value', () => {
29-
memory.computed = function (val1: any, val2: any, val3: any) {
33+
memory.storage.computed = function (val1: any, val2: any, val3: any) {
3034
return val1 + val2 + val3
3135
}
3236

33-
memory.anotherComputed = function () {
37+
memory.storage.anotherComputed = function () {
3438
return 42
3539
}
3640

37-
memory.someVal = 42;
41+
memory.storage.someVal = 42;
3842

3943
expect(memory.getValue('$computed(42, $anotherComputed(), $someVal)')).to.equal(126);
4044
});
@@ -54,15 +58,15 @@ test('simple string returns as is', () => {
5458
// Query tests
5559

5660
test('query string with one existing variable', () => {
57-
memory.num = 1;
61+
memory.storage.num = 1;
5862
const queryString = 'Component > #{$num} of Collection'
5963
const expected = `Component > #1 of Collection`;
6064
expect(memory.getValue(queryString)).to.equal(expected);
6165
});
6266

6367
test('query string with two existing variables', () => {
64-
memory.ind1 = 1;
65-
memory.ind2 = 15;
68+
memory.storage.ind1 = 1;
69+
memory.storage.ind2 = 15;
6670
const queryString = 'Component > #{$ind1} of Collection1 > #{$ind2} of Collection2'
6771
const expected = `Component > #1 of Collection1 > #15 of Collection2`;
6872
expect(memory.getValue(queryString)).to.equal(expected);
@@ -298,7 +302,6 @@ test('interpolation without memory values', () => {
298302
});
299303

300304
test('non string input', () => {
301-
//@ts-ignore
302305
expect(memory.getValue(42)).to.equal(42);
303306
});
304307

@@ -311,6 +314,15 @@ test('correct error message', () => {
311314
expect(() => memory.getValue('$x()')).toThrow('$x is not a function');
312315
});
313316

317+
test('getter', () => {
318+
let closure = 1;
319+
memory.register({
320+
get x() { return closure++ }
321+
});
322+
expect(memory.getValue('$x')).to.equal(1);
323+
expect(memory.getValue('$x')).to.equal(2);
324+
});
325+
314326

315327

316328

tests/memory_singleton.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
const memory = require('../index');
22

3-
memory.singletonVal = 'singleton cjs';
3+
memory.setValue('singletonVal', 'singleton cjs');

tests/memory_singleton.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import memory from '../index.js';
22

3-
memory.singletonVal = 'singleton mjs';
3+
memory.setValue('singletonVal', 'singleton mjs');

0 commit comments

Comments
 (0)