svelte-common 4.20.0 → 4.21.1

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.
Files changed (3) hide show
  1. package/README.md +26 -0
  2. package/package.json +4 -4
  3. package/src/util.mjs +45 -5
package/README.md CHANGED
@@ -38,6 +38,10 @@ or the [live example](https://arlac77.github.io/components/svelte-common/example
38
38
  * [Parameters](#parameters-4)
39
39
  * [keyPrefixStore](#keyprefixstore)
40
40
  * [Parameters](#parameters-5)
41
+ * [tokens](#tokens)
42
+ * [Parameters](#parameters-6)
43
+ * [getAttribute](#getattribute)
44
+ * [Parameters](#parameters-7)
41
45
 
42
46
  ## filter
43
47
 
@@ -108,6 +112,28 @@ Create a derived store where all the object keys are prefixed.
108
112
 
109
113
  Returns **WriteableStore** 
110
114
 
115
+ ## tokens
116
+
117
+ Split property path into tokens
118
+
119
+ ### Parameters
120
+
121
+ * `string` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
122
+
123
+ Returns **Iterator<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**&#x20;
124
+
125
+ ## getAttribute
126
+
127
+ Deliver attribute value.
128
+ The name may be a property path like 'a.b.c'.
129
+
130
+ ### Parameters
131
+
132
+ * `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**&#x20;
133
+ * `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**&#x20;
134
+
135
+ Returns **any** value associated with the given property name
136
+
111
137
  # install
112
138
 
113
139
  With [npm](http://npmjs.org) do:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-common",
3
- "version": "4.20.0",
3
+ "version": "4.21.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -43,13 +43,13 @@
43
43
  "ava": "^5.1.0",
44
44
  "documentation": "^14.0.0",
45
45
  "mf-styling": "^1.7.27",
46
- "npm-pkgbuild": "^10.15.17",
46
+ "npm-pkgbuild": "^10.15.18",
47
47
  "semantic-release": "^19.0.5",
48
48
  "stylelint": "^14.16.0",
49
49
  "stylelint-config-standard": "^29.0.0",
50
- "svelte": "^3.53.1",
50
+ "svelte": "^3.54.0",
51
51
  "testcafe": "^2.1.0",
52
- "vite": "^3.2.4"
52
+ "vite": "^3.2.5"
53
53
  },
54
54
  "optionalDependencies": {
55
55
  "mf-hosting": "^1.7.9"
package/src/util.mjs CHANGED
@@ -165,6 +165,37 @@ export function keyPrefixStore(store, prefix) {
165
165
  };
166
166
  }
167
167
 
168
+ /**
169
+ * Split property path into tokens
170
+ * @param {string} string
171
+ * @return {Iterator<string>}
172
+ */
173
+ function* tokens(string) {
174
+ let identifier = "";
175
+
176
+ for (const c of string) {
177
+ switch (c) {
178
+ case "\t":
179
+ case " ": break;
180
+ case ".":
181
+ case "[":
182
+ case "]":
183
+ if (identifier.length) {
184
+ yield identifier;
185
+ identifier = "";
186
+ }
187
+ yield c;
188
+ break;
189
+ default:
190
+ identifier += c;
191
+ }
192
+ }
193
+
194
+ if (identifier.length) {
195
+ yield identifier;
196
+ }
197
+ }
198
+
168
199
  /**
169
200
  * Deliver attribute value.
170
201
  * The name may be a property path like 'a.b.c'.
@@ -172,16 +203,25 @@ export function keyPrefixStore(store, prefix) {
172
203
  * @param {string} name
173
204
  * @returns {any} value associated with the given property name
174
205
  */
175
- export function getAttribute(object, name) {
206
+ export function getAttribute(object, name) {
176
207
  if (object && object[name] !== undefined) {
177
208
  return object[name];
178
209
  }
179
210
 
180
- for (const p of name.split(/\./)) {
181
- if (object === undefined) {
182
- break;
211
+ for (const token of tokens(name)) {
212
+ switch (token) {
213
+ case ".":
214
+ case "[":
215
+ case "]":
216
+ break;
217
+
218
+ default:
219
+ if (object === undefined) {
220
+ break;
221
+ }
222
+
223
+ object = object[token];
183
224
  }
184
- object = object[p];
185
225
  }
186
226
 
187
227
  return object;