@qubit-ltd/common-decorator 3.9.1 → 3.10.1

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 CHANGED
@@ -127,9 +127,11 @@ class.
127
127
  - `object`: the calling object itself.
128
128
 
129
129
  This function copies the fields of the object `obj` to this object, only copying
130
- fields defined in this object's class. If a field in the `obj` object is
131
- `undefined` or `null`, it sets the field's value to the default value. Note that
132
- `obj` can have a different prototype to this object.
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.
133
135
 
134
136
  #### <span id="model-clone">Instance method: Class.prototype.clone()</span>
135
137
 
@@ -227,6 +229,9 @@ fields of this object. If `fields` is an array of strings, it normalizes all the
227
229
  normalizable fields whose names are specified in the array. Note that a field is
228
230
  normalizable if and only if it is decorated by the `@Normalizable` decorator.
229
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
+
230
235
  #### <span id="model-validateField">Instance method: Class.prototype.validateField(field)</span>
231
236
 
232
237
  - Parameters:
package/README.zh_CN.md CHANGED
@@ -112,7 +112,8 @@ pnpm add @qubit-ltd/common-decorator
112
112
  - `object`:调用该方法的对象自身。
113
113
 
114
114
  此函数将 `obj` 对象的字段复制到当前对象中,仅复制当前对象类中定义的字段。
115
- 如果 `obj` 中的字段为 `undefined` 或 `null`,将其值设为默认值。
115
+ 如果 `obj` 中不存在某个字段,则将当前对象对应字段设为默认值。但如果 `obj` 中某个字段存在
116
+ 且值为 `null` 或 `undefined`,函数会保留这些 `null` 或 `undefined` 值,而不是设为默认值。
116
117
  注意,`obj` 可以与当前对象有不同的原型。
117
118
 
118
119
  #### <span id="model-clone">实例方法:Class.prototype.clone()</span>
@@ -189,6 +190,9 @@ pnpm add @qubit-ltd/common-decorator
189
190
  如果 `fields` 是一个字符串数组,则规范化数组中指定名称的所有可规范化字段。
190
191
  请注意,字段只有在被 `@Normalizable` 装饰器装饰时才是可规范化的。
191
192
 
193
+ **重要提示**:如果字段值为 `null` 或 `undefined`,规范化过程将会保留这些 `null` 或 `undefined` 值,
194
+ 而不会将其替换为默认值。
195
+
192
196
  #### <span id="model-validateField">实例方法:Class.prototype.validateField(field)</span>
193
197
 
194
198
  - 参数:
