@qubit-ltd/common-decorator 3.8.9

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 (55) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +1156 -0
  3. package/README.zh_CN.md +929 -0
  4. package/dist/common-decorator.cjs +6818 -0
  5. package/dist/common-decorator.cjs.map +1 -0
  6. package/dist/common-decorator.min.cjs +1 -0
  7. package/dist/common-decorator.min.cjs.map +1 -0
  8. package/dist/common-decorator.min.mjs +1 -0
  9. package/dist/common-decorator.min.mjs.map +1 -0
  10. package/dist/common-decorator.mjs +6792 -0
  11. package/dist/common-decorator.mjs.map +1 -0
  12. package/doc/api/DefaultAssignmentOptions.html +678 -0
  13. package/doc/api/DefaultOptions.html +954 -0
  14. package/doc/api/DefaultToJsonOptions.html +633 -0
  15. package/doc/api/Enum.html +1848 -0
  16. package/doc/api/Model.html +3607 -0
  17. package/doc/api/Page.html +1105 -0
  18. package/doc/api/fonts/OpenSans-Bold-webfont.eot +0 -0
  19. package/doc/api/fonts/OpenSans-Bold-webfont.svg +1838 -0
  20. package/doc/api/fonts/OpenSans-Bold-webfont.woff +0 -0
  21. package/doc/api/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  22. package/doc/api/fonts/OpenSans-BoldItalic-webfont.svg +1838 -0
  23. package/doc/api/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  24. package/doc/api/fonts/OpenSans-Italic-webfont.eot +0 -0
  25. package/doc/api/fonts/OpenSans-Italic-webfont.svg +1838 -0
  26. package/doc/api/fonts/OpenSans-Italic-webfont.woff +0 -0
  27. package/doc/api/fonts/OpenSans-Light-webfont.eot +0 -0
  28. package/doc/api/fonts/OpenSans-Light-webfont.svg +1839 -0
  29. package/doc/api/fonts/OpenSans-Light-webfont.woff +0 -0
  30. package/doc/api/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  31. package/doc/api/fonts/OpenSans-LightItalic-webfont.svg +1843 -0
  32. package/doc/api/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  33. package/doc/api/fonts/OpenSans-Regular-webfont.eot +0 -0
  34. package/doc/api/fonts/OpenSans-Regular-webfont.svg +1839 -0
  35. package/doc/api/fonts/OpenSans-Regular-webfont.woff +0 -0
  36. package/doc/api/fonts/OpenSans-Semibold-webfont.eot +0 -0
  37. package/doc/api/fonts/OpenSans-Semibold-webfont.svg +1838 -0
  38. package/doc/api/fonts/OpenSans-Semibold-webfont.ttf +0 -0
  39. package/doc/api/fonts/OpenSans-Semibold-webfont.woff +0 -0
  40. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.eot +0 -0
  41. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.svg +1838 -0
  42. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.ttf +0 -0
  43. package/doc/api/fonts/OpenSans-SemiboldItalic-webfont.woff +0 -0
  44. package/doc/api/global.html +6049 -0
  45. package/doc/api/index.html +1325 -0
  46. package/doc/api/scripts/linenumber.js +34 -0
  47. package/doc/api/scripts/prettify/Apache-License-2.0.txt +202 -0
  48. package/doc/api/scripts/prettify/lang-css.js +2 -0
  49. package/doc/api/scripts/prettify/prettify.js +28 -0
  50. package/doc/api/styles/jsdoc-default.css +699 -0
  51. package/doc/api/styles/prettify-jsdoc.css +120 -0
  52. package/doc/api/styles/prettify-tomorrow.css +141 -0
  53. package/doc/common-decorator.min.visualization.html +4949 -0
  54. package/doc/common-decorator.visualization.html +4949 -0
  55. package/package.json +107 -0
