@@ -7,34 +7,77 @@ const NUMBER_TYPE_REGEXP = /^\d+|\d+\.\d+$/;
7
7
8
8
class Memory {
9
9
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
+ */
10
16
getValue ( str ) {
11
17
if ( KEY_REGEXP . test ( str ) ) return this . getKey ( str )
12
18
if ( PARSE_STRING_REGEXP . test ( str ) ) return this . getString ( str )
13
19
return str
14
20
}
15
21
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
+ */
16
28
setValue ( key , value ) {
17
29
this [ key ] = value ;
18
30
}
19
31
32
+ /**
33
+ * Get key from memory
34
+ * @private
35
+ * @param {string } str - key to resolve
36
+ * @returns {any } - resolved value
37
+ */
20
38
getKey ( str ) {
21
39
const keyMatch = str . match ( KEY_REGEXP ) ;
22
40
const key = keyMatch ? keyMatch [ 1 ] : null ;
23
41
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' ) {
26
44
const params = this . getComputedParams ( str ) ;
27
- return this [ key ] . apply ( null , params )
45
+ return value . apply ( null , params )
28
46
}
29
- return this [ key ]
47
+ return value
30
48
}
31
49
}
32
50
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
+ */
33
70
getString ( str ) {
34
71
const matches = str . match ( PARSE_STRING_REGEXP ) . map ( match => match . replace ( / { | } / g, `` ) ) ;
35
72
return matches . reduce ( ( string , variable ) => string . replace ( `{${ variable } }` , this . getKey ( variable ) ) , str ) ;
36
73
}
37
74
75
+ /**
76
+ * Extract arguments for computed function
77
+ * @private
78
+ * @param {string } str - string with params
79
+ * @returns {Array<string> } - array of params
80
+ */
38
81
getComputedParams ( str ) {
39
82
const paramsString = str . match ( KEY_REGEXP ) ;
40
83
if ( ! ( paramsString && paramsString [ 3 ] ) ) return [ ]
@@ -46,6 +89,10 @@ class Memory {
46
89
} )
47
90
}
48
91
92
+ /**
93
+ * Register memory object
94
+ * @param obj - object to register
95
+ */
49
96
register ( obj ) {
50
97
for ( const prop in obj ) {
51
98
this [ prop ] = obj [ prop ] ;
0 commit comments