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

panates / opra / 29494915195

16 Jul 2026 11:33AM UTC coverage: 83.024% (+0.4%) from 82.653%
29494915195

push

github

erayhanoglu
1.29.1

4118 of 5238 branches covered (78.62%)

Branch coverage included in aggregate %.

34348 of 41093 relevant lines covered (83.59%)

239.47 hits per line

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

85.82
/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';
97✔
10
import { DATATYPE_METADATA, DECORATOR } from '../constants.js';
97✔
11
import { DataType } from './data-type.js';
80✔
12

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

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

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

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

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

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

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

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

2✔
85
/**
2✔
86
 * Type definition of EnumType prototype
2✔
87
 * @interface EnumType
2✔
88
 */
2✔
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
17✔
96
  if (!this) return EnumType[DECORATOR].apply(undefined, args);
17!
97
  // Constructor
17✔
98
  const [owner, initArgs, context] = args as [
17!
99
    DocumentElement,
17✔
100
    EnumType.InitArguments,
17✔
101
    DocumentInitContext | undefined,
17!
102
  ];
×
103
  DataType.call(this, owner, initArgs, context);
×
104
  const _this = asMutable(this);
×
105
  _this.kind = OpraSchema.EnumType.Kind;
×
106
  if (initArgs.base) {
×
107
    // noinspection SuspiciousTypeOfGuard
×
108
    if (!(initArgs.base instanceof EnumType)) {
1✔
109
      throw new TypeError(
1✔
110
        `"${(initArgs.base as DataType).kind}" can't be set as base for a "${_this.kind}"`,
2!
111
      );
2✔
112
    }
2✔
113
    _this.base = initArgs.base;
2✔
114
  }
2✔
115
  _this.instance = initArgs.instance;
2✔
116
  _this.ownAttributes = cloneObject(initArgs.attributes || {});
17!
117
  _this.attributes = _this.base ? cloneObject(_this.base.attributes) : {};
×
118
  for (const [k, el] of Object.entries(_this.ownAttributes)) {
×
119
    _this.attributes[k] = el;
×
120
  }
×
121
} as EnumTypeStatic;
×
122

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

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

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

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

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

1✔
229
EnumType.prototype = EnumTypeClass.prototype;
1✔
230
EnumType[DECORATOR] = EnumTypeFactory;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc