• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Haixing-Hu / js-common-decorator / 1970a515-3d5d-49ef-bafd-31c9a52cf4e9

10 Nov 2024 05:00PM UTC coverage: 80.83% (-1.5%) from 82.353%
1970a515-3d5d-49ef-bafd-31c9a52cf4e9

push

circleci

Haixing-Hu
feat: make @Normalizable support normalization of enum class field

481 of 608 branches covered (79.11%)

Branch coverage included in aggregate %.

90 of 116 new or added lines in 19 files covered. (77.59%)

20 existing lines in 4 files now uncovered.

649 of 790 relevant lines covered (82.15%)

227.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.33
/src/impl/model/normalize-map-field.js
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
//    Copyright (c) 2022 - 2023.
4
//    Haixing Hu, Qubit Co. Ltd.
5
//
6
//    All rights reserved.
7
//
8
////////////////////////////////////////////////////////////////////////////////
9
import getFieldElementType from '../utils/get-field-element-type';
10

11
/**
12
 * Normalizes the specified map field of the specified object.
13
 *
14
 * @param {function} Class
15
 *     The class of the object to be normalized.
16
 * @param {object} obj
17
 *     The object to be normalized, which must be an instance of the `Class` class.
18
 * @param {string} field
19
 *     The name of the specified field to be normalized. This function assumes
20
 *     that the field exists and is normalizable.
21
 * @param {any} value
22
 *     The value of the specified field of the specified object.
23
 * @param {object} options
24
 *     The optional options for the normalization. Default value is an empty
25
 *     object. Currently, the following options are supported:
26
 *     - `path: string`, the path of the root object of this object.
27
 *       The default value of this option is `''`.
28
 *     - `types: object`, the additional information about types of fields of
29
 *       classes. The keys of this object are the path of the fields or
30
 *       sub-fields of this object, the values are the type of the fields,
31
 *       represented as the constructor function of the type. The path of the
32
 *       root of this object is an empty, therefore the path of the direct field of
33
 *       this object is of the form `'.field'`, and the path of the sub-field of
34
 *       a field is of the form `'.field.subField'`. The default value of this
35
 *       option is `{}`.
36
 *     - `elementTypes: object`, the additional information about types of
37
 *       elements of fields of classes. The keys of this object are the path of
38
 *       the fields or sub-fields of the target object, the values are the type
39
 *       of the elements, represented as the constructor function of the type.
40
 *       The default value of this option is `{}`.
41
 * @param {function} normalizer
42
 *     The normalizer function of the specified field.
43
 * @returns {boolean}
44
 *     If the field value is a built-in map, this function normalizes the field
45
 *     of the specified object by calling the normalizer function on each
46
 *     element in the set, and returns `true`; otherwise, this function does
47
 *     nothing and returns `false`.
48
 * @author Haixing Hu
49
 * @private
50
 */
51
function normalizeMapField(Class, obj, field, value, options, normalizer) {
52
  if (value instanceof Map) {
37!
NEW
53
    const fieldPath = `${options.path}.${field}`;
×
NEW
54
    const elementType = getFieldElementType(Class, field, fieldPath, options);
×
NEW
55
    const context = {
×
56
      type: elementType,
57
      path: fieldPath,
58
      types: options.types,
59
      elementTypes: options.elementTypes,
60
    };
NEW
61
    obj[field] = new Map(Array.from(value, ([k, v]) => [k, normalizer(v, context)]));
×
UNCOV
62
    return true;
×
63
  }
64
  return false;
37✔
65
}
66

67
export default normalizeMapField;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc