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

oprajs / opra / 14419802659

12 Apr 2025 12:55PM UTC coverage: 82.088% (+4.2%) from 77.908%
14419802659

push

github

web-flow
Merge pull request #27 from oprajs/dev

Dev

3458 of 4432 branches covered (78.02%)

Branch coverage included in aggregate %.

16 of 16 new or added lines in 10 files covered. (100.0%)

1793 existing lines in 129 files now uncovered.

29424 of 35625 relevant lines covered (82.59%)

183.2 hits per line

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

89.02
/packages/common/src/document/data-type/enum-type.ts
1
import 'reflect-metadata';
1✔
2
import { omitUndefined } from '@jsopen/objects';
1✔
3
import { asMutable, type Combine, type Type } from 'ts-gems';
1✔
4
import { type Validator, vg } from 'valgen';
1✔
5
import { cloneObject } from '../../helpers/index.js';
1✔
6
import { OpraSchema } from '../../schema/index.js';
1✔
7
import type { ApiDocument } from '../api-document.js';
1✔
8
import type { DocumentElement } from '../common/document-element.js';
1✔
9
import { DocumentInitContext } from '../common/document-init-context.js';
1✔
10
import { DATATYPE_METADATA, DECORATOR } from '../constants.js';
1✔
11
import { DataType } from './data-type.js';
1✔
12

1✔
13
/**
1✔
14
 * @namespace EnumType
1✔
15
 */
1✔
16
export namespace EnumType {
1✔
17
  export type EnumObject = Record<string, string | number>;
1✔
18
  export type EnumArray = readonly string[];
1✔
19

1✔
20
  export interface Metadata
1✔
21
    extends Combine<
1✔
22
      {
1✔
23
        kind: OpraSchema.EnumType.Kind;
1✔
24
        base?: EnumObject | EnumArray | string;
1✔
25
        name: string;
1✔
26
      },
1✔
27
      DataType.Metadata,
1✔
28
      OpraSchema.EnumType
1✔
29
    > {}
1✔
30

1✔
31
  export interface Options<
1✔
32
    T,
1✔
33
    Keys extends string | number | symbol = T extends readonly any[]
1✔
34
      ? T[number]
1✔
35
      : keyof T,
1✔
36
  > extends Combine<
1✔
37
      {
1✔
38
        base?: EnumObject | EnumArray | string;
1✔
39
        meanings?: Record<Keys, string>;
1✔
40
      },
1✔
41
      DataType.Options
1✔
42
    > {}
1✔
43

1✔
44
  export interface InitArguments
1✔
45
    extends Combine<
1✔
46
      {
1✔
47
        kind: OpraSchema.EnumType.Kind;
1✔
48
        base?: EnumType;
1✔
49
        instance?: object;
1✔
50
      },
1✔
51
      DataType.InitArguments,
1✔
52
      EnumType.Metadata
1✔
53
    > {}
1✔
54
}
1✔
55

1✔
56
/**
1✔
57
 * Mixin type definition of class constructor and helper method for EnumType
1✔
58
 */
1✔
59
export interface EnumTypeStatic {
1✔
60
  /**
1✔
61
   * Class constructor of EnumType
1✔
62
   *
1✔
63
   * @param owner
1✔
64
   * @param args
1✔
65
   * @constructor
1✔
66
   */
1✔
67
  new (owner: DocumentElement, args?: EnumType.InitArguments): EnumType;
1✔
68

1✔
69
  <T extends EnumType.EnumObject, B extends EnumType.EnumObject>(
1✔
70
    enumSource: T,
1✔
71
    base?: B,
1✔
72
    options?: EnumType.Options<T>,
1✔
73
  ): B & T;
1✔
74

1✔
75
  <T extends EnumType.EnumArray, B extends EnumType.EnumArray>(
1✔
76
    enumSource: T,
1✔
77
    base?: B,
1✔
78
    options?: EnumType.Options<T>,
1✔
79
  ): B & T;
1✔
80

1✔
81
  <T extends EnumType.EnumObject | EnumType.EnumArray>(
1✔
82
    target: T,
1✔
83
    options?: EnumType.Options<T>,
1✔
84
  ): T;
1✔
85
}
1✔
86

1✔
87
/**
1✔
88
 * Type definition of EnumType prototype
1✔
89
 * @interface EnumType
1✔
90
 */
1✔
91
export interface EnumType extends EnumTypeClass {}
1✔
92

1✔
93
/**
1✔
94
 * @class EnumType
1✔
95
 */
1✔
96
export const EnumType = function (this: EnumType | void, ...args: any[]) {
1✔
97
  // Injector
74✔
98
  if (!this) return EnumType[DECORATOR].apply(undefined, args);
74✔
99
  // Constructor
59✔
100
  const [owner, initArgs, context] = args as [
59✔
101
    DocumentElement,
59✔
102
    EnumType.InitArguments,
59✔
103
    DocumentInitContext | undefined,
59✔
104
  ];
59✔
105
  DataType.call(this, owner, initArgs, context);
59✔
106
  const _this = asMutable(this);
59✔
107
  _this.kind = OpraSchema.EnumType.Kind;
59✔
108
  if (initArgs.base) {
74✔
109
    // noinspection SuspiciousTypeOfGuard
2✔
110
    if (!(initArgs.base instanceof EnumType)) {
2!
111
      throw new TypeError(
×
UNCOV
112
        `"${(initArgs.base as DataType).kind}" can't be set as base for a "${_this.kind}"`,
×
UNCOV
113
      );
×
UNCOV
114
    }
×
115
    _this.base = initArgs.base;
2✔
116
  }
2✔
117
  _this.instance = initArgs.instance;
59✔
118
  _this.ownAttributes = cloneObject(initArgs.attributes || {});
74!
119
  _this.attributes = _this.base ? cloneObject(_this.base.attributes) : {};
74✔
120
  for (const [k, el] of Object.entries(_this.ownAttributes)) {
74✔
121
    _this.attributes[k] = el;
388✔
122
  }
388✔
123
} as EnumTypeStatic;
59✔
124

