• 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

82.93
/src/impl/utils/get-field-element-type.js
1
////////////////////////////////////////////////////////////////////////////////
2
//
3
//    Copyright (c) 2022 - 2024.
4
//    Haixing Hu, Qubit Co. Ltd.
5
//
6
//    All rights reserved.
7
//
8
////////////////////////////////////////////////////////////////////////////////
9
import classMetadataCache from '../class-metadata-cache';
10
import { KEY_FIELD_ELEMENT_TYPE } from '../metadata-keys';
11
import getCollectionElementType from './get-collection-element-type';
12
import getDefaultInstance from './get-default-instance';
13
import getFieldMetadata from './get-field-metadata';
14

15
/**
16
 * Gets the element type of a field of an object.
17
 *
18
 * The function will first check the annotated element type information of the
19
 * specified field of the object. If the annotated type information is found, the
20
 * function will return it. Otherwise, the function will check the additional
21
 * type information provided by the options. If the additional type information
22
 * is found, the function will return it. Otherwise, the function will return
23
 * `null`.
24
 *
25
 * @param {function} Class
26
 *     The constructor of the class of the object.
27
 * @param {string} field
28
 *     the name of the field.
29
 * @param {string} path
30
 *     the path of the field in the property tree of the original root object.
31
 * @param {object} options
32
 *     the additional options for the assignment.
33
 * @return {function|null}
34
 *     the element type of the specified field of the object, or `null` if the
35
 *     field element type cannot be inferred.
36
 * @private
37
 * @author Haixing Hu
38
 */
39
function getFieldElementType(Class, field, path = undefined, options = {}) {
10✔
40
  path ??= `.${field}`;
1,128✔
41
  options ??= {};
1,128✔
42
  // try to find the annotated element type information
43
  const metadata = classMetadataCache.get(Class);
1,128✔
44
  const annotatedElementType = getFieldMetadata(metadata, field, KEY_FIELD_ELEMENT_TYPE);
1,128✔
45
  if (annotatedElementType) {
1,128✔
46
    if (typeof annotatedElementType !== 'function') {
31!
NEW
47
      throw new TypeError(`The annotated element type of '${Class.name}.${path}' is not a function.`);
×
48
    }
49
    return annotatedElementType;
31✔
50
  }
51
  // try to find the additional type information provided by the options
52
  if (options && options.elementTypes && options.elementTypes[path]) {
1,097✔
53
    const result = options.elementTypes[path];
7✔
54
    if (typeof result !== 'function') {
7!
NEW
55
      throw new TypeError(`The target element type of '${Class.name}.${path}' is not a function.`);
×
56
    }
57
    return result;
7✔
58
  }
59
  // try to find the type information from the default field value
60
  const defaultInstance = getDefaultInstance(Class);
1,090✔
61
  const defaultFieldValue = defaultInstance[field];
1,090✔
62
  if ((defaultFieldValue !== undefined) && (defaultFieldValue !== null)) {
1,090✔
63
    const elementType = getCollectionElementType(defaultFieldValue);
981✔
64
    if (elementType !== null && elementType !== undefined) {
981!
NEW
65
      return elementType;
×
66
    }
67
  }
68
  // otherwise returns null
69
  return null;
1,090✔
70
}
71

72
export default getFieldElementType;
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