Skip to content

Commit 99368e8

Browse files
committed
add bimap.js for Enum
1 parent 38aa228 commit 99368e8

File tree

11 files changed

+424
-154
lines changed

11 files changed

+424
-154
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tls/enum",
3-
"version": "0.6.2",
3+
"version": "0.6.3",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": ["dist/"]

playground/enum.js

Lines changed: 0 additions & 132 deletions
This file was deleted.

playground/enum_2.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Enum } from "../src/enum.js";
2+
3+
class Color extends Enum {
4+
/** @type {Color} Red color constant */
5+
static RED = new Color('RED', '#FF0000');
6+
/** @type {Color} Green color constant */
7+
static GREEN = new Color('GREEN', '#00FF00');
8+
/** @type {Color} Blue color constant */
9+
static BLUE = new Color('BLUE', '#0000FF');
10+
}
11+
12+
const test = Color.fromValue('#00FF00');
13+
//const test_2 = Color.fromValue('#000000')

src/bimap.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// @ts-self-types="../type/bimap.d.ts"
2+
3+
export class BiMap {
4+
constructor() {
5+
this.keyToValue = new Map();
6+
this.valueToKey = new Map();
7+
}
8+
9+
// Add a key-value pair
10+
set(key, value) {
11+
if (this.keyToValue.has(key) || this.valueToKey.has(value)) {
12+
throw new Error("Key or value already exists!");
13+
}
14+
this.keyToValue.set(key, value);
15+
this.valueToKey.set(value, key);
16+
}
17+
18+
// Get a value by key
19+
getValue(key) {
20+
return this.keyToValue.get(key);
21+
}
22+
23+
// Get a key by value
24+
getKey(value) {
25+
return this.valueToKey.get(value);
26+
}
27+
28+
// Check if a key exists
29+
hasKey(key) {
30+
return this.keyToValue.has(key);
31+
}
32+
33+
// Check if a value exists
34+
hasValue(value) {
35+
return this.valueToKey.has(value);
36+
}
37+
38+
// Delete by key
39+
deleteByKey(key) {
40+
const value = this.keyToValue.get(key);
41+
if (value !== undefined) {
42+
this.keyToValue.delete(key);
43+
this.valueToKey.delete(value);
44+
}
45+
}
46+
47+
// Delete by value
48+
deleteByValue(value) {
49+
const key = this.valueToKey.get(value);
50+
if (key !== undefined) {
51+
this.valueToKey.delete(value);
52+
this.keyToValue.delete(key);
53+
}
54+
}
55+
56+
// Clear all entries
57+
clear() {
58+
this.keyToValue.clear();
59+
this.valueToKey.clear();
60+
}
61+
62+
keys(){ return this.keyToValue.keys() }
63+
values(){ return this.keyToValue.values() }
64+
}

src/contentype.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
import { Uint8, Uint16, Struct } from "./dep.ts";
55
import { Enum } from "./enum.js";
6+
import { Handshake } from "./handshaketype.js";
67
import { HandshakeType } from "./handshaketype.js";
78
import { Version } from "./version.js";
9+
import { Alert } from "./alert.js"
810

