-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
enum.ts
188 lines (167 loc) · 5.69 KB
/
enum.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @category core/value-types
*/
import { value } from '../utils/js';
import { EDITOR, TEST, DEV } from 'internal:constants';
import { legacyCC } from '../global-exports';
import { errorID } from '../platform/debug';
import { assertIsTrue } from '../data/utils/asserts';
/**
* @en
* Define an enum type. <br/>
* If a enum item has a value of -1, it will be given an Integer number according to it's order in the list.<br/>
* Otherwise it will use the value specified by user who writes the enum definition.
*
* @zh
* 定义一个枚举类型。<br/>
* 用户可以把枚举值设为任意的整数,如果设为 -1,系统将会分配为上一个枚举值 + 1。
*
* @param obj - a JavaScript literal object containing enum names and values, or a TypeScript enum type
* @return the defined enum type
*/
export function Enum<T> (obj: T): T {
if ('__enums__' in obj) {
return obj;
}
value(obj, '__enums__', null, true);
return Enum.update(obj);
}
/**
* @en
* Update the enum object properties.
* @zh
* 更新枚举对象的属性列表。
* @param obj
*/
Enum.update = <T> (obj: T): T => {
let lastIndex: number = -1;
const keys: string[] = Object.keys(obj);
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
let val = obj[key];
if (val === -1) {
val = ++lastIndex;
obj[key] = val;
}
else {
if (typeof val === 'number') {
lastIndex = val;
}
else if (typeof val === 'string' && Number.isInteger(parseFloat(key))) {
continue;
}
}
const reverseKey: string = '' + val;
if (key !== reverseKey) {
if ((EDITOR || TEST) && reverseKey in obj && obj[reverseKey] !== key) {
errorID(7100, reverseKey);
continue;
}
value(obj, reverseKey, key);
}
}
return obj;
}
namespace Enum {
export interface Enumerator<EnumT> {
/**
* The name of the enumerator.
*/
name: keyof EnumT;
/**
* The value of the numerator.
*/
value: EnumT[typeof name];
}
}
interface EnumExtras<EnumT> {
__enums__: null | Enum.Enumerator<EnumT>[];
}
/**
* Determines if the object is an enum type.
* @param enumType The object to judge.
*/
Enum.isEnum = <EnumT extends {}>(enumType: EnumT) => {
return enumType && enumType.hasOwnProperty('__enums__');
};
function assertIsEnum <EnumT extends {}>(enumType: EnumT): asserts enumType is EnumT & EnumExtras<EnumT> {
assertIsTrue(enumType.hasOwnProperty('__enums__'));
}
/**
* Get the enumerators from the enum type.
* @param enumType An enum type.
*/
Enum.getList = <EnumT extends {}>(enumType: EnumT): readonly Enum.Enumerator<EnumT>[] => {
assertIsEnum(enumType);
if (enumType.__enums__) {
return enumType.__enums__;
}
return Enum.updateList(enumType as EnumT);
};
/**
* Update the enumerators from the enum type.
* @param enumType - the enum type defined from cc.Enum
* @return {Object[]}
*/
Enum.updateList = <EnumT extends {}>(enumType: EnumT): readonly Enum.Enumerator<EnumT>[] => {
assertIsEnum(enumType);
const enums: any[] = enumType.__enums__ || [];
enums.length = 0;
// tslint:disable-next-line: forin
for (const name in enumType) {
const v = enumType[name];
if (Number.isInteger(v)) {
enums.push({ name, value: v });
}
}
enums.sort((a, b) => a.value - b.value);
enumType.__enums__ = enums;
return enums;
};
if (DEV) {
// check key order in object literal
const _TestEnum = Enum({
ZERO: -1,
ONE: -1,
TWO: -1,
THREE: -1,
});
if (_TestEnum.ZERO !== 0 || _TestEnum.ONE !== 1 || _TestEnum.THREE !== 3) {
errorID(7101);
}
}
/**
* Make the enum type `enumType` as enumeration so that Creator may identify, operate on it.
* Formally, as a result of invocation on this function with enum type `enumType`:
* - `Enum.isEnum(enumType)` returns `true`;
* - `Enum.getList(enumType)` returns the enumerators of `enumType`.
* @param enumType An enum type, eg, a kind of type with similar semantic defined by TypeScript.
*/
export function ccenum<EnumT extends {}> (enumType: EnumT) {
if (!('__enums__' in enumType)) {
value(enumType, '__enums__', null, true);
}
}
legacyCC.Enum = Enum;