2020-06-09 06:38:56 +03:00
|
|
|
/*
|
2021-04-23 05:07:38 +03:00
|
|
|
Copyright 2020, 2021 The Matrix.org Foundation C.I.C.
|
2020-06-09 06:38:56 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the values for an enum.
|
|
|
|
* @param e The enum.
|
|
|
|
* @returns The enum values.
|
|
|
|
*/
|
2021-04-23 05:07:38 +03:00
|
|
|
export function getEnumValues(e: any): (string | number)[] {
|
|
|
|
// String-based enums will simply be objects ({Key: "value"}), but number-based
|
|
|
|
// enums will instead map themselves twice: in one direction for {Key: 12} and
|
|
|
|
// the reverse for easy lookup, presumably ({12: Key}). In the reverse mapping,
|
|
|
|
// the key is a string, not a number.
|
|
|
|
//
|
|
|
|
// For this reason, we try to determine what kind of enum we're dealing with.
|
|
|
|
|
2020-06-09 06:38:56 +03:00
|
|
|
const keys = Object.keys(e);
|
2021-04-23 05:07:38 +03:00
|
|
|
const values: (string | number)[] = [];
|
|
|
|
for (const key of keys) {
|
|
|
|
const value = e[key];
|
|
|
|
if (Number.isFinite(value) || e[value.toString()] !== Number(key)) {
|
|
|
|
values.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return values;
|
2020-06-09 06:38:56 +03:00
|
|
|
}
|
2020-07-21 01:51:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a given value is a valid value for the provided enum.
|
|
|
|
* @param e The enum to check against.
|
|
|
|
* @param val The value to search for.
|
|
|
|
* @returns True if the enum contains the value.
|
|
|
|
*/
|
|
|
|
export function isEnumValue<T>(e: T, val: string | number): boolean {
|
|
|
|
return getEnumValues(e).includes(val);
|
|
|
|
}
|