svelte-common 4.20.0 → 4.21.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.
Files changed (3) hide show
  1. package/README.md +14 -0
  2. package/package.json +4 -4
  3. package/src/util.mjs +43 -5
package/README.md CHANGED
@@ -38,6 +38,8 @@ 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
+ * [getAttribute](#getattribute)
42
+ * [Parameters](#parameters-6)
41
43
 
42
44
  ## filter
43
45
 
@@ -108,6 +110,18 @@ Create a derived store where all the object keys are prefixed.
108
110
 
109
111
  Returns **WriteableStore** 
110
112
 
113
+ ## getAttribute
114
+
115
+ Deliver attribute value.
116
+ The name may be a property path like 'a.b.c'.
117
+
118
+ ### Parameters
119
+
120
+ * `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
121
+ * `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
122
+
123
+ Returns **any** value associated with the given property name
124
+
111
125
  # install
112
126
 
113
127
  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.0",
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,35 @@ 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 ".":
179
+ case "[":
180
+ case "]":
181
+ if (identifier.length) {
182
+ yield identifier;
183
+ identifier = "";
184
+ }
185
+ yield c;
186
+ break;
187
+ default:
188
+ identifier += c;
189
+ }
190
+ }
191
+
192
+ if (identifier.length) {
193
+ yield identifier;
194
+ }
195
+ }
196
+
168
197
  /**
169
198
  * Deliver attribute value.
170
199
  * The name may be a property path like 'a.b.c'.
@@ -172,16 +201,25 @@ export function keyPrefixStore(store, prefix) {
172
201
  * @param {string} name
173
202
  * @returns {any} value associated with the given property name
174
203
  */
175
- export function getAttribute(object, name) {
204
+ export function getAttribute(object, name) {
176
205
  if (object && object[name] !== undefined) {
177
206
  return object[name];
178
207
  }
179
208
 
180
- for (const p of name.split(/\./)) {
181
- if (object === undefined) {
182
- break;
209
+ for (const token of tokens(name)) {
210
+ switch (token) {
211
+ case ".":
212
+ case "[":
213
+ case "]":
214
+ break;
215
+
216
+ default:
217
+ if (object === undefined) {
218
+ break;
219
+ }
220
+
221
+ object = object[token];
183
222
  }
184
- object = object[p];
185
223
  }
186
224
 
187
225
  return object;