pacc 4.9.0 → 4.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/package.json +1 -1
- package/src/expression.mjs +18 -4
- package/src/settergetter.mjs +21 -1
- package/src/tokens.mjs +1 -1
package/README.md
CHANGED
|
@@ -122,6 +122,7 @@ const result = getAttribute({ a: [0,{ b: 4 }]}, "a[1].b");
|
|
|
122
122
|
* [DOUBLE\_AMPERSAND](#double_ampersand)
|
|
123
123
|
* [BAR](#bar)
|
|
124
124
|
* [DOUBLE\_BAR](#double_bar)
|
|
125
|
+
* [EOF](#eof)
|
|
125
126
|
|
|
126
127
|
## prepareAttributesDefinitions
|
|
127
128
|
|
|
@@ -444,6 +445,8 @@ Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Globa
|
|
|
444
445
|
### Parameters
|
|
445
446
|
|
|
446
447
|
* `str` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
|
|
448
|
+
* `precedence` (optional, default `0`)
|
|
449
|
+
* `type`  
|
|
447
450
|
|
|
448
451
|
Returns **[Token](#token)** 
|
|
449
452
|
|
|
@@ -551,6 +554,10 @@ Type: [Token](#token)
|
|
|
551
554
|
|
|
552
555
|
Type: [Token](#token)
|
|
553
556
|
|
|
557
|
+
## EOF
|
|
558
|
+
|
|
559
|
+
Type: [Token](#token)
|
|
560
|
+
|
|
554
561
|
# install
|
|
555
562
|
|
|
556
563
|
With [npm](http://npmjs.org) do:
|
package/package.json
CHANGED
package/src/expression.mjs
CHANGED
|
@@ -21,6 +21,11 @@ import {
|
|
|
21
21
|
export function parse(context) {
|
|
22
22
|
let node, token;
|
|
23
23
|
|
|
24
|
+
function error(message) {
|
|
25
|
+
const error = new Error(message);
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
|
|
24
29
|
const advance = () => {
|
|
25
30
|
const { value, done } = context.tokens.next();
|
|
26
31
|
token = done ? EOF : value;
|
|
@@ -28,10 +33,7 @@ export function parse(context) {
|
|
|
28
33
|
|
|
29
34
|
const expect = expected => {
|
|
30
35
|
if (token !== expected) {
|
|
31
|
-
|
|
32
|
-
`unexpected '${token.str}' expecting '${expected.str}'`
|
|
33
|
-
);
|
|
34
|
-
throw error;
|
|
36
|
+
error(`unexpected '${token.str}' expecting '${expected.str}'`);
|
|
35
37
|
}
|
|
36
38
|
advance();
|
|
37
39
|
};
|
|
@@ -48,10 +50,15 @@ export function parse(context) {
|
|
|
48
50
|
case OPEN_BRACKET: {
|
|
49
51
|
const node = expression(0);
|
|
50
52
|
expect(CLOSE_BRACKET);
|
|
53
|
+
if (typeof node === "number") {
|
|
54
|
+
return { path: [node] };
|
|
55
|
+
}
|
|
51
56
|
return node;
|
|
52
57
|
}
|
|
53
58
|
}
|
|
54
59
|
return { token, left, right: expression(token.precedence) };
|
|
60
|
+
case "eof":
|
|
61
|
+
error("unexpected EOF");
|
|
55
62
|
}
|
|
56
63
|
|
|
57
64
|
switch (typeof token) {
|
|
@@ -85,9 +92,16 @@ export function parse(context) {
|
|
|
85
92
|
left.path.push(...right.path);
|
|
86
93
|
return left;
|
|
87
94
|
}
|
|
95
|
+
if (typeof left === "number") {
|
|
96
|
+
right.path.unshift(left);
|
|
97
|
+
return right;
|
|
98
|
+
}
|
|
88
99
|
return { path: [left.token, right.token] };
|
|
89
100
|
}
|
|
90
101
|
|
|
102
|
+
if (right.token === EOF) {
|
|
103
|
+
error("unexpected EOF");
|
|
104
|
+
}
|
|
91
105
|
return {
|
|
92
106
|
token,
|
|
93
107
|
left,
|
package/src/settergetter.mjs
CHANGED
|
@@ -58,7 +58,27 @@ export function setAttribute(object, expression, value) {
|
|
|
58
58
|
* @returns {any} value associated with the given property name
|
|
59
59
|
*/
|
|
60
60
|
export function getAttribute(object, expression) {
|
|
61
|
-
|
|
61
|
+
const { path } = parse({ tokens: tokens(expression) });
|
|
62
|
+
|
|
63
|
+
for (const key of path) {
|
|
64
|
+
if (object !== undefined) {
|
|
65
|
+
switch (typeof object[key]) {
|
|
66
|
+
case "function":
|
|
67
|
+
object = object[key]();
|
|
68
|
+
if (typeof object[Symbol.iterator] === "function") {
|
|
69
|
+
object = [...object];
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
object = object[key];
|
|
74
|
+
break;
|
|
75
|
+
case "undefined":
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return object;
|
|
62
82
|
}
|
|
63
83
|
|
|
64
84
|
/**
|
package/src/tokens.mjs
CHANGED
|
@@ -53,7 +53,7 @@ export /** @type {Token} */ const DOUBLE_AMPERSAND = createToken(
|
|
|
53
53
|
);
|
|
54
54
|
export /** @type {Token} */ const BAR = createToken("|");
|
|
55
55
|
export /** @type {Token} */ const DOUBLE_BAR = createToken("||", 30, "infixr");
|
|
56
|
-
export /** @type {Token} */ const EOF = createToken("EOF", -1);
|
|
56
|
+
export /** @type {Token} */ const EOF = createToken("EOF", -1, "eof");
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
59
|
* Split property path into tokens
|