mirror of
https://github.com/hulkholden/n64js.git
synced 2024-06-16 02:57:21 -04:00
Move makeEnum to its own module so it can be reused elsewhere.
This commit is contained in:
parent
33ebe8bbf1
commit
6d10f003b2
21
src/enum.js
Normal file
21
src/enum.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { toString32 } from "./format.js";
|
||||
|
||||
/**
|
||||
* Adds a nameOf function to the provided Object so that we can easily find the
|
||||
* name for a given value. e.g.:
|
||||
* var name = Foo.nameOf(fooValue);
|
||||
* @param {!Object<string, number>} values
|
||||
* @return {!Object<string, number>}
|
||||
*/
|
||||
export function makeEnum(values) {
|
||||
values.nameOf = value => {
|
||||
for (let name in values) {
|
||||
if (Object.prototype.hasOwnProperty.call(values, name) && values[name] === value) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return toString32(value);
|
||||
};
|
||||
|
||||
return Object.freeze(values);
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
import { makeEnum } from '../enum.js';
|
||||
import * as format from '../format.js';
|
||||
|
||||
export const RenderMode = {
|
||||
|
@ -347,26 +348,6 @@ export function getGeometryModeFlagsText(flags, data) {
|
|||
return t.length > 0 ? t.substr(1) : '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a nameOf function to the provided Object so that we can easily find the
|
||||
* name for a given value. e.g.:
|
||||
* var name = gbi.Foo.nameOf(fooValue);
|
||||
* @param {!Object<string, number>} values
|
||||
* @return {!Object<string, number>}
|
||||
*/
|
||||
function makeEnum(values) {
|
||||
values.nameOf = (value) => {
|
||||
for (var name in values) {
|
||||
if (Object.prototype.hasOwnProperty.call(values, name) && values[name] === value) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
return format.toString32(value);
|
||||
};
|
||||
|
||||
return Object.freeze(values);
|
||||
}
|
||||
|
||||
export const ImageFormat = makeEnum({
|
||||
G_IM_FMT_RGBA: 0,
|
||||
G_IM_FMT_YUV: 1,
|
||||
|
|
Loading…
Reference in a new issue