repository-provider 32.6.3 → 32.6.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "repository-provider",
3
- "version": "32.6.3",
3
+ "version": "32.6.5",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -34,12 +34,12 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "ava": "^5.1.1",
37
- "browser-ava": "^1.3.19",
37
+ "browser-ava": "^1.3.20",
38
38
  "c8": "^7.12.0",
39
39
  "documentation": "^14.0.1",
40
40
  "repository-provider-test-support": "^2.2.36",
41
- "semantic-release": "^20.0.3",
42
- "typescript": "^4.9.4"
41
+ "semantic-release": "^20.1.0",
42
+ "typescript": "^4.9.5"
43
43
  },
44
44
  "engines": {
45
45
  "node": ">=16.19.0"
@@ -0,0 +1,208 @@
1
+
2
+ import { setAttribute, getAttribute } from "./attribute.mjs";
3
+
4
+ /**
5
+ * @typedef {Object} Attribute
6
+ *
7
+ * @property {string} type
8
+ * @property {boolean} writable
9
+ * @property {boolean} [private] should the value be shown
10
+ * @property {string} [depends] name of an attribute we depend on
11
+ * @property {string} description
12
+ * @property {any} [default] the default value
13
+ * @property {Function} [set] set the value
14
+ * @property {Function} [get] get the value can be used to calculate default values
15
+ * @property {string|string[]} [env] environment variable use to provide the value
16
+ */
17
+
18
+ /**
19
+ * Create properties from options and default options.
20
+ * Already present properties (direct) are skipped.
21
+ * The attribute list from the class will be applied to the
22
+ * options and merged with the given set of properties.
23
+ * ```js
24
+ * class aClass {
25
+ * static get attributes() {
26
+ * return { with_default: { default: 77 }};
27
+ * }
28
+ * }
29
+ *
30
+ * definePropertiesFromOptions(new aClass());
31
+ * // equivalent to
32
+ * Object.definedProperties(new aClass(),{ with_default: { value: 77 }})
33
+ * ```
34
+ * @see Object.definedProperties()
35
+ * @see Object.getOwnPropertyDescriptor()
36
+ * @param {Object} object target object
37
+ * @param {Object} options as passed to object constructor
38
+ * @param {Object} properties object properties
39
+ * @param {Object} attributes
40
+ */
41
+ export function definePropertiesFromOptions(
42
+ object,
43
+ options = {},
44
+ properties = {},
45
+ attributes = object.constructor.attributes || []
46
+ ) {
47
+ const applyLater = {};
48
+
49
+ Object.entries(attributes).forEach(([name, attribute]) => {
50
+ const path = name.split(/\./);
51
+ const first = path.shift();
52
+ const property = properties[first];
53
+
54
+ let value = getAttribute(options, name);
55
+
56
+ if (value === undefined) {
57
+ if (attribute.get) {
58
+ value = attribute.get(attribute, object, properties);
59
+ } else if (
60
+ attribute.default !== undefined &&
61
+ attribute.default !== getAttribute(object, name)
62
+ ) {
63
+ value = attribute.default;
64
+ }
65
+ }
66
+
67
+ if (attribute.set) {
68
+ value = attribute.set(value);
69
+ } else {
70
+ switch (attribute.type) {
71
+ case "set":
72
+ if (Array.isArray(value)) {
73
+ value = new Set(value);
74
+ }
75
+ break;
76
+ case "boolean":
77
+ if (value !== undefined) {
78
+ value =
79
+ value === 0 || value === "0" || value === false ? false : true;
80
+ }
81
+ break;
82
+ }
83
+ }
84
+
85
+ if (path.length) {
86
+ const remaining = path.join(".");
87
+ if (property) {
88
+ setAttribute(property.value, remaining, value);
89
+ } else {
90
+ const slice = {};
91
+ setAttribute(slice, remaining, value);
92
+ properties[first] = { configurable: true, value: slice };
93
+ }
94
+ } else {
95
+ if (value !== undefined) {
96
+ const op = Object.getOwnPropertyDescriptor(
97
+ object.constructor.prototype,
98
+ first
99
+ );
100
+
101
+ if (op?.set || property?.set) {
102
+ applyLater[first] = value;
103
+ } else {
104
+ properties[first] = Object.assign(
105
+ { value, writable: attribute.writable },
106
+ property
107
+ );
108
+ }
109
+ }
110
+ }
111
+ });
112
+
113
+ Object.defineProperties(object, properties);
114
+ Object.assign(object, applyLater);
115
+ }
116
+
117
+ /**
118
+ * Get default values.
119
+ * @param {Object} attributes
120
+ * @return {Object} filled with default values
121
+ */
122
+ export function defaultValues(attributes, object) {
123
+ return Object.fromEntries(
124
+ Object.entries(attributes).reduce((a, c) => {
125
+ const [name, attribute] = c;
126
+
127
+ if (attribute.default !== undefined) {
128
+ a.push([name, attribute.default]);
129
+ } else if (attribute.get !== undefined) {
130
+ const value = attribute.get(attribute, object);
131
+ if (value !== undefined) {
132
+ a.push([name, value]);
133
+ }
134
+ }
135
+
136
+ return a;
137
+ }, [])
138
+ );
139
+ }
140
+
141
+ /**
142
+ * Create json based on present options.
143
+ * In other words only produce key value pairs if value is defined.
144
+ * @param {Object} object
145
+ * @param {Object} initial
146
+ * @param {Object} attributes to operator on
147
+ * @return {Object} initial + defined values
148
+ */
149
+ export function optionJSON(
150
+ object,
151
+ initial = {},
152
+ attributes = object.constructor.attributes
153
+ ) {
154
+ return attributes
155
+ ? Object.keys(attributes).reduce((a, c) => {
156
+ const value = object[c];
157
+ if (value !== undefined && !(value instanceof Function)) {
158
+ if (value instanceof Set) {
159
+ a[c] = [...value];
160
+ } else {
161
+ a[c] = value;
162
+ }
163
+ }
164
+ return a;
165
+ }, initial)
166
+ : initial;
167
+ }
168
+
169
+ /**
170
+ * Rename attributes.
171
+ * Filters out null, undefined and empty strings.
172
+ * ```js
173
+ * mapAttributes({a:1},{a:"a'"}) // {"a'": 1}
174
+ * ```
175
+ * @param {Object} object
176
+ * @param {Object} mapping
177
+ * @return {Object} keys renamed after mapping
178
+ */
179
+ export function mapAttributes(object, mapping) {
180
+ if (object !== undefined) {
181
+ const o = {};
182
+
183
+ for (const k of Object.keys(object)) {
184
+ const v = getAttribute(object, k);
185
+ if (v !== undefined && v !== null && v !== "") {
186
+ setAttribute(o, mapping[k] || k, v);
187
+ }
188
+ }
189
+
190
+ return o;
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Same as mapAttributes but with the inverse mapping.
196
+ * Filters out null, undefined and empty strings
197
+ * @param {Object} object
198
+ * @param {Object} mapping
199
+ * @return {Object} keys renamed after mapping
200
+ */
201
+ export function mapAttributesInverse(object, mapping) {
202
+ return mapping === undefined
203
+ ? object
204
+ : mapAttributes(
205
+ object,
206
+ Object.fromEntries(Object.entries(mapping).map(([k, v]) => [v, k]))
207
+ );
208
+ }
package/src/attribute.mjs CHANGED
@@ -1,143 +1,7 @@
1
- /**
2
- * @typedef {Object} Attribute
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} description
9
- * @property {any} [default] the default value
10
- * @property {Function} [set] set the value
11
- * @property {Function} [get] get the value can be used to calculate default values
12
- * @property {string|string[]} [env] environment variable use to provide the value
13
- */
14
-
15
- /**
16
- * Create properties from options and default options.
17
- * Already present properties (direct) are skipped.
18
- * The attribute list from the class will be applied to the
19
- * options and merged with the given set of properties.
20
- * ```js
21
- * class aClass {
22
- * static get attributes() {
23
- * return { with_default: { default: 77 }};
24
- * }
25
- * }
26
- *
27
- * definePropertiesFromOptions(new aClass());
28
- * // equivalent to
29
- * Object.definedProperties(new aClass(),{ with_default: { value: 77 }})
30
- * ```
31
- * @see Object.definedProperties()
32
- * @see Object.getOwnPropertyDescriptor()
33
- * @param {Object} object target object
34
- * @param {Object} options as passed to object constructor
35
- * @param {Object} properties object properties
36
- * @param {Object} attributes
37
- */
38
- export function definePropertiesFromOptions(
39
- object,
40
- options = {},
41
- properties = {},
42
- attributes = object.constructor.attributes || []
43
- ) {
44
- const applyLater = {};
45
-
46
- Object.entries(attributes).forEach(([name, attribute]) => {
47
- const path = name.split(/\./);
48
- const first = path.shift();
49
- const property = properties[first];
50
-
51
- let value = getAttribute(options, name);
52
-
53
- if (value === undefined) {
54
- if (attribute.get) {
55
- value = attribute.get(attribute, object, properties);
56
- } else if (
57
- attribute.default !== undefined &&
58
- attribute.default !== getAttribute(object, name)
59
- ) {
60
- value = attribute.default;
61
- }
62
- }
63
-
64
- if (attribute.set) {
65
- value = attribute.set(value);
66
- } else {
67
- switch (attribute.type) {
68
- case "set":
69
- if (Array.isArray(value)) {
70
- value = new Set(value);
71
- }
72
- break;
73
- case "boolean":
74
- if (value !== undefined) {
75
- value =
76
- value === 0 || value === "0" || value === false ? false : true;
77
- }
78
- break;
79
- }
80
- }
81
-
82
- if (path.length) {
83
- const remaining = path.join(".");
84
- if (property) {
85
- setAttribute(property.value, remaining, value);
86
- } else {
87
- const slice = {};
88
- setAttribute(slice, remaining, value);
89
- properties[first] = { configurable: true, value: slice };
90
- }
91
- } else {
92
- if (value !== undefined) {
93
- const op = Object.getOwnPropertyDescriptor(
94
- object.constructor.prototype,
95
- first
96
- );
97
-
98
- if (op?.set || property?.set) {
99
- applyLater[first] = value;
100
- } else {
101
- properties[first] = Object.assign(
102
- { value, writable: attribute.writable },
103
- property
104
- );
105
- }
106
- }
107
- }
108
- });
109
-
110
- Object.defineProperties(object, properties);
111
- Object.assign(object, applyLater);
112
- }
113
-
114
- /**
115
- * Get default values.
116
- * @param {Object} attributes
117
- * @return {Object} filled with default values
118
- */
119
- export function defaultValues(attributes, object) {
120
- return Object.fromEntries(
121
- Object.entries(attributes).reduce((a, c) => {
122
- const [name, attribute] = c;
123
-
124
- if (attribute.default !== undefined) {
125
- a.push([name, attribute.default]);
126
- } else if (attribute.get !== undefined) {
127
- const value = attribute.get(attribute, object);
128
- if (value !== undefined) {
129
- a.push([name, value]);
130
- }
131
- }
132
-
133
- return a;
134
- }, [])
135
- );
136
- }
137
1
 
138
2
  /**
139
3
  * Split property path into tokens
140
- * @param {string} string
4
+ * @param {string} string
141
5
  * @return {Iterator<string>}
142
6
  */
143
7
  function* tokens(string) {
@@ -146,7 +10,12 @@ function* tokens(string) {
146
10
  for (const c of string) {
147
11
  switch (c) {
148
12
  case "\t":
149
- case " ": break;
13
+ case " ":
14
+ break;
15
+
16
+ case ">":
17
+ case "<":
18
+
150
19
  case ".":
151
20
  case "[":
152
21
  case "]":
@@ -179,20 +48,22 @@ export function setAttribute(object, name, value) {
179
48
 
180
49
  for (const token of tokens(name)) {
181
50
  switch (token) {
51
+ case ">":
52
+ case "<":
182
53
  case ".":
183
- case "[":
184
- case "]":
185
- break;
186
-
187
- default:
188
- if (object[token] === undefined || typeof object[token] !== "object") {
189
- object[token] = {};
190
- }
54
+ case "[":
55
+ case "]":
56
+ break;
57
+
58
+ default:
59
+ if (object[token] === undefined || typeof object[token] !== "object") {
60
+ object[token] = {};
61
+ }
191
62
 
192
- lastObject = object;
193
- lastKey = token;
63
+ lastObject = object;
64
+ lastKey = token;
194
65
 
195
- object = object[token];
66
+ object = object[token];
196
67
  }
197
68
  }
198
69
 
@@ -213,6 +84,8 @@ export function getAttribute(object, name) {
213
84
 
214
85
  for (const token of tokens(name)) {
215
86
  switch (token) {
87
+ case ">":
88
+ case "<":
216
89
  case ".":
217
90
  case "[":
218
91
  case "]":
@@ -229,72 +102,3 @@ export function getAttribute(object, name) {
229
102
 
230
103
  return object;
231
104
  }
232
-
233
- /**
234
- * Create json based on present options.
235
- * In other words only produce key value pairs if value is defined.
236
- * @param {Object} object
237
- * @param {Object} initial
238
- * @param {Object} attributes to operator on
239
- * @return {Object} initial + defined values
240
- */
241
- export function optionJSON(
242
- object,
243
- initial = {},
244
- attributes = object.constructor.attributes
245
- ) {
246
- return attributes
247
- ? Object.keys(attributes).reduce((a, c) => {
248
- const value = object[c];
249
- if (value !== undefined && !(value instanceof Function)) {
250
- if (value instanceof Set) {
251
- a[c] = [...value];
252
- } else {
253
- a[c] = value;
254
- }
255
- }
256
- return a;
257
- }, initial)
258
- : initial;
259
- }
260
-
261
- /**
262
- * Rename attributes.
263
- * Filters out null, undefined and empty strings.
264
- * ```js
265
- * mapAttributes({a:1},{a:"a'"}) // {"a'": 1}
266
- * ```
267
- * @param {Object} object
268
- * @param {Object} mapping
269
- * @return {Object} keys renamed after mapping
270
- */
271
- export function mapAttributes(object, mapping) {
272
- if (object !== undefined) {
273
- const o = {};
274
-
275
- for (const k of Object.keys(object)) {
276
- const v = getAttribute(object, k);
277
- if (v !== undefined && v !== null && v !== "") {
278
- setAttribute(o, mapping[k] || k, v);
279
- }
280
- }
281
-
282
- return o;
283
- }
284
- }
285
-
286
- /**
287
- * Same as mapAttributes but with the inverse mapping.
288
- * Filters out null, undefined and empty strings
289
- * @param {Object} object
290
- * @param {Object} mapping
291
- * @return {Object} keys renamed after mapping
292
- */
293
- export function mapAttributesInverse(object, mapping) {
294
- return mapping === undefined
295
- ? object
296
- : mapAttributes(
297
- object,
298
- Object.fromEntries(Object.entries(mapping).map(([k, v]) => [v, k]))
299
- );
300
- }
@@ -1,4 +1,4 @@
1
- import { definePropertiesFromOptions, mapAttributes } from "./attribute.mjs";
1
+ import { definePropertiesFromOptions, mapAttributes } from "./attribute-extras.mjs";
2
2
  import { description_attribute, id_attribute } from "./attributes.mjs";
3
3
 
4
4
  /**
package/src/index.mjs CHANGED
@@ -19,5 +19,6 @@ export * from "./milestone.mjs";
19
19
  export * from "./review.mjs";
20
20
  export * from "./application.mjs";
21
21
  export * from "./attribute.mjs";
22
+ export * from "./attribute-extras.mjs";
22
23
  export * from "./util.mjs";
23
24
  export * from "./attributes.mjs";
@@ -1,4 +1,4 @@
1
- import { optionJSON } from "./attribute.mjs";
1
+ import { optionJSON } from "./attribute-extras.mjs";
2
2
  import { BaseObject } from "./base-object.mjs";
3
3
  import { name_attribute, url_attribute, description_attribute, id_attribute } from "./attributes.mjs";
4
4
 
@@ -1,4 +1,4 @@
1
- import { optionJSON } from "./attribute.mjs";
1
+ import { optionJSON } from "./attribute-extras.mjs";
2
2
  import { OwnedObject } from "./owned-object.mjs";
3
3
  import { Branch } from "./branch.mjs";
4
4
  import { Repository } from "./repository.mjs";