Skip to content

Commit e4251a2

Browse files
AlexGalichenkoOleksandr_Halichenko
andauthored
added feature to resolve objects and arrays (#3)
* added feature to resolve objects and arrays * update version Co-authored-by: Oleksandr_Halichenko <[email protected]>
1 parent 6fae557 commit e4251a2

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

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

src/Memory.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,77 @@ const NUMBER_TYPE_REGEXP = /^\d+|\d+\.\d+$/;
77

88
class Memory {
99

10+
/**
11+
* Get value from memory
12+
* @param {string} str - string to resolve
13+
* @returns {any} - resolved value
14+
* @example const value = memory.getValue('$val');
15+
*/
1016
getValue(str) {
1117
if (KEY_REGEXP.test(str)) return this.getKey(str)
1218
if (PARSE_STRING_REGEXP.test(str)) return this.getString(str)
1319
return str
1420
}
1521

22+
/**
23+
* Set value in memory
24+
* @param {string} key - key to store
25+
* @param {any} value - value to store
26+
* @example memory.setValue('value', 42);
27+
*/
1628
setValue(key, value) {
1729
this[key] = value;
1830
}
1931

32+
/**
33+
* Get key from memory
34+
* @private
35+
* @param {string} str - key to resolve
36+
* @returns {any} - resolved value
37+
*/
2038
getKey(str) {
2139
const keyMatch = str.match(KEY_REGEXP);
2240
const key = keyMatch ? keyMatch[1] : null;
2341
if (key) {
24-
if (!(key in this)) throw new Error(`${key} is not found in memory`);
25-
if (typeof this[key] === 'function') {
42+
const value = this.getProperty(key);
43+
if (typeof value === 'function') {
2644
const params = this.getComputedParams(str);
27-
return this[key].apply(null, params)
45+
return value.apply(null, params)
2846
}
29-
return this[key]
47+
return value
3048
}
3149
}
3250

51+
/**
52+
* Resolve object
53+
* @private
54+
* @param {string} key - key to resolve
55+
* @returns {any} - resolved value
56+
*/
57+
getProperty(key) {
58+
const props = key.replace(/]/g, '').split(/[[.]/).map(prop => prop.replace(QUOTES_REPLACE_REGEXP, ''));
59+
const obj = this[props.shift()];
60+
if (obj === undefined) throw new Error(`${key} is not found in memory`);
61+
return props.reduce((value, prop) => value[prop], obj)
62+
}
63+
64+
/**
65+
* Resolve string with interpolation
66+
* @private
67+
* @param {string} str - string to resolve
68+
* @returns {string} - resolved string
69+
*/
3370
getString(str) {
3471
const matches = str.match(PARSE_STRING_REGEXP).map(match => match.replace(/{|}/g, ``));
3572
return matches.reduce((string, variable) => string.replace(`{${variable}}`, this.getKey(variable)), str);
3673
}
3774

75+
/**
76+
* Extract arguments for computed function
77+
* @private
78+
* @param {string} str - string with params
79+
* @returns {Array<string>} - array of params
80+
*/
3881
getComputedParams(str) {
3982
const paramsString = str.match(KEY_REGEXP);
4083
if (!(paramsString && paramsString[3])) return []
@@ -46,6 +89,10 @@ class Memory {
4689
})
4790
}
4891

92+
/**
93+
* Register memory object
94+
* @param obj - object to register
95+
*/
4996
register(obj) {
5097
for (const prop in obj) {
5198
this[prop] = obj[prop];

tests/memory.spec.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,72 @@ test('throws for query string with one existing and one non-existent variables',
8282
memory.ind1 = 3;
8383
const queryString = 'Component > #{$ind1} of Collection1 > #{$notExists} of Collection2'
8484
expect(() => memory.getValue(queryString)).to.throw('notExists is not found in memory');
85-
});
85+
});
86+
87+
test('property from object in dot notation', () => {
88+
memory.register({
89+
someValue: {
90+
prop: 42
91+
}
92+
});
93+
94+
expect(memory.getValue('$someValue.prop')).to.equal(42);
95+
});
96+
97+
test('property from object in bracket notation', () => {
98+
memory.register({
99+
someValue: {
100+
prop: 42
101+
}
102+
});
103+
104+
expect(memory.getValue('$someValue[\'prop\']')).to.equal(42);
105+
expect(memory.getValue('$someValue["prop"]')).to.equal(42);
106+
});
107+
108+
test('element from array', () => {
109+
memory.register({
110+
someValue: [1,2,3]
111+
});
112+
113+
expect(memory.getValue('$someValue[2]')).to.equal(3);
114+
});
115+
116+
test('property is function (number argument)', () => {
117+
memory.register({
118+
someValue: {
119+
fn: function (val) { return val }
120+
}
121+
});
122+
123+
expect(memory.getValue('$someValue.fn(42)')).to.equal(42);
124+
});
125+
126+
test('property is function (string argument)', () => {
127+
memory.register({
128+
someValue: {
129+
fn: function (val) { return val }
130+
}
131+
});
132+
133+
expect(memory.getValue('$someValue.fn("42")')).to.equal('42');
134+
});
135+
136+
test('property two level', () => {
137+
memory.register({
138+
someValue: {
139+
arr: [
140+
{ prop: 42 }
141+
]
142+
}
143+
});
144+
145+
expect(memory.getValue('$someValue.arr[0].prop')).to.equal(42);
146+
});
147+
148+
test('property from added object', () => {
149+
const obj = { prop: 42 };
150+
memory.setValue('obj', obj);
151+
152+
expect(memory.getValue('$obj.prop')).to.equal(42);
153+
});

0 commit comments

Comments
 (0)