pacc 1.0.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/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2023 by pacc
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ [![npm](https://img.shields.io/npm/v/pacc.svg)](https://www.npmjs.com/package/pacc)
2
+ [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
3
+ [![bundlejs](https://deno.bundlejs.com/?q=pacc\&badge=detailed)](https://bundlejs.com/?q=pacc)
4
+ [![downloads](http://img.shields.io/npm/dm/pacc.svg?style=flat-square)](https://npmjs.org/package/pacc)
5
+ [![GitHub Issues](https://img.shields.io/github/issues/arlac77/pacc.svg?style=flat-square)](https://github.com/arlac77/pacc/issues)
6
+ [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Farlac77%2Fpacc%2Fbadge\&style=flat)](https://actions-badge.atrox.dev/arlac77/pacc/goto)
7
+ [![Styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
8
+ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
9
+ [![Known Vulnerabilities](https://snyk.io/test/github/arlac77/pacc/badge.svg)](https://snyk.io/test/github/arlac77/pacc)
10
+ [![Coverage Status](https://coveralls.io/repos/arlac77/pacc/badge.svg)](https://coveralls.io/github/arlac77/pacc)
11
+
12
+ # pacc
13
+
14
+ propetty path utils
15
+
16
+ # API
17
+
18
+ # install
19
+
20
+ With [npm](http://npmjs.org) do:
21
+
22
+ ```shell
23
+ npm install pacc
24
+ ```
25
+
26
+ # license
27
+
28
+ BSD-2-Clause
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "pacc",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "property path utils",
8
+ "contributors": [
9
+ {
10
+ "name": "Markus Felten",
11
+ "email": "markus.felten@gmx.de"
12
+ }
13
+ ],
14
+ "exports": {
15
+ ".": "./src/attribute.mjs"
16
+ },
17
+ "license": "BSD-2-Clause",
18
+ "scripts": {
19
+ "test": "npm run test:browser-ava && npm run test:ava",
20
+ "test:browser-ava": "browser-ava --headless --no-keep-open tests/*-ava.mjs tests/*-ava-browser.mjs",
21
+ "test:ava": "ava --timeout 4m tests/*-ava.mjs tests/*-ava-node.mjs",
22
+ "cover": "c8 -x 'tests/**/*' --temp-directory build/tmp ava --timeout 4m tests/*-ava.mjs tests/*-ava-node.mjs && c8 report -r lcov -o build/coverage --temp-directory build/tmp",
23
+ "docs": "documentation readme --section=API ./src/**/*.mjs",
24
+ "lint": "npm run lint:docs",
25
+ "lint:docs": "documentation lint ./src/**/*.mjs"
26
+ },
27
+ "devDependencies": {
28
+ "ava": "^5.3.1",
29
+ "browser-ava": "^2.0.0",
30
+ "c8": "^8.0.1",
31
+ "documentation": "^14.0.2",
32
+ "semantic-release": "^22.0.6"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/arlac77/pacc"
37
+ },
38
+ "bugs": {
39
+ "url": "https://github.com/arlac77/pacc/issues"
40
+ },
41
+ "homepage": "https://github.com/arlac77/pacc#readme",
42
+ "template": {
43
+ "inheritFrom": [
44
+ "arlac77/template-arlac77-github",
45
+ "arlac77/template-browser-ava",
46
+ "arlac77/template-esm-only"
47
+ ]
48
+ }
49
+ }
@@ -0,0 +1,295 @@
1
+ /**
2
+ * @typedef {Object} AttributeDefinition
3
+ *
4
+ * @property {string} type
5
+ * @property {boolean} writable
6
+ * @property {boolean} [private] should the value be shown
7
+ * @property {string} [depends] name of an attribute we depend on
8
+ * @property {string[]} additionalAttributes extra attributes that are present in case our attribute is set
9
+ * @property {string} description
10
+ * @property {any} [default] the default value
11
+ * @property {Function} [set] set the value
12
+ * @property {Function} [get] get the value can be used to calculate default values
13
+ * @property {string[]|string} [env] environment variable use to provide the value
14
+ */
15
+
16
+ /**
17
+ * Split property path into tokens
18
+ * @generator
19
+ * @param {string} string
20
+ * @yields {string}
21
+ */
22
+ export function* tokens(string) {
23
+ let state, buffer;
24
+
25
+ for (const c of string) {
26
+ switch (state) {
27
+ case "string-escaping":
28
+ const esc = {
29
+ "\\": "\\",
30
+ t: "\t",
31
+ b: "\b",
32
+ r: "\r",
33
+ n: "\n",
34
+ f: "\f"
35
+ };
36
+ buffer += esc[c];
37
+ state = "string";
38
+ continue;
39
+ }
40
+
41
+ switch (c) {
42
+ case "\t":
43
+ case " ":
44
+ switch (state) {
45
+ case undefined:
46
+ break;
47
+ case "string":
48
+ buffer += c;
49
+ break;
50
+ case "identifier":
51
+ yield buffer;
52
+ buffer = "";
53
+ state = undefined;
54
+ break;
55
+ default:
56
+ yield state;
57
+ state = undefined;
58
+ }
59
+ break;
60
+
61
+ case "\\":
62
+ switch (state) {
63
+ case "string":
64
+ state = "string-escaping";
65
+ break;
66
+ }
67
+ break;
68
+ case '"':
69
+ case "'":
70
+ switch (state) {
71
+ case undefined:
72
+ buffer = "";
73
+ state = "string";
74
+ break;
75
+ case "string":
76
+ yield buffer;
77
+ state = undefined;
78
+ break;
79
+ case "identifier":
80
+ yield buffer;
81
+ buffer = "";
82
+ state = "string";
83
+ break;
84
+ default:
85
+ yield state;
86
+ buffer = "";
87
+ state = "string";
88
+ }
89
+ break;
90
+ case "!":
91
+ case ">":
92
+ case "<":
93
+ switch (state) {
94
+ case undefined:
95
+ state = c;
96
+ break;
97
+ case "string":
98
+ buffer += c;
99
+ break;
100
+ case "identifier":
101
+ yield buffer;
102
+ state = c;
103
+ break;
104
+ default:
105
+ yield state;
106
+ state = c;
107
+ }
108
+ break;
109
+
110
+ case "=":
111
+ switch (state) {
112
+ case undefined:
113
+ state = c;
114
+ break;
115
+ case "string":
116
+ buffer += c;
117
+ break;
118
+ case "identifier":
119
+ yield buffer;
120
+ state = c;
121
+ break;
122
+ default:
123
+ state += c;
124
+ }
125
+ break;
126
+ case ".":
127
+ case "+":
128
+ case "-":
129
+ case "*":
130
+ case "/":
131
+ case "(":
132
+ case ")":
133
+ case "[":
134
+ case "]":
135
+ switch (state) {
136
+ case undefined:
137
+ state = c;
138
+ break;
139
+ case "string":
140
+ buffer += c;
141
+ break;
142
+ case "identifier":
143
+ yield buffer;
144
+ state = c;
145
+ break;
146
+ default:
147
+ yield state;
148
+ state = c;
149
+ }
150
+ break;
151
+ default:
152
+ switch (state) {
153
+ case undefined:
154
+ buffer = c;
155
+ state = "identifier";
156
+ break;
157
+ case "string":
158
+ case "identifier":
159
+ buffer += c;
160
+ break;
161
+ default:
162
+ if (
163
+ (c >= "a" && c <= "z") ||
164
+ (c >= "A" && c <= "Z") ||
165
+ (c >= "0" && c <= "9") ||
166
+ c === "_"
167
+ ) {
168
+ yield state;
169
+ state = "identifier";
170
+ buffer = c;
171
+ } else {
172
+ state += c;
173
+ }
174
+ }
175
+ }
176
+ }
177
+
178
+ switch (state) {
179
+ case undefined:
180
+ break;
181
+ case "string":
182
+ throw new Error("unterminated string");
183
+ case "identifier":
184
+ yield buffer;
185
+ break;
186
+ default:
187
+ yield state;
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Set Object attribute.
193
+ * The name may be a property path like 'a.b.c'.
194
+ * @param {Object} object
195
+ * @param {string} expression
196
+ * @param {any} value
197
+ */
198
+ export function setAttribute(object, expression, value) {
199
+ let lastObject = object;
200
+ let lastKey;
201
+
202
+ for (const token of tokens(expression)) {
203
+ switch (token) {
204
+ case ">":
205
+ case "<":
206
+ case ".":
207
+ case "[":
208
+ case "]":
209
+ break;
210
+
211
+ default:
212
+ if (object[token] === undefined || typeof object[token] !== "object") {
213
+ object[token] = {};
214
+ }
215
+
216
+ lastObject = object;
217
+ lastKey = token;
218
+
219
+ object = object[token];
220
+ }
221
+ }
222
+
223
+ if (lastKey) {
224
+ lastObject[lastKey] = value;
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Deliver attribute value.
230
+ * The name may be a property path like 'a.b.c' or a[2]
231
+ * @param {Object} object
232
+ * @param {string} expression
233
+ * @returns {any} value associated with the given property name
234
+ */
235
+ export function getAttribute(object, expression) {
236
+ return getAttributeAndOperator(object, expression)[0];
237
+ }
238
+
239
+ /**
240
+ * Deliver attribute value and operator.
241
+ * The name may be a property path like 'a.b.c <='.
242
+ * @param {Object} object
243
+ * @param {string} expression
244
+ * @returns {[any,string]} value associated with the given property name
245
+ */
246
+ export function getAttributeAndOperator(object, expression, getters = {}) {
247
+ let op = "=";
248
+ let predicateTokens;
249
+
250
+ for (const token of tokens(expression)) {
251
+ switch (token) {
252
+ case ">=":
253
+ case "<=":
254
+ case ">":
255
+ case "<":
256
+ case "=":
257
+ case "!=":
258
+ op = token;
259
+ break;
260
+ case ".":
261
+ case "[":
262
+ predicateTokens = [];
263
+ break;
264
+ case "]":
265
+ // TODO: should loop over array actually getAttribute api should deliver iterators
266
+ if (object[Symbol.iterator]) {
267
+ object = [...object][0];
268
+ }
269
+
270
+ predicateTokens = undefined;
271
+ break;
272
+ case "*":
273
+ predicateTokens.push(token);
274
+ break;
275
+
276
+ default:
277
+ if (object === undefined) {
278
+ break;
279
+ }
280
+
281
+ const g = getters[token];
282
+ if (g) {
283
+ object = g(object);
284
+ } else {
285
+ if (object[token] !== undefined) {
286
+ object = object[token];
287
+ } else {
288
+ return [undefined, op];
289
+ }
290
+ }
291
+ }
292
+ }
293
+
294
+ return [object, op];
295
+ }