1✔
125
/**
1✔
126
 * @class EnumType
1✔
127
 */
1✔
128
class EnumTypeClass extends DataType {
1✔
129
  declare readonly kind: OpraSchema.EnumType.Kind;
1✔
130
  declare readonly base?: EnumType;
1✔
131
  declare readonly instance?: object;
1✔
132
  declare readonly attributes: Record<
1✔
133
    string | number,
1✔
134
    OpraSchema.EnumType.ValueInfo
1✔
135
  >;
1✔
136
  declare readonly ownAttributes: Record<
1✔
137
    string | number,
1✔
138
    OpraSchema.EnumType.ValueInfo
1✔
139
  >;
1✔
140

1✔
141
  extendsFrom(baseType: DataType | string | Type | object): boolean {
1✔
142
    if (!(baseType instanceof DataType))
×
143
      baseType = this.node.getDataType(baseType);
×
144
    if (!(baseType instanceof EnumType)) return false;
×
145
    if (baseType === this) return true;
×
146
    return !!this.base?.extendsFrom(baseType);
×
UNCOV
147
  }
×
148

1✔
149
  generateCodec(): Validator {
1✔
150
    return vg.isEnum(Object.keys(this.attributes) as any);
107✔
151
  }
107✔
152

1✔
153
  toJSON(options?: ApiDocument.ExportOptions): OpraSchema.EnumType {
1✔
154
    const superJson = super.toJSON(options);
64✔
155
    const baseName = this.base
64✔
156
      ? this.node.getDataTypeNameWithNs(this.base)
2✔
157
      : undefined;
62✔
158
    return omitUndefined<OpraSchema.EnumType>({
64✔
159
      ...superJson,
64✔
160
      kind: this.kind,
64✔
161
      base: baseName,
64✔
162
      attributes: cloneObject(this.ownAttributes),
64✔
163
    });
64✔
164
  }
64✔
165

1✔
166
  protected _locateBase(
1✔
167
    callback: (base: EnumType) => boolean,
66✔
168
  ): EnumType | undefined {
66✔
169
    if (!this.base) return;
66✔
170
    if (callback(this.base)) return this.base;
66!
171
    if ((this.base as any)._locateBase)
2✔
172
      return (this.base as any)._locateBase(callback);
2✔
173
  }
66✔
174
}
1✔
175

1✔
176
EnumType.prototype = EnumTypeClass.prototype;
1✔
177
Object.assign(EnumType, EnumTypeClass);
1✔
178

1✔
179
/**
1✔
180
 *
1✔
181
 */
1✔
182
function EnumTypeFactory(enumSource: object, ...args: any[]) {
15✔
183
  const base = args.length >= 2 ? args[0] : undefined;
15!
184
  const options = args.length >= 2 ? args[1] : args[0];
15!
185
  let attributes: Record<string | number, OpraSchema.EnumType.ValueInfo> = {};
15✔
186
  let out = enumSource;
15✔
187
  if (Array.isArray(enumSource)) {
15✔
188
    if (base) {
1!
189
      if (!Array.isArray(base))
×
190
        throw new TypeError(
×
UNCOV
191
          'Both "target" and "base" arguments should be array',
×
UNCOV
192
        );
×
193
      out = [...base, ...enumSource];
×
UNCOV
194
    }
×
195
    attributes = {};
1✔
196
    enumSource.forEach(k => {
1✔
197
      const description = options?.meanings?.[k];
2✔
198
      attributes[k] = omitUndefined({ description });
2✔
199
    });
2✔
200
  } else {
15✔
201
    if (base) {
14!
202
      if (Array.isArray(base))
×
203
        throw new TypeError(
×
UNCOV
204
          'Both "target" and "base" arguments should be enum object',
×
UNCOV
205
        );
×
206
      out = { ...base, ...enumSource };
×
UNCOV
207
    }
×
208
    Object.keys(enumSource).forEach(k => {
14✔
209
      const description = options?.meanings?.[k];
72✔
210
      attributes[enumSource[k]] = omitUndefined({ alias: k, description });
72✔
211
    });
72✔
212
  }
14✔
213
  const metadata: EnumType.Metadata = {
15✔
214
    kind: OpraSchema.EnumType.Kind,
15✔
215
    attributes,
15✔
216
    base: options?.base,
15✔
217
    name: options?.name,
15✔
218
    description: options?.description,
15✔
219
  };
15✔
220
  Object.defineProperty(enumSource, DATATYPE_METADATA, {
15✔
221
    value: metadata,
15✔
222
    enumerable: false,
15✔
223
    configurable: true,
15✔
224
    writable: true,
15✔
225
  });
15✔
226
  return out;
15✔
227
}
15✔
228

1✔
229
EnumType.prototype = EnumTypeClass.prototype;
1✔
230
EnumType[DECORATOR] = EnumTypeFactory;
1✔
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