package/README.md ADDED
@@ -0,0 +1,1156 @@
1
+ # js-common-decorator
2
+
3
+ [![npm package](https://img.shields.io/npm/v/@qubit-ltd/common-decorator.svg)](https://npmjs.com/package/@qubit-ltd/common-decorator)
4
+ [![License](https://img.shields.io/badge/License-Apache-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
5
+ [![中文文档](https://img.shields.io/badge/文档-中文版-blue.svg)](README.zh_CN.md)
6
+ [![CircleCI](https://dl.circleci.com/status-badge/img/gh/Haixing-Hu/js-common-decorator/tree/master.svg?style=shield)](https://dl.circleci.com/status-badge/redirect/gh/Haixing-Hu/js-common-decorator/tree/master)
7
+ [![Coverage Status](https://coveralls.io/repos/github/Haixing-Hu/js-common-decorator/badge.svg?branch=master)](https://coveralls.io/github/Haixing-Hu/js-common-decorator?branch=master)
8
+
9
+ [@qubit-ltd/common-decorator] is a JavaScript library of common decorators,
10
+ provides decorators to add common methods to domain classes. The library
11
+ supports the most recent (currently May 2023)
12
+ [stage 3 proposal of JavaScript decorators].
13
+
14
+ ## <span id="content">Table of Contents</span>
15
+
16
+ - [Usage](#usage)
17
+ - [@Model Decorator](#model)
18
+ - [Instance method: Class.prototype.assign(obj, options = undefined)](#model-assign)
19
+ - [Instance method: Class.prototype.clone()](#model-clone)
20
+ - [Instance method: Class.prototype.isEmpty()](#model-isEmpty)
21
+ - [Instance method: Class.prototype.clear()](#model-clear)
22
+ - [Instance method: Class.prototype.equals(other)](#model-equals)
23
+ - [Instance method: Class.prototype.generateId()](#model-generateId)
24
+ - [Instance method: Class.prototype.normalizeField(field)](#model-normalizeField)
25
+ - [Instance method: Class.prototype.normalize(fields)](#model-normalize)
26
+ - [Instance method: Class.prototype.validateField(field)](#model-validateField)
27
+ - [Instance method: Class.prototype.validate(fields)](#model-validate)
28
+ - [Instance method: Class.prototype.toJSON(key, options = undefined)](#model-toJSON)
29
+ - [Instance method: Class.prototype.toJsonString(options = undeinfed)](#model-toJsonString)
30
+ - [Class method: Class.create(obj, options = undefined)](#model-create)
31
+ - [Class method: Class.createArray(array, options = undefined)](#model-createArray)
32
+ - [Class method: Class.createPage(page, options = undefined)](#model-createPage)
33
+ - [Class method: Class.isNullishOrEmpty()](#model-isNullishOrEmpty)
34
+ - [Class method: Class.parseJsonString(json, options=undefined)](#model-parseJsonString)
35
+ - [Usage Examples](#model-usage-examples)
36
+ - [@Enum Decorator](#enum)
37
+ - [Enumerator Fields](#enum-fields)
38
+ - [Instance method: Class.prototype.toString()](#enum-toString)
39
+ - [Instance method: Class.prototype.toJSON()](#enum-toJSON)
40
+ - [Class method: Class.values()](#enum-values)
41
+ - [Class method: Class.ofValue(value)](#enum-ofValue)
42
+ - [Class method: Class.hasValue(value)](#enum-hasValue)
43
+ - [Class method: Class.ofName(name)](#enum-ofName)
44
+ - [Class method: Class.hasName(name)](#enum-hasName)
45
+ - [Class method: Class.ofCode(code)](#enum-ofCode)
46
+ - [Class method: Class.hasCode(code)](#enum-hasCode)
47
+ - [Class method: Class.of(expr)](#enum-of)
48
+ - [Class method: Class.has(expr)](#enum-has)
49
+ - [Usage Example](#enum-usage-example)
50
+ - [DefaultOptions Class](#default-options)
51
+ - [Class method: DefaultOptions.get(name)](#default-options-get)
52
+ - [Class method: DefaultOptions.set(name, options)](#default-options-set)
53
+ - [Class method: DefaultOptions.merge(name, options)](#default-options-merge)
54
+ - [Configuration](#configuration)
55
+ - [Bundling with webpack](#webpack)
56
+ - [Bundling with vite](#vite)
57
+ - [Contributing](#contributing)
58
+ - [License](#license)
59
+
60
+ ## <span id="usage">Usage</span>
61
+
62
+ ### <span id="model">@Model Decorator</span>
63
+
64
+ This decorator is used to decorate a domain model class, which adds the
65
+ following instance and class methods to the decorated class.
66
+
67
+ **NOTE:** If the decorated class already implements any of the following methods,
68
+ this decorator will not override the methods already implemented by the decorated
69
+ class.
70
+
71
+ #### <span id="model-assign">Instance method: Class.prototype.assign(obj, options = undefined)</span>
72
+
73
+ - Parameters:
74
+ - `obj: object`: the object whose fields will be copied to this object,
75
+ which may have a different prototype to this object.
76
+ - `options: null|undefined|object`: the additional options for the assignment.
77
+ If this argument is `undefined` or `null`, the default options will be used.
78
+ The default options can be retrieved by calling `DefaultOptions.get('assign')`.
79
+ Available options are:
80
+ - `normalize: boolean`, indicates whether to normalize this object
81
+ after the assignment. The default value is `true`.
82
+ - `convertNaming: boolean`, indicates whether to convert the naming
83
+ style of the target object. The default value is `false`.
84
+ - `sourceNamingStyle: string`, the naming style of the source object,
85
+ i.e., the first argument of the `assign()` method. The default value
86
+ of this argument is `'LOWER_UNDERSCORE'`.
87
+ - `targetNamingStyle: string`, the naming style of the target object,
88
+ i.e., the object calling the `assign()` method. The default value
89
+ of this argument is `'LOWER_CAMEL'`.
90
+ - `types: object`, the additional information about types of fields of
91
+ classes. The keys of this object are the path of the fields or
92
+ sub-fields of the target object, the values are the type of the
93
+ fields, represented as the constructor function of the type.
94
+ The default value is `{}`.
95
+ - `elementTypes: object`, the additional information about types of
96
+ elements of fields of classes. The keys of this object are the path of
97
+ the fields or sub-fields of the target object, the values are the type
98
+ of the elements, represented as the constructor function of the type.
99
+ The default value is `{}`.
100
+ - Returns:
101
+ - `object`: the calling object itself.
102
+
103
+ This function copies the fields of the object `obj` to this object, only copying
104
+ fields defined in this object's class. If a field in the `obj` object is
105
+ `undefined` or `null`, it sets the field's value to the default value. Note that
106
+ `obj` can have a different prototype to this object.
107
+
108
+ #### <span id="model-clone">Instance method: Class.prototype.clone()</span>
109
+
110
+ - Parameters: none.
111
+ - Returns:
112
+ - `object`: a instance of the specified class deep cloned from the calling
113
+ object.
114
+
115
+ This function deep clones the calling object, returning a new instance of the
116
+ specified class with the same property values as the calling object. Note that
117
+ the returned object has the same prototype as the calling object.
118
+
119
+ #### <span id="model-clear">Instance method: Class.prototype.clear()</span>
120
+
121
+ - Parameters: none.
122
+ - Returns:
123
+ - `object`: the calling object itself.
124
+
125
+ This function sets all the properties of this object to their default values.
126
+ The default value of a field is the value of the field of the default
127
+ constructed instance of the class.
128
+
129
+ #### <span id="model-isEmpty">Instance method: Class.prototype.isEmpty()</span>
130
+
131
+ - Parameters: none.
132
+ - Returns:
133
+ - `boolean`: whether this object is empty.
134
+
135
+ This function checks if this object is empty, meaning that all of its fields
136
+ have default values. The default value of a field is the value of the field of
137
+ the default constructed instance of the class.
138
+
139
+ #### <span id="model-equals">Instance method: Class.prototype.equals(other)</span>
140
+
141
+ - Parameters:
142
+ - `other: object`: the object to be compared with this object.
143
+ - Returns:
144
+ - `boolean`: whether this object is deeply equal to `other`.
145
+
146
+ This function checks whether this object is deeply equal to `other`. Two objects
147
+ are deeply equal if and only if they have the same prototype, and all of their
148
+ fields are deeply equal. Two fields are deeply equal if and only if they have
149
+ the same value, or they are both `undefined` or `null`. If a field is an array,
150
+ it is deeply equal to another array if and only if they have the same length,
151
+ and all of their elements are deeply equal. If a field is an object, it is
152
+ deeply equal to another object if and only if they have the same prototype,
153
+ and all of their fields are deeply equal.
154
+
155
+ #### <span id="model-generateId">Instance method: Class.prototype.generateId()</span>
156
+
157
+ - Parameters: none.
158
+ - Returns:
159
+ - `string`: the string representation of the generated globally unique ID set
160
+ to the calling object.
161
+
162
+ If the decorated class defines a property named `id`, this instance method
163
+ `generateId()` is automatically added to the decorated class. Each call to this
164
+ method generates a globally unique ID for the current calling object
165
+ (represented as a string of an integer), sets the `id` field of the calling
166
+ object to the generated ID, and returns the generated ID. Note that if a parent
167
+ class `A` defines the `id` field, and a subclass `B` inherits the `id` field but
168
+ does not define its own `id` field, the `generateId()` method is added only to
169
+ class `A`, not to class `B`.
170
+
171
+ #### <span id="model-normalizeField">Instance method: Class.prototype.normalizeField(field)</span>
172
+
173
+ - Parameters:
174
+ - `field: string`: the name of the specified field to be normalized.
175
+ - Returns:
176
+ - `boolean`: whether the specified field was normalized.
177
+
178
+ This function normalizes the specified field of this object. If the object has
179
+ the specified field and the specified field is normalizable, the function
180
+ normalizes the specified field and returns `true`; otherwise, the function does
181
+ nothing and returns `false`. Note that a field is normalizable if and only if it
182
+ is decorated by the `@Normalizable` decorator.
183
+
184
+ #### <span id="model-normalize">Instance method: Class.prototype.normalize(fields)</span>
185
+
186
+ - Parameters:
187
+ - `fields: undefined | null | string | string[]`: the fields to be normalized.
188
+ It can be one of the following values:
189
+ - `undefined`: normalizes all the normalizable fields of this object.
190
+ - `null`: normalizes all the normalizable fields of this object.
191
+ - `"*"`: normalizes all the normalizable fields of this object.
192
+ - `string[]`: normalizes all the normalizable fields whose names are
193
+ specified in this array.
194
+ - Returns:
195
+ - `object`: the normalized calling object.
196
+
197
+ This function normalizes the specified fields of this object. The `fields`
198
+ parameter specifies the names of fields to be normalized. If `fields` is
199
+ `undefined`, `null`, or the string `"*"`, it normalizes all the normalizable
200
+ fields of this object. If `fields` is an array of strings, it normalizes all the
201
+ normalizable fields whose names are specified in the array. Note that a field is
202
+ normalizable if and only if it is decorated by the `@Normalizable` decorator.
203
+
204
+ #### <span id="model-validateField">Instance method: Class.prototype.validateField(field)</span>
205
+
206
+ - Parameters:
207
+ - `field: string`: the name of the specified field to be validated.
208
+ - Returns:
209
+ - `ValidationResult | null`: the validation result.
210
+
211
+ This function validates the specified field of this object. If the object has
212
+ the specified field and the specified field is validatable, the function
213
+ validates the specified field and returns the validation result; otherwise, the
214
+ function does nothing and returns `null`. Note that a field is validatable if
215
+ and only if it is decorated by the `@Validatable` decorator.
216
+
217
+ #### <span id="model-validate">Instance method: Class.prototype.validate(fields)</span>
218
+
219
+ - Parameters:
220
+ - `fields: undefined | null | string | string[]`: the fields to be validated.
221
+ It can be one of the following values:
222
+ - `undefined`: validates all the validatable fields of this object.
223
+ - `null`: validates all the validatable fields of this object.
224
+ - `"*"`: validates all the validatable fields of this object.
225
+ - `string[]`: validates all the validatable fields whose names are
226
+ specified in this array.
227
+ - Returns:
228
+ - `ValidationResult`: the validation result.
229
+
230
+ This function validates the specified fields of this object. The `fields`
231
+ parameter specifies the names of fields to be validated. If `fields` is
232
+ `undefined`, `null`, or the string `"*"`, it validates all the validatable
233
+ fields of this object. If `fields` is an array of strings, it validates all the
234
+ validatable fields whose names are specified in the array. Note that a field is
235
+ validatable if and only if it is decorated by the `@Validatable`
236
+ decorator.
237
+
238
+ #### <span id="model-toJSON">Instance method: Class.prototype.toJSON(key, options = undefined)</span>
239
+
240
+ - Parameters:
241
+ - `key: string`:`JSON.stringify()` calls `toJSON()` with one parameter,
242
+ the `key`,which takes the following values:
243
+ - if this object is a property value, this argument is the property
244
+ name;
245
+ - if this object is in an array, this argument is the index in the
246
+ array, as a string;
247
+ - if `JSON.stringify()` was directly called on this object, this
248
+ argument is an empty string.
249
+ - `options: null|undefined|object`: the additional options for the
250
+ serialization. If this argument is `undefined` or `null`, the default
251
+ options will be used. The default options can be retrieved by calling
252
+ `DefaultOptions.get('toJSON')`. Available options are:
253
+ - `normalize: boolean`, indicates whether to normalize this object
254
+ before serializing. The default value is `true`.
255
+ - `removeEmptyFields: boolean`, indicates whether to ignore the empty
256
+ fields of the object. If it is `true`, the empty fields of the object
257
+ will be removed before serialization. The default value is `false`.
258
+ - `convertNaming: boolean`, indicates whether to convert the naming
259
+ of properties of the object represented by the result JSON string.
260
+ The default value is `false`.
261
+ - `sourceNamingStyle: string`, the naming style of the source object,
262
+ i.e., the object calling the `toJSON()` method. The default value
263
+ of this argument is `'LOWER_CAMEL'`.
264
+ - `targetNamingStyle: string`, the naming style of the target object,
265
+ i.e., the object represented by the result JSON string of the
266
+ `toJSON()` method. The default value of this argument is
267
+ `'LOWER_UNDERSCORE'`.
268
+ - `space: string | number`, a string or number that's used to insert
269
+ white space (including indentation, line break characters, etc.) into
270
+ the output JSON string for readability purposes. If this is a number,
271
+ it indicates the number of space characters to be used as indentation,
272
+ clamped to 10 (that is, any number greater than 10 is treated as if
273
+ it were 10). Values less than 1 indicate that no space should be used.
274
+ If this is a string, the string (or the first 10 characters of the
275
+ string, if it's longer than that) is inserted before every nested
276
+ object or array. If this is anything other than a string or number
277
+ (can be either a primitive or a wrapper object) — for example, is
278
+ `null` or not provided — no white space is used. The default value
279
+ of this option is `null`.
280
+ - Returns:
281
+ - `object`: the object to be serialized by `JSON.stringify()`, which may be
282
+ a modify copy of this object.
283
+
284
+ This function gets the object to be serialized by `JSON.stringify()`.
285
+ If the value has a `toJSON()` method, it's responsible to define what
286
+ data will be serialized. Instead of the object being serialized, the value
287
+ returned by the `toJSON()` method when called will be serialized.
288
+
289
+ **NOTE:** this function returns an object to be serialized by
290
+ `JSON.stringify()`, instead of a JSON string. Use `JSON.stringify()`
291
+ or `this.toJsonString()` methods to serialize this object into a JSON
292
+ string.
293
+
294
+ #### <span id="model-toJsonString">Instance method: Class.prototype.toJsonString(options = undefined)</span>
295
+
296
+ - Parameters:
297
+ - `options: null|undefined|object`: the additional options for the
298
+ serialization. If this argument is `undefined` or `null`, the default
299
+ options will be used. The default options can be retrieved by calling
300
+ `DefaultOptions.get('toJSON')`. Available options are:
301
+ - `normalize: boolean`, indicates whether to normalize this object
302
+ before serializing. The default value is `true`.
303
+ - `removeEmptyFields: boolean`, indicates whether to ignore the empty
304
+ fields of the object. If it is `true`, the empty fields of the object
305
+ will be removed before serialization. The default value is `false`.
306
+ - `convertNaming: boolean`, indicates whether to convert the naming
307
+ of properties of the object represented by the result JSON string.
308
+ The default value is `false`.
309
+ - `sourceNamingStyle: string`, the naming style of the source object,
310
+ i.e., the object calling the `toJSON()` method. The default value
311
+ of this argument is `'LOWER_CAMEL'`.
312
+ - `targetNamingStyle: string`, the naming style of the target object,
313
+ i.e., the object represented by the result JSON string of the
314
+ `toJSON()` method. The default value of this argument is
315
+ `'LOWER_UNDERSCORE'`.
316
+ - `space: string | number`, a string or number that's used to insert
317
+ white space (including indentation, line break characters, etc.) into
318
+ the output JSON string for readability purposes. If this is a number,
319
+ it indicates the number of space characters to be used as indentation,
320
+ clamped to 10 (that is, any number greater than 10 is treated as if
321
+ it were 10). Values less than 1 indicate that no space should be used.
322
+ If this is a string, the string (or the first 10 characters of the
323
+ string, if it's longer than that) is inserted before every nested
324
+ object or array. If this is anything other than a string or number
325
+ (can be either a primitive or a wrapper object) — for example, is
326
+ `null` or not provided — no white space is used. The default value
327
+ of this option is `null`.
328
+ - Returns:
329
+ - `string`: the JSON string serialized from this object, as `JSON.stringify()`
330
+ does, except that this function provides additional stringification
331
+ options.
332
+
333
+ This function serializes this object into a JSON string.
334
+
335
+ **NOTE:** This method supports native `bigint` value. For example, the
336
+ `bigint` value `9223372036854775807n` will be stringify as
337
+ `9223372036854775807`.
338
+
339
+ #### <span id="model-create">Class method: Class.create(obj, options = undefined)</span>
340
+
341
+ - Parameters:
342
+ - `obj: object`: the data object used to create the new instance.
343
+ - `options: null|undefined|object`: the additional options for the creation.
344
+ If this argument is `undefined` or `null`, the default options will be used.
345
+ The default options can be retrieved by calling `DefaultOptions.get('assign')`.
346
+ Available options are:
347
+ - `normalize: boolean`, indicates whether to normalize this object
348
+ after the assignment. The default value is `true`.
349
+ - `convertNaming: boolean`, indicates whether to convert the naming
350
+ style of the target object. The default value is `false`.
351
+ - `sourceNamingStyle: string`, the naming style of the source object,
352
+ i.e., the first argument of the `create()` method. The default
353
+ value of this argument is `'LOWER_UNDERSCORE'`.
354
+ - `targetNamingStyle: string`, the naming style of the target object,
355
+ i.e., the object returned by the `create()` method. The default
356
+ value of this argument is `'LOWER_CAMEL'`.
357
+ - `types: object`, the additional information about types of
358
+ fields of classes. The keys of this object are the path of the fields
359
+ or sub-fields of the target object, the values are the type of the
360
+ fields, represented as the constructor function of the type.
361
+ The default value is `{}`.
362
+ - `elementTypes: object`, the additional information about types of
363
+ elements of fields of classes. The keys of this object are the path of
364
+ the fields or sub-fields of the target object, the values are the type
365
+ of the elements, represented as the constructor function of the type.
366
+ The default value is `{}`.
367
+ - Returns:
368
+ - `object | null`: if the `obj` is `undefined` or `null`, returns `null`;
369
+ otherwise, returns a new instance of the model class whose fields are
370
+ initialized with the data in the `obj`.
371
+
372
+ This function creates a instance of the specified class from a data object,
373
+ whose fields are recursively initialized with properties in the `obj`. Note that
374
+ `obj` can have a different prototype to the specified class.
375
+
376
+ #### <span id="model-createArray">Class method: Class.createArray(array, options = undefined)</span>
377
+
378
+ - Parameters:
379
+ - `array: object[]`: the data object array used to create the new array.
380
+ - `options: null|undefined|object`: the additional options for the creation.
381
+ If this argument is `undefined` or `null`, the default options will be used.
382
+ The default options can be retrieved by calling `DefaultOptions.get('assign')`.
383
+ Available options are:
384
+ - `normalize: boolean`, indicates whether to normalize this object
385
+ after the assignment. The default value is `true`.
386
+ - `convertNaming: boolean`, indicates whether to convert the naming
387
+ style of the target object. The default value is `false`.
388
+ - `sourceNamingStyle: string`, the naming style of the source object,
389
+ i.e., the elements in the first argument of the `createArray()`
390
+ method. The default value of this argument is `'LOWER_UNDERSCORE'`.
391
+ - `targetNamingStyle: string`, the naming style of the target object,
392
+ i.e., the elements in the array returned by the `createArray()`
393
+ method. The default value of this argument is `'LOWER_CAMEL'`.
394
+ - `types: object`, the additional information about types of
395
+ fields of classes. The keys of this object are the path of the fields
396
+ or sub-fields of the target object, the values are the type of the
397
+ fields, represented as the constructor function of the type.
398
+ The default value is `{}`.
399
+ - `elementTypes: object`, the additional information about types of
400
+ elements of fields of classes. The keys of this object are the path of
401
+ the fields or sub-fields of the target object, the values are the type
402
+ of the elements, represented as the constructor function of the type.
403
+ The default value is `{}`.
404
+ - Returns:
405
+ - `object[] | null`: if the `array` is `undefined` or `null`, returns `null`;
406
+ otherwise, returns a new array of instances of the model class whose
407
+ fields are initialized with corresponding data object in the `array`.
408
+
409
+ This function creates an array of instances of the specified class from a data
410
+ object array. The fields of instances in the returned array are recursively
411
+ initialized with corresponding properties of the corresponding data object in
412
+ the `array`. Note that data objects in `array` can have different prototypes to
413
+ the specified class.
414
+
415
+ #### <span id="model-createPage">Class method: Class.createPage(page, options = undefined)</span>
416
+
417
+ - Parameters:
418
+ - `page: object`: the pagination data object used to create the new `Page`
419
+ instance.
420
+ - `options: null|undefined|object`: the additional options for the creation.
421
+ If this argument is `undefined` or `null`, the default options will be used.
422
+ The default options can be retrieved by calling `DefaultOptions.get('assign')`.
423
+ Available options are:
424
+ - `normalize: boolean`, indicates whether to normalize this object
425
+ after the assignment. The default value is `true`.
426
+ - `convertNaming: boolean`, indicates whether to convert the naming
427
+ style of the target object. The default value is `false`.
428
+ - `sourceNamingStyle: string`, the naming style of the source object,
429
+ i.e., the elements in the `content` array of the first argument of
430
+ the `createPage()` method. The default value of this argument is
431
+ `'LOWER_UNDERSCORE'`.
432
+ - `targetNamingStyle: string`, the naming style of the target object,
433
+ i.e., the elements in the `content` array of the `Page` object
434
+ returned by the `createPage()` method. The default value of this
435
+ argument is `'LOWER_CAMEL'`.
436
+ - `types: object`, the additional information about types of
437
+ fields of classes. The keys of this object are the path of the fields
438
+ or sub-fields of the target object, the values are the type of the
439
+ fields, represented as the constructor function of the type.
440
+ The default value is `{}`.
441
+ - `elementTypes: object`, the additional information about types of
442
+ elements of fields of classes. The keys of this object are the path of
443
+ the fields or sub-fields of the target object, the values are the type
444
+ of the elements, represented as the constructor function of the type.
445
+ The default value is `{}`.
446
+ - Returns:
447
+ - `Page | null`: if the `page` is `undefined` or `null`, returns `null`;
448
+ otherwise, returns a new instance of the `Page` class whose content are
449
+ initialized with the content of the pagination data object `page`.
450
+
451
+ This function creates a `Page` object, whose content are initialized with the
452
+ content of the specified pagination data object. Typically, `page` is a list of
453
+ domain objects obtained from a server using the `GET` method, and the object
454
+ should conform to the `Page` class definition. This class method returns
455
+ a new `Page` object, with the `content` property being the result of
456
+ `createArray(page.content, options)`, and the other properties matching those of
457
+ the `page` object. If `page` is not a valid `Page` object, it returns `null`.
458
+
459
+ #### <span id="model-isNullishOrEmpty">Class method: Class.isNullishOrEmpty(obj)</span>
460
+
461
+ - Parameters:
462
+ - `obj: object`: the object to be checked.
463
+ - Returns:
464
+ - `boolean`: whether the specified object is `undefined`, `null`, or an
465
+ empty object constructed with default values.
466
+
467
+ This function checks whether the specified object is `undefined`, `null`, or an
468
+ empty object constructed with default values. An object is empty if and only if
469
+ all of its fields have default values. The default value of a field is the value
470
+ of the field of the default constructed instance of the class. This function is
471
+ a convenient method to call `Class.prototype.isEmpty()`, with the handling of
472
+ nullish values.
473
+
474
+ #### <span id="model-parseJsonString">Class method: Class.parseJsonString(json, options = undefined)</span>
475
+
476
+ - Parameters:
477
+ - `json: string`: the JSON string to be parsed.
478
+ - `options: null|undefined|object`: the additional options for the parsing.
479
+ If this argument is `undefined` or `null`, the default options will be used.
480
+ The default options can be retrieved by calling `DefaultOptions.get('assign')`.
481
+ Available options are:
482
+ - `normalize: boolean`, indicates whether to normalize this object
483
+ after the assignment. The default value is `true`.
484
+ - `convertNaming: boolean`, indicates whether to convert the naming
485
+ style of properties of the object represented by the JSON string.
486
+ The default value is `false`.
487
+ - `sourceNamingStyle: string`, the naming style of the source object,
488
+ i.e., the object represented by the JSON string. The default value
489
+ of this argument is `'LOWER_UNDERSCORE'`.
490
+ - `targetNamingStyle: string`, the naming style of the target object,
491
+ i.e., the object returned by the `parseJsonString()` method. The
492
+ default value of this argument is `'LOWER_CAMEL'`.
493
+ - Returns:
494
+ - `boolean`: whether the specified object is `undefined`, `null`, or an
495
+ empty object constructed with default values.
496
+
497
+ This function parses an object of this class from a JSON string.
498
+
499
+ **NOTE:** This method supports integer values fall out of IEEE 754 integer
500
+ precision. For example, the integer value `9223372036854775807` will be
501
+ parsed as the native `bigint` value `9223372036854775807n`.
502
+
503
+ #### <span id="model-usage-examples">Usage Examples</span>
504
+
505
+ The following is the usage example of the `@Model` decorator.
506
+
507
+ ```js
508
+ @Model
509
+ class Credential {
510
+
511
+ @Normalizable
512
+ @Validator(validateCredentialTypeField)
513
+ @Type(CredentialType)
514
+ @Label('证件类型')
515
+ type = 'IDENTITY_CARD';
516
+
517
+ @Normalizable(trimUppercaseString)
518
+ @Validator(validateCredentialNumberField)
519
+ @Label('证件号码')
520
+ number = '';
521
+
522
+ constructor(type = CredentialType.DEFAULT.value, number = '') {
523
+ this.type = type;
524
+ this.number = number;
525
+ }
526
+
527
+ isIdentityCard() {
528
+ return (this.type === 'IDENTITY_CARD');
529
+ }
530
+ }
531
+
532
+ @Model
533
+ class Person {
534
+
535
+ @Normalizable(trimString)
536
+ @Label('ID')
537
+ id = null;
538
+
539
+ @Normalizable(trimUppercaseString)
540
+ @Validator(validatePersonNameField)
541
+ @Label('姓名')
542
+ name = '';
543
+
544
+ @Normalizable
545
+ @DefaultValidator
546
+ @Type(Credential)
547
+ @Label('证件')
548
+ credential = null;
549
+
550
+ @Normalizable
551
+ @Validator(validatePersonGenderField)
552
+ @Type(Gender)
553
+ @Label('性别')
554
+ gender = '';
555
+
556
+ @Normalizable(trimString)
557
+ @Validator(validatePersonBirthdayField)
558
+ @Label('出生日期')
559
+ birthday = '';
560
+
561
+ @Normalizable(trimUppercaseString)
562
+ @Validator(validateMobileField)
563
+ @Label('手机号码')
564
+ mobile = '';
565
+
566
+ @Normalizable(trimString)
567
+ @Validator(validateEmailField)
568
+ @Label('电子邮件地址')
569
+ @Nullable
570
+ email = '';
571
+
572
+ equals(other) {
573
+ if (!(other instanceof PersonWithEquals)) {
574
+ return false;
575
+ }
576
+ if ((this.credential === null) || (other.credential === null)) {
577
+ // If one of the two people does not have ID inofmation, it is impossible
578
+ // to compare whether they are the same person thus they will be considered
579
+ // different.
580
+ return false;
581
+ }
582
+ // Two persons are logically equals if and only if they have the same
583
+ // credential.
584
+ return (this.credential.type === other.credential.type)
585
+ && (this.credential.number === other.credential.number);
586
+ }
587
+ }
588
+ ```
589
+
590
+ After applying the `@Model` decorator, the following methods will be automatically
591
+ added:
592
+
593
+ - `Credential.prototype.assign(obj, options = undefined)`
594
+ - `Credential.prototype.clear()`
595
+ - `Credential.prototype.clone()`
596
+ - `Credential.prototype.isEmpty()`
597
+ - `Credential.prototype.equals(obj)`
598
+ - `Credential.prototype.normalize(fields)`
599
+ - `Credential.prototype.validate(fields, options)`
600
+ - `Credential.prototype.toJSON(key, options = undefined)`
601
+ - `Credential.prototype.toJsonString(options = undefined)`
602
+ - `Credential.create(obj, options = undefined)`
603
+ - `Credential.createArray(array, options = undefined)`
604
+ - `Credential.createPage(page, options = undefined)`
605
+ - `Credential.isNullishOrEmpty(obj)`
606
+ - `Credential.parseJsonString(json, options = undefined)`
607
+ - `Person.prototype.assign(obj, normalized)`
608
+ - `Person.prototype.clear()`
609
+ - `Person.prototype.clone()`
610
+ - `Person.prototype.isEmpty()`
611
+ - `Person.prototype.normalize(fields)`
612
+ - `Person.prototype.validate(fields, options)`
613
+ - `Person.prototype.generateId()`
614
+ - `Person.prototype.toJSON(key, options = undefined)`
615
+ - `Person.prototype.toJsonString(options = undefined)`
616
+ - `Person.create(obj, options = undefined)`
617
+ - `Person.createArray(array, options = undefined)`
618
+ - `Person.createPage(page, options = undefined)`
619
+ - `Person.isNullishOrEmpty(obj)`
620
+ - `Person.parseJsonString(json, options = undefined)`
621
+ -
622
+ **NOTE:**
623
+
624
+ - Because the `Credential` class does not have an `id` attribute, the `@Model`
625
+ decorator does not add a `generateId()` instance method to it.
626
+ - Because `Person` already implements the `Person.prototype.equals()` method,
627
+ the `@Model` decorator will **not** override its own implementation of
628
+ the `Person.prototype.equals()` method.
629
+
630
+ ### <span id="enum">@Enum Decorator</span>
631
+
632
+ This decorator is used to decorate an enumeration class.
633
+
634
+ #### <span id="enum-fields">Enumerator Fields</span>
635
+
636
+ An enumeration class is a class whose instances are enumerators. An enumerator
637
+ is an object with the following properties:
638
+ - `value`:the value of the enumerator, which is exactly the name of the
639
+ static field of the enumeration class that corresponds to the enumerator.
640
+ - `name`: the display name of the enumerator, which could be specified
641
+ by the default string or object value of the static field of the
642
+ enumeration class that corresponds to the enumerator. It the default value
643
+ is not specified, the name of the enumerator is the same as its value.
644
+ - `i18n`: the i18n key of the enumerator, which is an optional property. It
645
+ could be specified by the default object value of the static field of the
646
+ enumeration class that corresponds to the enumerator. If this property is
647
+ specified, the `name` property will be transformed to a `getter`, which will
648
+ get the i18n value of the enumerator from the i18n resource bundle.
649
+ - `code`: the code of the enumerator, which is an optional property. It could
650
+ be specified by the default object value of the static field of the
651
+ enumeration class that corresponds to the enumerator.
652
+ - other properties: other properties of the enumerator could be specified
653
+ by the default object value of the static field of the enumeration class
654
+ that corresponds to the enumerator.
655
+
656
+ #### <span id="enum-toString">Instance method: Class.prototype.toString()</span>
657
+
658
+ - Parameters: none.
659
+ - Returns:
660
+ - `string`: the string representation of this enumerator, which is the
661
+ `value` of this enumerator.
662
+
663
+ This function returns the string representation of this enumerator, which is
664
+ the `value` of this enumerator.
665
+
666
+ #### <span id="enum-toJSON">Instance method: Class.prototype.toJSON()</span>
667
+
668
+ - Parameters: none.
669
+ - Returns:
670
+ - `string`: the JSON representation of this enumerator, which is the JSON
671
+ string representation of the `value` of this enumerator, i.e., the double
672
+ quoted string of the `value`.
673
+
674
+ This function returns the JSON representation of this enumerator.
675
+
676
+ #### <span id="enum-values">Class method: Class.values()</span>
677
+
678
+ - Parameters: none.
679
+ - Returns:
680
+ - `Class[]`: the array of all enumerators of this enumeration class.
681
+
682
+ This function returns the array of all enumerators of this enumeration class.
683
+
684
+ #### <span id="enum-ofValue">Class method: Class.ofValue(value)</span>
685
+
686
+ - Parameters:
687
+ - `value: string`: the value of the enumerator to be returned. Note that this
688
+ argument will be trimmed and uppercased to get the actual value of the
689
+ enumerator.
690
+ - Returns:
691
+ - `Class`: the enumerator in this enumeration class with the specified value,
692
+ or `undefined` if no such enumerator exists.
693
+
694
+ This function returns the enumerator with the specified value.
695
+
696
+ #### <span id="enum-hasValue">Class method: Class.hasValue(value)</span>
697
+
698
+ - Parameters:
699
+ - `value: string`: the value of the enumerator to be tested. Note that this
700
+ argument will be trimmed and uppercased to get the actual value of the
701
+ enumerator.
702
+ - Returns:
703
+ - `boolean`: returns `true` if there is an enumerator in this enumeration
704
+ class with the specified value, or `false` otherwise.
705
+
706
+ This function tests whether there is an enumerator with the specified value.
707
+
708
+ #### <span id="enum-ofName">Class method: Class.ofName(name)</span>
709
+
710
+ - Parameters:
711
+ - `name: string`: the name of the enumerator to be returned.
712
+ - Returns:
713
+ - `Class`: the enumerator in this enumeration class with the specified name,
714
+ or `undefined` if no such enumerator exists.
715
+
716
+ This function returns the enumerator with the specified name.
717
+
718
+ #### <span id="enum-hasName">Class method: Class.hasName(name)</span>
719
+
720
+ - Parameters:
721
+ - `name: string`: the name of the enumerator to be tested.
722
+ - Returns:
723
+ - `boolean`: returns `true` if there is an enumerator in this enumeration
724
+ class with the specified name, or `false` otherwise.
725
+
726
+ This function tests whether there is an enumerator with the specified name.
727
+
728
+ #### <span id="enum-ofCode">Class method: Class.ofCode(code)</span>
729
+
730
+ - Parameters:
731
+ - `code: string`: the code of the enumerator to be returned.
732
+ - Returns:
733
+ - `Class`: the enumerator in this enumeration class with the specified code,
734
+ or `undefined` if no such enumerator exists.
735
+
736
+ This function returns the enumerator with the specified value.
737
+
738
+ #### <span id="enum-hasCode">Class method: Class.hasCode(code)</span>
739
+
740
+ - Parameters:
741
+ - `code: string`: the code of the enumerator to be tested.
742
+ - Returns:
743
+ - `boolean`: returns `true` if there is an enumerator in this enumeration
744
+ class with the specified code, or `false` otherwise.
745
+
746
+ This function tests whether there is an enumerator with the specified code.
747
+
748
+ #### <span id="enum-of">Class method: Class.of(expr)</span>
749
+
750
+ - Parameters:
751
+ - `expr: object | string`: the expression corresponds to the enumerator to
752
+ be returned. The expression could be one of the following:
753
+ - an enumerator of this enumeration class;
754
+ - or the value of an enumerator of this enumeration class;
755
+ - or the name of an enumerator of this enumeration class;
756
+ - or the code of an enumerator of this enumeration class.
757
+ - Returns:
758
+ - `Class`: the enumerator in this enumeration class corresponds to the
759
+ specified expression, or `undefined` if no such enumerator exists.
760
+
761
+ This function returns the enumerator with the specified value.
762
+
763
+ #### <span id="enum-has">Class method: Class.has(expr)</span>
764
+
765
+ - Parameters:
766
+ - `expr: object | string`: the expression corresponds to the enumerator to
767
+ be returned. The expression could be one of the following:
768
+ - an enumerator of this enumeration class;
769
+ - or the value of an enumerator of this enumeration class;
770
+ - or the name of an enumerator of this enumeration class;
771
+ - or the code of an enumerator of this enumeration class.
772
+ - Returns:
773
+ - `boolean`: returns `true` if there is an enumerator in this enumeration
774
+ class corresponds to the specified expression, or `false` otherwise.
775
+
776
+ This function tests whether there is an enumerator with the specified code.
777
+
778
+ #### <span id="enum-usage-example">Usage Example</span>
779
+
780
+ ```js
781
+ @Enum
782
+ class Gender {
783
+ static MALE = 'Male';
784
+ static FEMALE = 'Female';
785
+ }
786
+ ```
787
+ The above code is equivalent to the following code:
788
+ ```js
789
+ class Gender {
790
+ static MALE = Object.freeze(new Gender('MALE', 'Male'));
791
+
792
+ static FEMALE = Object.freeze(new Gender('FEMALE', 'Female'));
793
+
794
+ static values() {
795
+ return [ Gender.MALE, Gender.FEMALE ];
796
+ }
797
+
798
+ static ofValue(value) {
799
+ switch (value) {
800
+ case 'MALE':
801
+ return Gender.MALE;
802
+ case 'FEMALE':
803
+ return Gender.FEMALE;
804
+ default:
805
+ return undefined;
806
+ }
807
+ }
808
+
809
+ static hasValue(value) {
810
+ return Gender.ofValue(value) !== undefined;
811
+ }
812
+
813
+ static ofName(name) {
814
+ return Gender.values().find((e) => e.name === name);
815
+ }
816
+
817
+ static hasName(name) {
818
+ return Gender.ofName(name) !== undefined;
819
+ }
820
+
821
+ static ofCode(code) {
822
+ return Gender.values().find((e) => e.code === code);
823
+ }
824
+
825
+ static hasCode(code) {
826
+ return Gender.ofCode(code) !== undefined;
827
+ }
828
+
829
+ static of(expr) {
830
+ if (expr instanceof Gender) {
831
+ return expr;
832
+ } else {
833
+ return Gender.ofValue(expr) ?? Gender.ofName(expr) ?? Gender.ofCode(expr);
834
+ }
835
+ }
836
+
837
+ static has(expr) {
838
+ return Gender.of(expr) !== undefined;
839
+ }
840
+
841
+ constructor(value, name) {
842
+ this.value = value;
843
+ this.name = name;
844
+ }
845
+
846
+ toString() {
847
+ return this.value;
848
+ }
849
+
850
+ toJSON() {
851
+ return this.value;
852
+ }
853
+ }
854
+ ```
855
+
856
+ The static fields of the enumeration class could also be specified as objects,
857
+ which will be used to initialize the enumerators. For example:
858
+ ```js
859
+ @Enum
860
+ class Gender {
861
+ static MALE = { name: 'Male', i18n: 'i18n.gender.male', code: '001', data: { value: 0 } };
862
+
863
+ static FEMALE = { name: 'Female', i18n: 'i18n.gender.female', code: '002', data: { value: 1 } };
864
+ }
865
+ ```
866
+ The above code is equivalent to the following code:
867
+ ```js
868
+ class Gender {
869
+ static MALE = Object.freeze(new Gender('MALE', 'Male',
870
+ { i18n: 'i18n.gender.male', code: '001', data: {value: 0 } }));
871
+
872
+ static FEMALE = Object.freeze(new Gender('FEMALE', 'Female',
873
+ { i18n: 'i18n.gender.female', code: '002', data: {value: 1 } }));
874
+
875
+ ...
876
+
877
+ constructor(value, name, payload) {
878
+ this.value = value;
879
+ this.name = name;
880
+ Object.assign(this, payload);
881
+ }
882
+
883
+ ...
884
+ }
885
+ ```
886
+ Note that the enumerator in the above `Gender` class has a `code`, `i18n`
887
+ and `data` properties. Since it has `i18n` property which specifies the i18n
888
+ key of the enumerator in the resource bundle, the `name` property of the
889
+ enumerator will be transformed to a `getter` which will get the i18n value
890
+ corresponding to the i18n key from the i18n resource bundle.
891
+
892
+ The enumerators can also be defined without default values, for example:
893
+ ```js
894
+ @Enum
895
+ class Gender {
896
+ static MALE;
897
+ static FEMALE;
898
+ }
899
+ ```
900
+ The above code is equivalent to the following code:
901
+ ```js
902
+ class Gender {
903
+ static MALE = Object.freeze(new Gender('MALE'));
904
+
905
+ static FEMALE = Object.freeze(new Gender('FEMALE'));
906
+
907
+ ...
908
+
909
+ constructor(value) {
910
+ this.value = value;
911
+ this.name = value;
912
+ }
913
+
914
+ ...
915
+ }
916
+ ```
917
+ That is, the name of the enumerator is exactly its value.
918
+
919
+ ### <span id="default-options">`DefaultOptions` class</span>
920
+
921
+ The `DefaultOptions` class is used to get or set the default options of different
922
+ aspects of this library.
923
+
924
+ The class accesses an internal `Map` object. The key of the map is the name
925
+ of aspects, and the value of the map is an object representing the default
926
+ options of the aspect.
927
+
928
+ For example, the default options of the `assign()` method of the class
929
+ decorated by `@Model` is stored in the key `assign`. That is,
930
+ `DefaultOption.get('assign')` returns the object representing the default
931
+ options of the `assign()` method.
932
+
933
+ The program can change the default options with `DefaultOptions.set('key', options)`
934
+ method.
935
+
936
+ Currently, the following aspects are supported:
937
+ - `assign`: the default options of the `Class.prototype.assign()`,
938
+ `Class.create()`, `Class.createArray()`, `Class.createPage()`,
939
+ `Class.parseJsonString()` methods of the class decorated by `@Model`.
940
+ - `toJSON`: the default options of the `Class.prototype.toJSON()`,
941
+ `Class.prototype.toJsonString()` methods of the class decorated by `@Model`.
942
+
943
+ #### <span id="default-options-get">Class method: `DefaultOptions.get(aspect)`</span>
944
+
945
+ Gets the default options of the specified aspect.
946
+
947
+ The function returns the object representing the default options of the aspect,
948
+ or `undefined`if the aspect does not exist. Note that the returned object is a
949
+ deep cloned copy of the object stored in the internal map, so that the
950
+ modification of the returned object will **not** affect the default options
951
+ stored in the internal map.
952
+
953
+ ```js
954
+ import { DefaultOptions } from '@qubit-ltd/common-decorator';
955
+
956
+ const opt1 = DefaultOptions.get('assign');
957
+ expect(opt1.convertNaming).toBe(false);
958
+ opt1.convertNaming = true;
959
+ const opt2 = DefaultOptions.get('assign');
960
+ expect(opt2.convertNaming).toBe(false);
961
+ ```
962
+
963
+ #### <span id="default-options-set">Class method: `DefaultOptions.set(aspect, options)`</span>
964
+
965
+ Sets the default options of the specified aspect.
966
+
967
+ This function will merge the new options into the old default options of the aspect.
968
+ If the new options have the same property as the old default options stored
969
+ in the internal map, the value of the new options will override the value of the
970
+ old default options; otherwise, the new property will be added to the old default
971
+ options.
972
+
973
+ ```js
974
+ import { DefaultOptions } from '@qubit-ltd/common-decorator';
975
+
976
+ const opt1 = DefaultOptions.get('assign');
977
+ expect(opt1.convertNaming).toBe(false);
978
+ DefaultOptions.set('assign', { convertNaming: true });
979
+ const opt2 = DefaultOptions.get('assign');
980
+ expect(opt2.convertNaming).toBe(true);
981
+ expect(opt1.convertNaming).toBe(false);
982
+ ```
983
+
984
+
985
+ #### <span id="default-options-merge">Class method: `DefaultOptions.merge(aspect, options)`</span>
986
+
987
+ Gets the default options of the specified aspect, merging the provided default
988
+ options into the returned object.
989
+
990
+ **NOTE:** This function does **NOT** change the default options stored in the
991
+ internal map, instead, it returns a new object representing the merged options.
992
+
993
+ ```js
994
+ import { DefaultOptions } from '@qubit-ltd/common-decorator';
995
+
996
+ const opt1 = DefaultOptions.get('assign');
997
+ expect(opt1.convertNaming).toBe(false);
998
+ const opt2 = DefaultOptions.merge('assign', { convertNaming: true });
999
+ expect(opt2.convertNaming).toBe(true);
1000
+ expect(opt1.convertNaming).toBe(false);
1001
+ const opt3 = DefaultOptions.merge('assign', null);
1002
+ expect(opt3.convertNaming).toBe(false);
1003
+ ```
1004
+
1005
+ ## <span id="configuration">Configuration</span>
1006
+
1007
+ This library uses the most recent (currently May 2023)
1008
+ [stage 3 proposal of JavaScript decorators]. Therefore, you must configure
1009
+ [Babel] with [@babel/plugin-transform-class-properties] and the
1010
+ [@babel/plugin-proposal-decorators] plugins.
1011
+
1012
+ **NOTE:** To support the [stage 3 proposal of JavaScript decorator metadata],
1013
+ the version of the [Babel] plugin [@babel/plugin-proposal-decorators] must be
1014
+ at least `7.23.0`.
1015
+
1016
+ ### <span id="webpack">Bundling with [webpack]</span>
1017
+
1018
+ 1. Install the required dependencies:
1019
+ ```shell
1020
+ yarn add @qubit-ltd/common-decorator
1021
+ yarn add --dev @babel/core @babel/runtime @babel/preset-env
1022
+ yarn add --dev @babel/plugin-proposal-decorators @babel/plugin-transform-class-properties @babel/plugin-transform-runtime
1023
+ ```
1024
+ 2. Configure [Babel] by using the [@babel/plugin-transform-class-properties]
1025
+ and [@babel/plugin-proposal-decorators] plugins. A possible [Babel]
1026
+ configuration file `babelrc.json` is as follows:
1027
+ ```json
1028
+ {
1029
+ "presets": [
1030
+ "@babel/preset-env"
1031
+ ],
1032
+ "plugins": [
1033
+ "@babel/plugin-transform-runtime",
1034
+ ["@babel/plugin-proposal-decorators", { "version": "2023-05" }],
1035
+ "@babel/plugin-transform-class-properties"
1036
+ ]
1037
+ }
1038
+ ```
1039
+
1040
+ ### <span id="vite">Bundling with [vite]</span>
1041
+
1042
+ 1. Install the required dependencies:
1043
+ ```shell
1044
+ yarn add @qubit-ltd/common-decorator
1045
+ yarn add --dev @babel/core @babel/runtime @babel/preset-env
1046
+ yarn add --dev @babel/plugin-proposal-decorators @babel/plugin-transform-class-properties @babel/plugin-transform-runtime
1047
+ ```
1048
+ 2. Configure [Babel] by using [@babel/plugin-transform-class-properties] and
1049
+ [@babel/plugin-proposal-decorators] plugins. A possible [Babel] configuration
1050
+ file `babelrc.json` is as follows:
1051
+ ```json
1052
+ {
1053
+ "presets": [
1054
+ ["@babel/preset-env", { "modules": false }]
1055
+ ],
1056
+ "plugins": [
1057
+ "@babel/plugin-transform-runtime",
1058
+ ["@babel/plugin-proposal-decorators", { "version": "2023-05" }],
1059
+ "@babel/plugin-transform-class-properties"
1060
+ ]
1061
+ }
1062
+ ```
1063
+ **Note:** When bundling with [vite], make sure to set the `modules` parameter
1064
+ of `@babel/preset-env` to `false`.
1065
+ 3. Configure [vite] by modifying the `vite.config.js` file to add support for
1066
+ [Babel]. A possible `vite.config.js` file is as follows:
1067
+ ```js
1068
+ import { fileURLToPath, URL } from 'node:url';
1069
+ import { defineConfig } from 'vite';
1070
+ import vue from '@vitejs/plugin-vue';
1071
+ import * as babel from '@babel/core';
1072
+
1073
+ // A very simple Vite plugin support babel transpilation
1074
+ const babelPlugin = {
1075
+ name: 'plugin-babel',
1076
+ transform: (src, id) => {
1077
+ if (/\.(jsx?|vue)$/.test(id)) { // the pattern of the file to handle
1078
+ return babel.transform(src, {
1079
+ filename: id,
1080
+ babelrc: true,
1081
+ });
1082
+ }
1083
+ },
1084
+ };
1085
+ // https://vitejs.dev/config/
1086
+ export default defineConfig({
1087
+ plugins: [
1088
+ vue({
1089
+ script: {
1090
+ babelParserPlugins: ['decorators'], // must enable decorators support
1091
+ },
1092
+ }),
1093
+ babelPlugin, // must be after the vue plugin
1094
+ ],
1095
+ resolve: {
1096
+ alias: {
1097
+ '@': fileURLToPath(new URL('./src', import.meta.url)),
1098
+ },
1099
+ },
1100
+ });
1101
+ ```
1102
+ **Note:** In the above configuration file, we've implemented a simple [vite]
1103
+ plugin to transpile the code processed by the [vite-plugin-vue] plugin using
1104
+ [Babel]. Although there's a [vite-plugin-babel] plugin that claims to add
1105
+ [Babel] support to [vite], we found it doesn't correctly handle [vue] Single
1106
+ File Components (SFCs). After closely examining its source code, we
1107
+ determined that to achieve correct transpilation, we need to apply [Babel]
1108
+ after [vite-plugin-vue] processes the source code. Therefore, the very
1109
+ simple plugin function above suffices for our needs. As an alternative,
1110
+ you can use [our version of vite-plugin-babel], and the following is an
1111
+ example configuration:
1112
+ ```js
1113
+ import { fileURLToPath, URL } from 'node:url';
1114
+ import { defineConfig } from 'vite';
1115
+ import vue from '@vitejs/plugin-vue';
1116
+ import babel from '@qubit-ltd/vite-plugin-babel';
1117
+
1118
+ export default defineConfig({
1119
+ plugins: [
1120
+ vue({
1121
+ script: {
1122
+ babelParserPlugins: ['decorators'], // must enable decorators support
1123
+ },
1124
+ }),
1125
+ babel(),
1126
+ ],
1127
+ resolve: {
1128
+ alias: {
1129
+ '@': fileURLToPath(new URL('./src', import.meta.url)),
1130
+ },
1131
+ },
1132
+ });
1133
+ ```
1134
+
1135
+ ## <span id="contributing">Contributing</span>
1136
+
1137
+ If you find any issues or have suggestions for improvements, please feel free
1138
+ to open an issue or submit a pull request to the [GitHub repository].
1139
+
1140
+ ## <span id="license">License</span>
1141
+
1142
+ [@qubit-ltd/common-decorator] is distributed under the Apache 2.0 license.
1143
+ See the [LICENSE](LICENSE) file for more details.
1144
+
1145
+ [@qubit-ltd/common-decorator]: https://npmjs.com/package/@qubit-ltd/common-decorator
1146
+ [Babel]: https://babeljs.io/
1147
+ [@babel/plugin-transform-class-properties]: https://babeljs.io/docs/babel-plugin-transform-class-properties
1148
+ [@babel/plugin-proposal-decorators]: https://babeljs.io/docs/babel-plugin-proposal-decorators
1149
+ [stage 3 proposal of JavaScript decorators]: https://github.com/tc39/proposal-decorators
1150
+ [stage 3 proposal of JavaScript decorator metadata]: https://github.com/tc39/proposal-decorator-metadata
1151
+ [GitHub repository]: https://github.com/Haixing-Hu/js-common-decorator
1152
+ [webpack]: https://webpack.js.org/
1153
+ [vite]: https://vitejs.dev/
1154
+ [vite-plugin-vue]: https://www.npmjs.com/package/@vitejs/plugin-vue
1155
+ [vite-plugin-babel]: https://www.npmjs.com/package/vite-plugin-babel
1156
+ [our version of vite-plugin-babel]: https://npmjs.com/package/@qubit-ltd/vite-plugin-babel