Skip to content

Commit 9d2d2a7

Browse files
minht11Borewit
authored andcommitted
Refactor enums to mapped objects
1 parent 46111e2 commit 9d2d2a7

File tree

17 files changed

+176
-130
lines changed

17 files changed

+176
-130
lines changed

lib/apev2/APEv2Token.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ export interface IFooter {
8383
flags: ITagFlags // ToDo: what is this???
8484
}
8585

86-
export enum DataType {
87-
text_utf8 = 0,
88-
binary = 1,
89-
external_info = 2,
90-
reserved = 3
86+
export const DataType = {
87+
text_utf8: 0,
88+
binary: 1,
89+
external_info: 2,
90+
reserved: 3
9191
}
92+
export type DataType = typeof DataType[keyof typeof DataType]
9293

9394
/**
9495
* APE_DESCRIPTOR: defines the sizes (and offsets) of all the pieces, as well as the MD5 checksum

lib/asf/AsfObject.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,33 @@ export class AsfContentParseError extends makeUnexpectedFileContentError('ASF'){
1616
/**
1717
* Data Type: Specifies the type of information being stored. The following values are recognized.
1818
*/
19-
export enum DataType {
19+
export const DataType = {
2020
/**
2121
* Unicode string. The data consists of a sequence of Unicode characters.
2222
*/
23-
UnicodeString = 0,
23+
UnicodeString: 0,
2424
/**
2525
* BYTE array. The type of data is implementation-specific.
2626
*/
27-
ByteArray = 1,
27+
ByteArray: 1,
2828
/**
2929
* BOOL. The data is 2 bytes long and should be interpreted as a 16-bit unsigned integer. Only 0x0000 or 0x0001 are permitted values.
3030
*/
31-
Bool = 2,
31+
Bool: 2,
3232
/**
3333
* DWORD. The data is 4 bytes long and should be interpreted as a 32-bit unsigned integer.
3434
*/
35-
DWord = 3,
35+
DWord: 3,
3636
/**
3737
* QWORD. The data is 8 bytes long and should be interpreted as a 64-bit unsigned integer.
3838
*/
39-
QWord = 4,
39+
QWord: 4,
4040
/**
4141
* WORD. The data is 2 bytes long and should be interpreted as a 16-bit unsigned integer.
4242
*/
43-
Word = 5
43+
Word: 5
4444
}
45+
export type DataType = typeof DataType[keyof typeof DataType]
4546

4647
/**
4748
* Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ee663575
@@ -607,7 +608,7 @@ export class WmPictureToken implements IGetToken<IWmPicture> {
607608
const description = new Token.StringType(index - 5, 'utf-16le').get(buffer, 5);
608609

609610
return {
610-
type: AttachedPictureType[typeId],
611+
type: AttachedPictureType[typeId as keyof typeof AttachedPictureType],
611612
format,
612613
description,
613614
size,

lib/common/MetadataCollector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
type FormatId,
33
type IAudioMetadata, type ICommonTagsResult,
44
type IFormat,
5-
type INativeTags, type IOptions, type IQualityInformation, type IPicture, type ITrackInfo, TrackType, type IComment, type AnyTagValue
5+
type INativeTags, type IOptions, type IQualityInformation, type IPicture, type ITrackInfo, TrackTypeValueToKeyMap, type IComment, type AnyTagValue
66
} from '../type.js';
77

88
import initDebug from 'debug';
@@ -109,7 +109,7 @@ export class MetadataCollector implements INativeMetadataCollector {
109109
}
110110

111111
public addStreamInfo(streamInfo: ITrackInfo) {
112-
debug(`streamInfo: type=${streamInfo.type ? TrackType[streamInfo.type] : '?'}, codec=${streamInfo.codecName}`);
112+
debug(`streamInfo: type=${streamInfo.type ? TrackTypeValueToKeyMap[streamInfo.type] : '?'}, codec=${streamInfo.codecName}`);
113113
this.format.trackInfo.push(streamInfo);
114114
}
115115

lib/dsf/DsfChunk.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ export const DsdChunk: IGetToken<IDsdChunk> = {
6060
};
6161
}
6262
};
63-
64-
export enum ChannelType {
65-
mono = 1,
66-
stereo = 2,
67-
channels = 3,
68-
quad = 4,
69-
'4 channels' = 5,
70-
'5 channels' = 6,
71-
'5.1 channels' = 7
63+
export const ChannelType = {
64+
mono: 1,
65+
stereo: 2,
66+
channels: 3,
67+
quad: 4,
68+
'4 channels': 5,
69+
'5 channels': 6,
70+
'5.1 channels': 7
7271
}
72+
export type ChannelType = typeof ChannelType[keyof typeof ChannelType];
7373

7474
/**
7575
* Interface to format chunk payload chunk

lib/ebml/EbmlIterator.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ export interface ILinkedElementType extends IElementType {
1818
readonly container?: { [id: number]: ILinkedElementType; };
1919
}
2020

21-
export enum ParseAction {
22-
ReadNext = 0, // Continue reading the next elements
23-
IgnoreElement = 2, // Ignore (do not read) this element
24-
SkipSiblings = 3, // Skip all remaining elements at the same level
25-
TerminateParsing = 4, // Terminate the parsing process
26-
SkipElement = 5 // Consider the element has read, assume position is at the next element
27-
}
21+
export const ParseAction = {
22+
ReadNext: 0, // Continue reading the next elements
23+
IgnoreElement: 2, // Ignore (do not read) this element
24+
SkipSiblings: 3, // Skip all remaining elements at the same level
25+
TerminateParsing: 4, // Terminate the parsing process
26+
SkipElement: 5 // Consider the element has read, assume position is at the next element
27+
} as const;
28+
29+
export type ParseAction = typeof ParseAction[keyof typeof ParseAction];
2830

2931
/**
3032
* @return true, to quit the parser

lib/ebml/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
export interface ITree { [name: string]: string | number | boolean | Uint8Array | ITree | ITree[]; }
22

3-
export enum DataType { 'string' = 0, uint = 1, uid = 2, bool = 3, binary = 4, float = 5}
3+
export const DataType = {
4+
string: 0,
5+
uint: 1,
6+
uid: 2,
7+
bool: 3,
8+
binary: 4,
9+
float: 5,
10+
} as const;
11+
export type DataType = typeof DataType[keyof typeof DataType];
412

513
export type ValueType = string | number | Uint8Array | boolean | ITree | ITree[];
614

lib/flac/FlacParser.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ class FlacContentError extends makeUnexpectedFileContentError('FLAC'){
1919
* FLAC supports up to 128 kinds of metadata blocks; currently the following are defined:
2020
* ref: https://xiph.org/flac/format.html#metadata_block
2121
*/
22-
enum BlockType {
23-
STREAMINFO = 0,
24-
PADDING = 1,
25-
APPLICATION = 2,
26-
SEEKTABLE = 3,
27-
VORBIS_COMMENT = 4,
28-
CUESHEET = 5,
29-
PICTURE = 6
30-
}
22+
const BlockType = {
23+
STREAMINFO: 0, // STREAMINFO
24+
PADDING: 1, // PADDING
25+
APPLICATION: 2, // APPLICATION
26+
SEEKTABLE: 3, // SEEKTABLE
27+
VORBIS_COMMENT: 4, // VORBIS_COMMENT
28+
CUESHEET: 5, // CUESHEET
29+
PICTURE: 6 // PICTURE
30+
};
31+
type BlockType = typeof BlockType[keyof typeof BlockType];
3132

3233
export class FlacParser extends AbstractID3Parser {
3334

lib/id3v2/FrameParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export class FrameParser {
210210

211211
pic.format = FrameParser.fixPictureMimeType(pic.format);
212212

213-
pic.type = AttachedPictureType[uint8Array[offset]];
213+
pic.type = AttachedPictureType[uint8Array[offset] as keyof typeof AttachedPictureType];
214214
offset += 1;
215215

216216
fzero = util.findZero(uint8Array, offset, length, encoding);

lib/id3v2/ID3v2Token.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ import * as util from '../common/Util.js';
77
* The picture type according to the ID3v2 APIC frame
88
* Ref: http://id3.org/id3v2.3.0#Attached_picture
99
*/
10-
export enum AttachedPictureType {
11-
'Other' = 0,
12-
"32x32 pixels 'file icon' (PNG only)" = 1,
13-
'Other file icon' = 2,
14-
'Cover (front)' = 3,
15-
'Cover (back)' = 4,
16-
'Leaflet page' = 5,
17-
'Media (e.g. label side of CD)' = 6,
18-
'Lead artist/lead performer/soloist' = 7,
19-
'Artist/performer' = 8,
20-
'Conductor' = 9,
21-
'Band/Orchestra' = 10,
22-
'Composer' = 11,
23-
'Lyricist/text writer' = 12,
24-
'Recording Location' = 13,
25-
'During recording' = 14,
26-
'During performance' = 15,
27-
'Movie/video screen capture' = 16,
28-
'A bright coloured fish' = 17,
29-
'Illustration' = 18,
30-
'Band/artist logotype' = 19,
31-
'Publisher/Studio logotype' = 20
10+
export const AttachedPictureType = {
11+
0: 'Other',
12+
1: "32x32 pixels 'file icon' (PNG only)",
13+
2: 'Other file icon',
14+
3: 'Cover (front)',
15+
4: 'Cover (back)',
16+
5: 'Leaflet page',
17+
6: 'Media (e.g. label side of CD)',
18+
7: 'Lead artist/lead performer/soloist',
19+
8: 'Artist/performer',
20+
9: 'Conductor',
21+
10: 'Band/Orchestra',
22+
11: 'Composer',
23+
12: 'Lyricist/text writer',
24+
13: 'Recording Location',
25+
14: 'During recording',
26+
15: 'During performance',
27+
16: 'Movie/video screen capture',
28+
17: 'A bright coloured fish',
29+
18: 'Illustration',
30+
19: 'Band/artist logotype',
31+
20: 'Publisher/Studio logotype'
3232
}
3333

3434
export type ID3v2MajorVersion = 2 | 3 | 4;
@@ -46,21 +46,23 @@ export interface IExtendedHeader {
4646
/**
4747
* https://id3.org/id3v2.3.0#Synchronised_lyrics.2Ftext
4848
*/
49-
export enum LyricsContentType {
50-
other = 0,
51-
lyrics = 1,
52-
text = 2,
53-
movement_part = 3,
54-
events = 4,
55-
chord = 5,
56-
trivia_pop = 6
57-
}
49+
export const LyricsContentType = {
50+
other: 0,
51+
lyrics: 1,
52+
text: 2,
53+
movement_part: 3,
54+
events: 4,
55+
chord: 5,
56+
trivia_pop: 6,
57+
};
58+
export type LyricsContentType = typeof LyricsContentType[keyof typeof LyricsContentType];
5859

59-
export enum TimestampFormat {
60-
notSynchronized0 = 0,
61-
mpegFrameNumber = 1,
62-
milliseconds = 2
63-
}
60+
export const TimestampFormat = {
61+
notSynchronized0: 0,
62+
mpegFrameNumber: 1,
63+
milliseconds: 2
64+
};
65+
export type TimestampFormat = typeof TimestampFormat[keyof typeof TimestampFormat];
6466

6567
/**
6668
* 28 bits (representing up to 256MB) integer, the msb is 0 to avoid 'false syncsignals'.

lib/matroska/types.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,43 @@ export interface ISimpleTag {
9191
default?: boolean;
9292
}
9393

94-
export enum TargetType {
95-
shot = 10,
96-
scene = 20,
97-
track = 30,
98-
part = 40,
99-
album = 50,
100-
edition = 60,
101-
collection = 70
94+
export const TargetType = {
95+
10: 'shot',
96+
20: 'scene',
97+
30: 'track',
98+
40: 'part',
99+
50: 'album',
100+
60: 'edition',
101+
70: 'collection'
102+
}
103+
104+
export const TrackType = {
105+
video: 0x01,
106+
audio: 0x02,
107+
complex: 0x03,
108+
logo: 0x04,
109+
subtitle: 0x11,
110+
button: 0x12,
111+
control: 0x20
112+
};
113+
export type TrackType = typeof TrackType[keyof typeof TrackType];
114+
export type TrackTypeKey = keyof typeof TrackType;
115+
116+
export const TrackTypeValueToKeyMap: Record<TrackType, TrackTypeKey> = {
117+
[TrackType.video]: 'video',
118+
[TrackType.audio]: 'audio',
119+
[TrackType.complex]: 'complex',
120+
[TrackType.logo]: 'logo',
121+
[TrackType.subtitle]: 'subtitle',
122+
[TrackType.button]: 'button',
123+
[TrackType.control]: 'control'
102124
}
103125

104-
export enum TrackType {
105-
video = 0x01,
106-
audio = 0x02,
107-
complex = 0x03,
108-
logo = 0x04,
109-
subtitle= 0x11,
110-
button = 0x12,
111-
control = 0x20
112-
}
113-
114-
export type TrackTypeKey = keyof TrackType;
115-
116126
export interface ITarget {
117127
trackUID?: Uint8Array;
118128
chapterUID?: Uint8Array;
119129
attachmentUID?: Uint8Array;
120-
targetTypeValue?: TargetType;
130+
targetTypeValue?: keyof typeof TargetType;
121131
targetType?: string;
122132
}
123133

0 commit comments

Comments
 (0)