@qubit-ltd/common-decorator 3.9.0 → 3.10.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.
- package/README.md +32 -7
- package/README.zh_CN.md +29 -3
- package/dist/common-decorator.cjs +43 -21
- package/dist/common-decorator.cjs.map +1 -1
- package/dist/common-decorator.min.cjs +1 -1
- package/dist/common-decorator.min.cjs.map +1 -1
- package/dist/common-decorator.min.mjs +1 -1
- package/dist/common-decorator.min.mjs.map +1 -1
- package/dist/common-decorator.mjs +43 -21
- package/dist/common-decorator.mjs.map +1 -1
- package/doc/api/DefaultAssignmentOptions.html +1 -1
- package/doc/api/DefaultOptions.html +1 -1
- package/doc/api/DefaultToJsonOptions.html +1 -1
- package/doc/api/Enum.html +1 -1
- package/doc/api/Model.html +27 -11
- package/doc/api/Page.html +1 -1
- package/doc/api/global.html +37 -3
- package/doc/api/index.html +29 -8
- package/doc/common-decorator.min.visualization.html +1 -1
- package/doc/common-decorator.visualization.html +1 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
[](https://dl.circleci.com/status-badge/redirect/gh/Haixing-Hu/js-common-decorator/tree/master)
|
|
7
7
|
[](https://coveralls.io/github/Haixing-Hu/js-common-decorator?branch=master)
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
supports the most recent (
|
|
12
|
-
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
[@qubit-ltd/common-decorator] is a JavaScript library of common decorators that provides powerful tools to enhance your domain classes. The library supports the most recent (as of November 2023) [stage 3 proposal of JavaScript decorators].
|
|
12
|
+
|
|
13
|
+
With this library, you can easily add common methods to your domain classes, implement Java-like enum functionality, add validation and normalization capabilities, and much more - all using the modern decorator syntax.
|
|
13
14
|
|
|
14
15
|
## Features
|
|
15
16
|
|
|
@@ -20,6 +21,7 @@ supports the most recent (currently November 2023)
|
|
|
20
21
|
- **Normalization Support**: `@Normalizable` decorator enables field normalization
|
|
21
22
|
- **Type Safety**: `@Type` and `@ElementType` decorators for type checking
|
|
22
23
|
- **Serialization Utilities**: Built-in JSON serialization/deserialization support
|
|
24
|
+
- **High Test Coverage**: Comprehensive test suite ensuring reliability of all features
|
|
23
25
|
|
|
24
26
|
## Installation
|
|
25
27
|
|
|
@@ -29,6 +31,9 @@ npm install @qubit-ltd/common-decorator
|
|
|
29
31
|
|
|
30
32
|
# Using yarn
|
|
31
33
|
yarn add @qubit-ltd/common-decorator
|
|
34
|
+
|
|
35
|
+
# Using pnpm
|
|
36
|
+
pnpm add @qubit-ltd/common-decorator
|
|
32
37
|
```
|
|
33
38
|
|
|
34
39
|
## <span id="content">Table of Contents</span>
|
|
@@ -74,6 +79,7 @@ yarn add @qubit-ltd/common-decorator
|
|
|
74
79
|
- [Configuration](#configuration)
|
|
75
80
|
- [Bundling with webpack](#webpack)
|
|
76
81
|
- [Bundling with vite](#vite)
|
|
82
|
+
- [Recent Updates](#recent-updates)
|
|
77
83
|
- [Contributing](#contributing)
|
|
78
84
|
- [License](#license)
|
|
79
85
|
|
|
@@ -121,9 +127,11 @@ class.
|
|
|
121
127
|
- `object`: the calling object itself.
|
|
122
128
|
|
|
123
129
|
This function copies the fields of the object `obj` to this object, only copying
|
|
124
|
-
fields defined in this object's class. If a field in the `obj` object
|
|
125
|
-
|
|
126
|
-
`
|
|
130
|
+
fields defined in this object's class. If a field doesn't exist in the `obj` object,
|
|
131
|
+
it sets the field's value to the default value. If a field exists in the `obj` object
|
|
132
|
+
but its value is `null` or `undefined`, the function preserves the `null` or `undefined`
|
|
133
|
+
value rather than setting it to the default value. Note that `obj` can have a different
|
|
134
|
+
prototype to this object.
|
|
127
135
|
|
|
128
136
|
#### <span id="model-clone">Instance method: Class.prototype.clone()</span>
|
|
129
137
|
|
|
@@ -221,6 +229,9 @@ fields of this object. If `fields` is an array of strings, it normalizes all the
|
|
|
221
229
|
normalizable fields whose names are specified in the array. Note that a field is
|
|
222
230
|
normalizable if and only if it is decorated by the `@Normalizable` decorator.
|
|
223
231
|
|
|
232
|
+
**IMPORTANT**: If a field value is `null` or `undefined`, the normalization process will
|
|
233
|
+
preserve the `null` or `undefined` value rather than replacing it with a default value.
|
|
234
|
+
|
|
224
235
|
#### <span id="model-validateField">Instance method: Class.prototype.validateField(field)</span>
|
|
225
236
|
|
|
226
237
|
- Parameters:
|
|
@@ -1152,6 +1163,20 @@ must be at least `7.24.0`.
|
|
|
1152
1163
|
});
|
|
1153
1164
|
```
|
|
1154
1165
|
|
|
1166
|
+
## <span id="recent-updates">Recent Updates</span>
|
|
1167
|
+
|
|
1168
|
+
### Test Coverage Enhancements (December 2023)
|
|
1169
|
+
|
|
1170
|
+
- **Enum Clone Hook Test**: Added comprehensive tests to verify the behavior of enum clone hooks, ensuring that enumerator objects are properly handled during cloning operations.
|
|
1171
|
+
- **Field Validation Coverage**: Enhanced test coverage for field validation, specifically targeting edge cases to ensure robust validation in all scenarios.
|
|
1172
|
+
- **Model Implementation Tests**: Added additional tests for various model implementation functions to achieve higher code coverage.
|
|
1173
|
+
|
|
1174
|
+
The latest updates focus on improving test coverage and reliability, with particular attention to:
|
|
1175
|
+
|
|
1176
|
+
- Ensuring enumerator objects maintain their singleton pattern during cloning
|
|
1177
|
+
- Validating empty fields with proper error handling
|
|
1178
|
+
- Verifying that model implementation functions work correctly in all edge cases
|
|
1179
|
+
|
|
1155
1180
|
## <span id="contributing">Contributing</span>
|
|
1156
1181
|
|
|
1157
1182
|
If you find any issues or have suggestions for improvements, please feel free
|
package/README.zh_CN.md
CHANGED
|
@@ -6,8 +6,11 @@
|
|
|
6
6
|
[](https://dl.circleci.com/status-badge/redirect/gh/Haixing-Hu/js-common-decorator/tree/master)
|
|
7
7
|
[](https://coveralls.io/github/Haixing-Hu/js-common-decorator?branch=master)
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
## 概述
|
|
10
|
+
|
|
11
|
+
[@qubit-ltd/common-decorator] 是一个JavaScript通用装饰器库,为您的领域类提供强大的增强工具。该库支持最新的(截至2023年11月)JavaScript装饰器[stage 3 提案]。
|
|
12
|
+
|
|
13
|
+
使用这个库,您可以轻松地为领域类添加常用方法,实现类似Java的枚举功能,添加验证和规范化功能等 - 所有这些都使用现代装饰器语法。
|
|
11
14
|
|
|
12
15
|
## 特性
|
|
13
16
|
|
|
@@ -18,6 +21,7 @@
|
|
|
18
21
|
- **规范化支持**:`@Normalizable` 装饰器实现字段规范化
|
|
19
22
|
- **类型安全**:`@Type` 和 `@ElementType` 装饰器用于类型检查
|
|
20
23
|
- **序列化工具**:内置 JSON 序列化/反序列化支持
|
|
24
|
+
- **高测试覆盖率**:全面的测试套件确保所有功能的可靠性
|
|
21
25
|
|
|
22
26
|
## 安装
|
|
23
27
|
|
|
@@ -27,6 +31,9 @@ npm install @qubit-ltd/common-decorator
|
|
|
27
31
|
|
|
28
32
|
# 使用 yarn
|
|
29
33
|
yarn add @qubit-ltd/common-decorator
|
|
34
|
+
|
|
35
|
+
# 使用 pnpm
|
|
36
|
+
pnpm add @qubit-ltd/common-decorator
|
|
30
37
|
```
|
|
31
38
|
|
|
32
39
|
## <span id="content">目录</span>
|
|
@@ -72,6 +79,7 @@ yarn add @qubit-ltd/common-decorator
|
|
|
72
79
|
- [配置](#configuration)
|
|
73
80
|
- [使用 webpack 打包](#webpack)
|
|
74
81
|
- [使用 vite 打包](#vite)
|
|
82
|
+
- [最新更新](#recent-updates)
|
|
75
83
|
- [贡献](#contributing)
|
|
76
84
|
- [许可证](#license)
|
|
77
85
|
|
|
@@ -104,7 +112,8 @@ yarn add @qubit-ltd/common-decorator
|
|
|
104
112
|
- `object`:调用该方法的对象自身。
|
|
105
113
|
|
|
106
114
|
此函数将 `obj` 对象的字段复制到当前对象中,仅复制当前对象类中定义的字段。
|
|
107
|
-
如果 `obj`
|
|
115
|
+
如果 `obj` 中不存在某个字段,则将当前对象对应字段设为默认值。但如果 `obj` 中某个字段存在
|
|
116
|
+
且值为 `null` 或 `undefined`,函数会保留这些 `null` 或 `undefined` 值,而不是设为默认值。
|
|
108
117
|
注意,`obj` 可以与当前对象有不同的原型。
|
|
109
118
|
|
|
110
119
|
#### <span id="model-clone">实例方法:Class.prototype.clone()</span>
|
|
@@ -181,6 +190,9 @@ yarn add @qubit-ltd/common-decorator
|
|
|
181
190
|
如果 `fields` 是一个字符串数组,则规范化数组中指定名称的所有可规范化字段。
|
|
182
191
|
请注意,字段只有在被 `@Normalizable` 装饰器装饰时才是可规范化的。
|
|
183
192
|
|
|
193
|
+
**重要提示**:如果字段值为 `null` 或 `undefined`,规范化过程将会保留这些 `null` 或 `undefined` 值,
|
|
194
|
+
而不会将其替换为默认值。
|
|
195
|
+
|
|
184
196
|
#### <span id="model-validateField">实例方法:Class.prototype.validateField(field)</span>
|
|
185
197
|
|
|
186
198
|
- 参数:
|
|
@@ -927,6 +939,20 @@ expect(opt1.convertNaming).toBe(false);
|
|
|
927
939
|
});
|
|
928
940
|
```
|
|
929
941
|
|
|
942
|
+
## <span id="recent-updates">最新更新</span>
|
|
943
|
+
|
|
944
|
+
### 测试覆盖增强(2023年12月)
|
|
945
|
+
|
|
946
|
+
- **枚举克隆钩子测试**:添加了全面的测试,验证枚举克隆钩子的行为,确保在克隆操作中正确处理枚举器对象。
|
|
947
|
+
- **字段验证覆盖**:增强了字段验证的测试覆盖率,特别针对边缘情况,确保在所有场景下都能进行可靠的验证。
|
|
948
|
+
- **模型实现测试**:为各种模型实现函数添加了额外的测试,以实现更高的代码覆盖率。
|
|
949
|
+
|
|
950
|
+
最新更新主要集中在提高测试覆盖率和可靠性,特别关注:
|
|
951
|
+
|
|
952
|
+
- 确保枚举器对象在克隆过程中保持单例模式
|
|
953
|
+
- 验证空字段的处理与适当的错误处理
|
|
954
|
+
- 验证模型实现函数在所有边缘情况下都能正常工作
|
|
955
|
+
|
|
930
956
|
## <span id="contributing">贡献</span>
|
|
931
957
|
|
|
932
958
|
如果你发现任何问题或有改进建议,请随时在 [GitHub 仓库] 上提交 issue 或 pull request。
|
|
@@ -1042,7 +1042,8 @@ function enumNormalizer(EnumClass) {
|
|
|
1042
1042
|
* A default normalizer for a non-static class field.
|
|
1043
1043
|
*
|
|
1044
1044
|
* This normalizer does the following things:
|
|
1045
|
-
* - If the value is `undefined` or `null`, it returns the value itself
|
|
1045
|
+
* - If the value is `undefined` or `null`, it returns the value itself without
|
|
1046
|
+
* changing it;
|
|
1046
1047
|
* - If the value is a string, it returns the trimmed string;
|
|
1047
1048
|
* - If the value is a collection, it returns the same type of collection whose
|
|
1048
1049
|
* elements are normalized by the default normalizer;
|
|
@@ -10266,7 +10267,12 @@ function normalizeArrayField(Class, obj, field, value, options, normalizer) {
|
|
|
10266
10267
|
elementTypes: options.elementTypes
|
|
10267
10268
|
};
|
|
10268
10269
|
obj[field] = value.map(function (v) {
|
|
10269
|
-
|
|
10270
|
+
// for null or undefined values, keep the original value without normalization
|
|
10271
|
+
if (v === null || v === undefined) {
|
|
10272
|
+
return v;
|
|
10273
|
+
} else {
|
|
10274
|
+
return normalizer(v, context);
|
|
10275
|
+
}
|
|
10270
10276
|
});
|
|
10271
10277
|
return true;
|
|
10272
10278
|
}
|
|
@@ -10416,19 +10422,16 @@ function normalizeNormalField(Class, obj, field, value, options, normalizer) {
|
|
|
10416
10422
|
* @param {any} value
|
|
10417
10423
|
* The value of the specified field of the specified object.
|
|
10418
10424
|
* @returns {boolean}
|
|
10419
|
-
* If the field value is nullish, this function
|
|
10420
|
-
*
|
|
10421
|
-
* value of the default instance of the specified class, and returns `true`;
|
|
10425
|
+
* If the field value is nullish, this function returns `true` and preserves
|
|
10426
|
+
* the nullish value (null or undefined) in the object's field;
|
|
10422
10427
|
* otherwise, this function does nothing and returns `false`.
|
|
10423
10428
|
* @author Haixing Hu
|
|
10424
10429
|
* @private
|
|
10425
10430
|
*/
|
|
10426
10431
|
function normalizeNullishField(Class, obj, field, value) {
|
|
10427
10432
|
if (value === undefined || value === null) {
|
|
10428
|
-
// For field values that are `undefined` or `null`, the
|
|
10429
|
-
//
|
|
10430
|
-
var defaultInstance = getDefaultInstance(Class);
|
|
10431
|
-
obj[field] = defaultInstance[field];
|
|
10433
|
+
// For field values that are `undefined` or `null`, preserve the original value
|
|
10434
|
+
// instead of replacing it with the default value
|
|
10432
10435
|
return true;
|
|
10433
10436
|
}
|
|
10434
10437
|
return false;
|
|
@@ -11475,12 +11478,13 @@ function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t
|
|
|
11475
11478
|
*
|
|
11476
11479
|
* - Instance method `assign(obj, options)`: Copies the properties of the object
|
|
11477
11480
|
* `obj` to this object, only copying properties defined in the class of this
|
|
11478
|
-
* object. If a property in
|
|
11479
|
-
*
|
|
11480
|
-
*
|
|
11481
|
-
* The
|
|
11482
|
-
*
|
|
11483
|
-
*
|
|
11481
|
+
* object. If a property in `obj` does not exist, it sets the property of this
|
|
11482
|
+
* object to the default value. If a property exists in `obj` but its value is
|
|
11483
|
+
* `null` or `undefined`, the function preserves the `null` or `undefined` value.
|
|
11484
|
+
* The function returns this object itself. Note that `obj` can have a different
|
|
11485
|
+
* prototype than this object. The `options` parameter is the additional options
|
|
11486
|
+
* for the assignment. Available options will be explained below. If the `options`
|
|
11487
|
+
* parameter is `undefined` or `null`, the default options will be used. The default
|
|
11484
11488
|
* options can be retrieved by calling `DefaultOptions.get('assign')`.
|
|
11485
11489
|
* - Instance method `clear()`: Sets all the properties of this object to their
|
|
11486
11490
|
* default values.
|
|
@@ -11707,8 +11711,10 @@ function Model(Class, context) {
|
|
|
11707
11711
|
* Copies the properties from a specified data object to this object, only
|
|
11708
11712
|
* copying properties defined in the class of this object.
|
|
11709
11713
|
*
|
|
11710
|
-
* If a property in the data object
|
|
11711
|
-
* sets the property of this object to the default value.
|
|
11714
|
+
* If a property in the data object doesn't exist, the function
|
|
11715
|
+
* sets the property of this object to the default value. However, if a property
|
|
11716
|
+
* exists in the data object but its value is `null` or `undefined`, the function
|
|
11717
|
+
* will preserve that `null` or `undefined` value in this object.
|
|
11712
11718
|
*
|
|
11713
11719
|
* Note that the data object may have a different prototype than this object.
|
|
11714
11720
|
*
|
|
@@ -11824,6 +11830,10 @@ function Model(Class, context) {
|
|
|
11824
11830
|
* A field is normalizable if and only if it is decorated with the
|
|
11825
11831
|
* `@{@link Normalizable}` decorator.
|
|
11826
11832
|
*
|
|
11833
|
+
* If a field value is `null` or `undefined`, the normalization process will
|
|
11834
|
+
* preserve the `null` or `undefined` value rather than replacing it with a
|
|
11835
|
+
* default value.
|
|
11836
|
+
*
|
|
11827
11837
|
* @param {undefined|string|array} fields
|
|
11828
11838
|
* the names of fields to be normalized. If this argument is not specified,
|
|
11829
11839
|
* or `undefined`, or `null`, or a string `'*'`, this function normalizes
|
|
@@ -12124,8 +12134,10 @@ function Model(Class, context) {
|
|
|
12124
12134
|
* It copies the property values from the corresponding properties of the
|
|
12125
12135
|
* specified data object maintaining the same prototype and class definition.
|
|
12126
12136
|
*
|
|
12127
|
-
* If a property in the data object
|
|
12128
|
-
*
|
|
12137
|
+
* If a property doesn't exist in the data object, the function sets the property
|
|
12138
|
+
* of the created instance to the default value. If a property exists in the data
|
|
12139
|
+
* object but its value is `null` or `undefined`, the function preserves the `null`
|
|
12140
|
+
* or `undefined` value.
|
|
12129
12141
|
*
|
|
12130
12142
|
* This method is usually used to transform a plain JSON object into the
|
|
12131
12143
|
* specified domain object.
|
|
@@ -12179,6 +12191,11 @@ function Model(Class, context) {
|
|
|
12179
12191
|
* copied from the corresponding elements in the data object array,
|
|
12180
12192
|
* maintaining the same prototype and class definition.
|
|
12181
12193
|
*
|
|
12194
|
+
* For each element in the array, if a property doesn't exist in the data object,
|
|
12195
|
+
* the function sets the property of the created instance to the default value.
|
|
12196
|
+
* If a property exists in the data object but its value is `null` or `undefined`,
|
|
12197
|
+
* the function preserves the `null` or `undefined` value.
|
|
12198
|
+
*
|
|
12182
12199
|
* This method is usually used to transform an array of plain JSON objects
|
|
12183
12200
|
* into an array of specified domain objects.
|
|
12184
12201
|
*
|
|
@@ -12231,6 +12248,11 @@ function Model(Class, context) {
|
|
|
12231
12248
|
* list of domain objects obtained from a server using the GET method, and
|
|
12232
12249
|
* the object should conform to the `Page` class definition.
|
|
12233
12250
|
*
|
|
12251
|
+
* For each element in the page content array, if a property doesn't exist in
|
|
12252
|
+
* the data object, the function sets the property of the created instance to
|
|
12253
|
+
* the default value. If a property exists in the data object but its value is
|
|
12254
|
+
* `null` or `undefined`, the function preserves the `null` or `undefined` value.
|
|
12255
|
+
*
|
|
12234
12256
|
* @param {object} page
|
|
12235
12257
|
* the specified pagination data object, which must conform to the
|
|
12236
12258
|
* `Page` class definition.
|
|
@@ -12786,11 +12808,11 @@ function createDeferredReadonlyInitializer(propertyName) {
|
|
|
12786
12808
|
* ```js
|
|
12787
12809
|
* class Meal {
|
|
12788
12810
|
* // Field with initial value - immediately read-only
|
|
12789
|
-
*
|
|
12811
|
+
* @Readonly
|
|
12790
12812
|
* entree = 'steak';
|
|
12791
12813
|
*
|
|
12792
12814
|
* // Field without initial value - will be read-only after first assignment
|
|
12793
|
-
*
|
|
12815
|
+
* @Readonly
|
|
12794
12816
|
* dessert;
|
|
12795
12817
|
* }
|
|
12796
12818
|
*
|