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

oprajs / opra / 19958387866

05 Dec 2025 09:19AM UTC coverage: 80.99% (-0.2%) from 81.23%
19958387866

push

github

erayhanoglu
chore: Updated deps

3534 of 4540 branches covered (77.84%)

Branch coverage included in aggregate %.

31111 of 38237 relevant lines covered (81.36%)

218.64 hits per line

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

88.76
/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 extends Combine<
1✔
21
    {
1✔
22
      kind: OpraSchema.EnumType.Kind;
1✔
23
      base?: EnumObject | EnumArray | string;
1✔
24
      name: string;
1✔
25
    },
1✔
26
    DataType.Metadata,
1✔
27
    OpraSchema.EnumType
1✔
28
  > {}
1✔
29

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

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

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

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

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

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

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

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

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

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

1✔
147
  generateCodec(): Validator {
1✔
148
    return vg.isEnum(Object.keys(this.attributes) as any);
108✔
149
  }
108✔
150

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

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

1✔
174
EnumType.prototype = EnumTypeClass.prototype;
1✔
175
Object.assign(EnumType, EnumTypeClass);
1✔
176

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