@@ -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;
@@ -1607,7 +1608,6 @@ function ofValueImpl(Class, value) {
1607
1608
  * The default instance of the specified class, or a new instance will be
1608
1609
  * created if it does not exist.
1609
1610
  * @author Haixing Hu
1610
- * @private
1611
1611
  */
1612
1612
  function getDefaultInstance(Class) {
1613
1613
  if (Class.prototype === Object.prototype) {
@@ -1809,7 +1809,6 @@ function getFieldMetadata(metadata, field, key) {
1809
1809
  * @return {function|null}
1810
1810
  * the element type of the specified field of the object, or `null` if the
1811
1811
  * field element type cannot be inferred.
1812
- * @private
1813
1812
  * @author Haixing Hu
1814
1813
  */
1815
1814
  function getFieldElementType(Class, field) {
@@ -1877,7 +1876,6 @@ function getFieldElementType(Class, field) {
1877
1876
  * @return {function|undefined}
1878
1877
  * the type of the specified field of the object, or `undefined` if the field
1879
1878
  * type cannot be inferred.
1880
- * @private
1881
1879
  * @author Haixing Hu
1882
1880
  */
1883
1881
  function getFieldType(Class, field) {
@@ -1932,7 +1930,6 @@ function getFieldType(Class, field) {
1932
1930
  * @return {string}
1933
1931
  * The corresponding key of the source object.
1934
1932
  * @author Haixing Hu
1935
- * @private
1936
1933
  */
1937
1934
  function getSourceField(targetField, options) {
1938
1935
  if ((options === null || options === void 0 ? void 0 : options.convertNaming) === true) {
@@ -10266,7 +10263,12 @@ function normalizeArrayField(Class, obj, field, value, options, normalizer) {
10266
10263
  elementTypes: options.elementTypes
10267
10264
  };
10268
10265
  obj[field] = value.map(function (v) {
10269
- return normalizer(v, context);
10266
+ // for null or undefined values, keep the original value without normalization
10267
+ if (v === null || v === undefined) {
10268
+ return v;
10269
+ } else {
10270
+ return normalizer(v, context);
10271
+ }
10270
10272
  });
10271
10273
  return true;
10272
10274
  }
@@ -10416,19 +10418,16 @@ function normalizeNormalField(Class, obj, field, value, options, normalizer) {
10416
10418
  * @param {any} value
10417
10419
  * The value of the specified field of the specified object.
10418
10420
  * @returns {boolean}
10419
- * If the field value is nullish, this function normalizes the field of the
10420
- * specified object by setting its field value to the corresponding field
10421
- * value of the default instance of the specified class, and returns `true`;
10421
+ * If the field value is nullish, this function returns `true` and preserves
10422
+ * the nullish value (null or undefined) in the object's field;
10422
10423
  * otherwise, this function does nothing and returns `false`.
10423
10424
  * @author Haixing Hu
10424
10425
  * @private
10425
10426
  */
10426
10427
  function normalizeNullishField(Class, obj, field, value) {
10427
10428
  if (value === undefined || value === null) {
10428
- // For field values that are `undefined` or `null`, the normalized value
10429
- // should be the default value of the field.
10430
- var defaultInstance = getDefaultInstance(Class);
10431
- obj[field] = defaultInstance[field];
10429
+ // For field values that are `undefined` or `null`, preserve the original value
10430
+ // instead of replacing it with the default value
10432
10431
  return true;
10433
10432
  }
10434
10433
  return false;
@@ -11475,12 +11474,13 @@ function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t
11475
11474
  *
11476
11475
  * - Instance method `assign(obj, options)`: Copies the properties of the object
11477
11476
  * `obj` to this object, only copying properties defined in the class of this
11478
- * object. If a property in the `obj` object is `undefined` or `null`, it sets
11479
- * the property of this object to the default value. The function returns this
11480
- * object itself. Note that `obj` can have a different prototype than this object.
11481
- * The `options` parameter is the additional options for the assignment.
11482
- * Available options will be explained below. If the `options` parameter is
11483
- * `undefined` or `null`, the default options will be used. The default
11477
+ * object. If a property in `obj` does not exist, it sets the property of this
11478
+ * object to the default value. If a property exists in `obj` but its value is
11479
+ * `null` or `undefined`, the function preserves the `null` or `undefined` value.
11480
+ * The function returns this object itself. Note that `obj` can have a different
11481
+ * prototype than this object. The `options` parameter is the additional options
11482
+ * for the assignment. Available options will be explained below. If the `options`
11483
+ * parameter is `undefined` or `null`, the default options will be used. The default
11484
11484
  * options can be retrieved by calling `DefaultOptions.get('assign')`.
11485
11485
  * - Instance method `clear()`: Sets all the properties of this object to their
11486
11486
  * default values.
@@ -11707,8 +11707,10 @@ function Model(Class, context) {
11707
11707
  * Copies the properties from a specified data object to this object, only
11708
11708
  * copying properties defined in the class of this object.
11709
11709
  *
11710
- * If a property in the data object is `undefined` or `null`, the function
11711
- * sets the property of this object to the default value.
11710
+ * If a property in the data object doesn't exist, the function
11711
+ * sets the property of this object to the default value. However, if a property
11712
+ * exists in the data object but its value is `null` or `undefined`, the function
11713
+ * will preserve that `null` or `undefined` value in this object.
11712
11714
  *
11713
11715
  * Note that the data object may have a different prototype than this object.
11714
11716
  *
@@ -11824,6 +11826,10 @@ function Model(Class, context) {
11824
11826
  * A field is normalizable if and only if it is decorated with the
11825
11827
  * `@{@link Normalizable}` decorator.
11826
11828
  *
11829
+ * If a field value is `null` or `undefined`, the normalization process will
11830
+ * preserve the `null` or `undefined` value rather than replacing it with a
11831
+ * default value.
11832
+ *
11827
11833
  * @param {undefined|string|array} fields
11828
11834
  * the names of fields to be normalized. If this argument is not specified,
11829
11835
  * or `undefined`, or `null`, or a string `'*'`, this function normalizes
@@ -12053,8 +12059,15 @@ function Model(Class, context) {
12053
12059
  * @name Model#toJSON
12054
12060
  * @memberof Model
12055
12061
  */
12056
- Class.prototype.toJSON = function toJSON(key) {
12057
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
12062
+ Class.prototype.toJSON = function toJSON() {
12063
+ var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
12064
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
12065
+ if (_typeof(key) === 'object') {
12066
+ // if the key is an object, it means that the `toJSON()` method is called
12067
+ // directly with the only argument `options`, so the `key` is the `options`
12068
+ options = key;
12069
+ key = '';
12070
+ }
12058
12071
  return toJsonImpl(this, key, _objectSpread$1(_objectSpread$1({}, options), {}, {
12059
12072
  skipRootToJSON: true
12060
12073
  }));
@@ -12112,7 +12125,7 @@ function Model(Class, context) {
12112
12125
  * @memberof Model
12113
12126
  */
12114
12127
  Class.prototype.toJsonString = function toJsonString() {
12115
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
12128
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
12116
12129
  return toJsonStringImpl(Class, this, options);
12117
12130
  };
12118
12131
  }
@@ -12124,8 +12137,10 @@ function Model(Class, context) {
12124
12137
  * It copies the property values from the corresponding properties of the
12125
12138
  * specified data object maintaining the same prototype and class definition.
12126
12139
  *
12127
- * If a property in the data object is `undefined` or `null`, the function
12128
- * sets the property of the created instance to the default value.
12140
+ * If a property doesn't exist in the data object, the function sets the property
12141
+ * of the created instance to the default value. If a property exists in the data
12142
+ * object but its value is `null` or `undefined`, the function preserves the `null`
12143
+ * or `undefined` value.
12129
12144
  *
12130
12145
  * This method is usually used to transform a plain JSON object into the
12131
12146
  * specified domain object.
@@ -12179,6 +12194,11 @@ function Model(Class, context) {
12179
12194
  * copied from the corresponding elements in the data object array,
12180
12195
  * maintaining the same prototype and class definition.
12181
12196
  *
12197
+ * For each element in the array, if a property doesn't exist in the data object,
12198
+ * the function sets the property of the created instance to the default value.
12199
+ * If a property exists in the data object but its value is `null` or `undefined`,
12200
+ * the function preserves the `null` or `undefined` value.
12201
+ *
12182
12202
  * This method is usually used to transform an array of plain JSON objects
12183
12203
  * into an array of specified domain objects.
12184
12204
  *
@@ -12231,6 +12251,11 @@ function Model(Class, context) {
12231
12251
  * list of domain objects obtained from a server using the GET method, and
12232
12252
  * the object should conform to the `Page` class definition.
12233
12253
  *
12254
+ * For each element in the page content array, if a property doesn't exist in
12255
+ * the data object, the function sets the property of the created instance to
12256
+ * the default value. If a property exists in the data object but its value is
12257
+ * `null` or `undefined`, the function preserves the `null` or `undefined` value.
12258
+ *
12234
12259
  * @param {object} page
12235
12260
  * the specified pagination data object, which must conform to the
12236
12261
  * `Page` class definition.
@@ -13297,6 +13322,10 @@ exports.createArray = createArray;
13297
13322
  exports.createPage = createPage;
13298
13323
  exports.defaultNormalizer = defaultNormalizer;
13299
13324
  exports.enumNormalizer = enumNormalizer;
13325
+ exports.getDefaultInstance = getDefaultInstance;
13326
+ exports.getFieldElementType = getFieldElementType;
13327
+ exports.getFieldType = getFieldType;
13328
+ exports.getSourceField = getSourceField;
13300
13329
  exports.isEnumClass = isEnumClass;
13301
13330
  exports.isEnumerator = isEnumerator;
13302
13331
  exports.normalize = normalize;