911
/**
1012
* The higher-level protocol used to process the enclosed
@@ -79,6 +81,16 @@ export class TLSPlaintext extends Uint8Array {
7981
this.version = version;
8082
this.fragment = fragment
8183
this.items = struct.items
84+
85+
this.parseFragment();
86+
}
87+
parseFragment(){
88+
if(this.type == ContentType.HANDSHAKE){
89+
this.handshake = Handshake.from(this.fragment);
90+
}
91+
if(this.type == ContentType.ALERT){
92+
this.alert = Alert.from(this.fragment);
93+
}
8294
}
8395
}
8496

src/enum.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// deno-lint-ignore-file no-slow-types
22
// @ts-self-types="../type/enum.d.ts"
33

4+
import { BiMap } from "./bimap.js";
5+
46
/**
57
* Abstract base class for creating enumeration types in JavaScript
68
* @abstract
@@ -19,7 +21,7 @@
1921
*/
2022
export class Enum {
2123
/** @private @static @type {Map<Function, Map<string, Enum>>} */
22-
static #instances = new Map();
24+
static #instances = new BiMap();
2325

2426
/** @private @static @type {Set<Function>} */
2527
static #frozen = new Set();
@@ -50,27 +52,25 @@ export class Enum {
5052
this.#ordinal = this.constructor.size;
5153

5254
// Register instance
53-
if (!Enum.#instances.has(this.constructor)) {
54-
Enum.#instances.set(this.constructor, new Map());
55+
if (!Enum.#instances.hasKey(this.constructor)) {
56+
Enum.#instances.set(this.constructor, new BiMap());
5557
}
5658

57-
const instanceMap = Enum.#instances.get(this.constructor);
59+
const instanceMap = Enum.#instances.getValue(this.constructor);
5860

5961
if (Enum.#frozen.has(this.constructor)) {
6062
throw new Error(`Cannot add new enum constant ${name} to frozen enum ${this.constructor.name}`);
6163
}
6264

63-
if (instanceMap.has(name)) {
65+
if (instanceMap.hasKey(name)) {
6466
throw new Error(`Duplicate enum constant name: ${name}`);
6567
}
6668

67-
for (const [, instance] of instanceMap) {
68-
if (instance.value === value) {
69-
throw new Error(`Duplicate enum value: ${value} for constant ${name}`);
70-
}
69+
if (instanceMap.hasValue(value)) {
70+
throw new Error(`Duplicate enum value: ${value} for constant ${name}`);
7171
}
7272

73-
instanceMap.set(name, this);
73+
instanceMap.set(name, value);
7474
}
7575

7676
/**
@@ -110,7 +110,7 @@ export class Enum {
110110
* @returns {Enum[]} Array of all enum constants
111111
*/
112112
static values() {
113-
const instanceMap = Enum.#instances.get(this) || new Map();
113+
const instanceMap = Enum.#instances.getValue(this) || new Map();
114114
return Array.from(instanceMap.values());
115115
}
116116

@@ -119,7 +119,7 @@ export class Enum {
119119
* @returns {string[]} Array of all enum constant names
120120
*/
121121
static names() {
122-
const instanceMap = Enum.#instances.get(this) || new Map();
122+
const instanceMap = Enum.#instances.getValue(this) || new Map();
123123
return Array.from(instanceMap.keys());
124124
}
125125

@@ -130,11 +130,11 @@ export class Enum {
130130
* @throws {Error} If no constant exists with the given name
131131
*/
132132
static valueOf(name) {
133-
const instanceMap = Enum.#instances.get(this);
134-
if (!instanceMap || !instanceMap.has(name)) {
133+
const instanceMap = Enum.#instances.getValue(this);
134+
if (!instanceMap || !instanceMap.hasKey(name)) {
135135
throw new Error(`No enum constant ${this.name}.${name}`);
136136
}
137-
return instanceMap.get(name);
137+
return instanceMap.getValue(name);
138138
}
139139

140140
/**
@@ -144,11 +144,13 @@ export class Enum {
144144
* @throws {Error} If no constant exists with the given value
145145
*/
146146
static fromValue(value) {
147-
const instance = this.values().find(inst => inst.value === value);
148-
if (!instance) {
149-
throw new Error(`No enum constant with value ${value} in ${this.name}`);
147+
//const instance = this.values().find(inst => inst.value === value);
148+
const biMap = Enum.#instances.getValue(this);
149+
if(!biMap.hasValue(value)){
150+
throw new Error(`No enum constant with value ${value} in ${this.name}`);
150151
}
151-
return instance;
152+
const key = biMap.getKey(value);
153+
return this[key]
152154
}
153155

154156
/**
@@ -230,4 +232,6 @@ export class Enum {
230232
}
231233
}
232234

235+
236+
233237
// npx -p typescript tsc ./src/enum.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist

0 commit comments

Comments
 (0)