repository-provider 32.6.4 → 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 +1 -1
- package/src/attribute-extras.mjs +208 -0
- package/src/attribute.mjs +0 -206
- package/src/base-object.mjs +1 -1
- package/src/index.mjs +1 -0
- package/src/named-object.mjs +1 -1
- package/src/pull-request.mjs +1 -1
package/package.json
CHANGED
|
@@ -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,139 +1,3 @@
|
|
|
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
|
|
@@ -143,7 +7,6 @@ export function defaultValues(attributes, object) {
|
|
|
143
7
|
function* tokens(string) {
|
|
144
8
|
let identifier = "";
|
|
145
9
|
|
|
146
|
-
|
|
147
10
|
for (const c of string) {
|
|
148
11
|
switch (c) {
|
|
149
12
|
case "\t":
|
|
@@ -239,72 +102,3 @@ export function getAttribute(object, name) {
|
|
|
239
102
|
|
|
240
103
|
return object;
|
|
241
104
|
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Create json based on present options.
|
|
245
|
-
* In other words only produce key value pairs if value is defined.
|
|
246
|
-
* @param {Object} object
|
|
247
|
-
* @param {Object} initial
|
|
248
|
-
* @param {Object} attributes to operator on
|
|
249
|
-
* @return {Object} initial + defined values
|
|
250
|
-
*/
|
|
251
|
-
export function optionJSON(
|
|
252
|
-
object,
|
|
253
|
-
initial = {},
|
|
254
|
-
attributes = object.constructor.attributes
|
|
255
|
-
) {
|
|
256
|
-
return attributes
|
|
257
|
-
? Object.keys(attributes).reduce((a, c) => {
|
|
258
|
-
const value = object[c];
|
|
259
|
-
if (value !== undefined && !(value instanceof Function)) {
|
|
260
|
-
if (value instanceof Set) {
|
|
261
|
-
a[c] = [...value];
|
|
262
|
-
} else {
|
|
263
|
-
a[c] = value;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return a;
|
|
267
|
-
}, initial)
|
|
268
|
-
: initial;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Rename attributes.
|
|
273
|
-
* Filters out null, undefined and empty strings.
|
|
274
|
-
* ```js
|
|
275
|
-
* mapAttributes({a:1},{a:"a'"}) // {"a'": 1}
|
|
276
|
-
* ```
|
|
277
|
-
* @param {Object} object
|
|
278
|
-
* @param {Object} mapping
|
|
279
|
-
* @return {Object} keys renamed after mapping
|
|
280
|
-
*/
|
|
281
|
-
export function mapAttributes(object, mapping) {
|
|
282
|
-
if (object !== undefined) {
|
|
283
|
-
const o = {};
|
|
284
|
-
|
|
285
|
-
for (const k of Object.keys(object)) {
|
|
286
|
-
const v = getAttribute(object, k);
|
|
287
|
-
if (v !== undefined && v !== null && v !== "") {
|
|
288
|
-
setAttribute(o, mapping[k] || k, v);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
return o;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Same as mapAttributes but with the inverse mapping.
|
|
298
|
-
* Filters out null, undefined and empty strings
|
|
299
|
-
* @param {Object} object
|
|
300
|
-
* @param {Object} mapping
|
|
301
|
-
* @return {Object} keys renamed after mapping
|
|
302
|
-
*/
|
|
303
|
-
export function mapAttributesInverse(object, mapping) {
|
|
304
|
-
return mapping === undefined
|
|
305
|
-
? object
|
|
306
|
-
: mapAttributes(
|
|
307
|
-
object,
|
|
308
|
-
Object.fromEntries(Object.entries(mapping).map(([k, v]) => [v, k]))
|
|
309
|
-
);
|
|
310
|
-
}
|
package/src/base-object.mjs
CHANGED
package/src/index.mjs
CHANGED
package/src/named-object.mjs
CHANGED
package/src/pull-request.mjs
CHANGED