• 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

75.0
/src/impl/utils/get-field-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_TYPE } from '../metadata-keys';
11
import getDefaultInstance from './get-default-instance';
12
import getFieldMetadata from './get-field-metadata';
13

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

70
export default getFieldType;
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