Skip to content

Commit 69a5355

Browse files
committed
revise .gitignore add ./
1 parent 953fcb3 commit 69a5355

21 files changed

+804
-24
lines changed

.gitignore

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.vscode/
2-
deno.lock
3-
dist/
4-
playground/
5-
commit.sh
6-
version.js
7-
.gitignore
1+
./.vscode/
2+
./deno.lock
3+
./dist/
4+
./playground/
5+
./commit.sh
6+
./version.js
7+
./.gitignore

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"request": "launch",
9+
"name": "Launch Program",
10+
"type": "node",
11+
"program": "${file}",
12+
"cwd": "${workspaceFolder}",
13+
"env": {},
14+
"runtimeExecutable": "C:\\Users\\ASUS\\.deno\\bin\\deno.EXE",
15+
"runtimeArgs": [
16+
"run",
17+
"--unstable",
18+
"--inspect-wait",
19+
"--allow-all"
20+
],
21+
"attachSimplePort": 9229
22+
}
23+
]
24+
}

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.9.9",
3+
"version": "0.10.0",
44
"exports": "./src/mod.ts",
55
"publish": {
66
"exclude": [

deno.lock

Lines changed: 405 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

playground/chiper_.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Cipher } from "../src/cipher.js";
2+
3+
const aesgcm = Cipher.AES_128_GCM_SHA256;
4+
5+
const isAesGcm = aesgcm.name.includes("aes")&&aesgcm.name.includes("gcm");
6+

playground/enum_old.js

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// deno-lint-ignore-file no-slow-types
2+
// @ts-self-types="../type/enum.d.ts"
3+
4+
/**
5+
* Abstract base class for creating enumeration types in JavaScript
6+
* @abstract
7+
* @example
8+
* ```js
9+
* class Color extends Enum {
10+
* static RED = new Color('RED', '#FF0000');
11+
* static GREEN = new Color('GREEN', '#00FF00');
12+
* static BLUE = new Color('BLUE', '#0000FF');
13+
*
14+
* static {
15+
* this.freeze();
16+
* }
17+
* }
18+
* ```
19+
*/
20+
export class Enum {
21+
/** @private @static @type {Map<Function, Map<string, Enum>>} */
22+
static #instances = new Map();
23+
24+
/** @private @static @type {Set<Function>} */
25+
static #frozen = new Set();
26+
27+
/** @private @type {string} */
28+
#name;
29+
30+
/** @private @type {*} */
31+
#value;
32+
33+
/** @private @type {number} */
34+
#ordinal;
35+
36+
/**
37+
* Creates a new enum constant
38+
* @param {string} name - The name of the enum constant
39+
* @param {*} value - The value associated with the enum constant
40+
* @throws {TypeError} If attempting to instantiate Enum directly
41+
* @throws {Error} If enum is frozen or if duplicate name/value exists
42+
*/
43+
constructor(name, value) {
44+
if (this.constructor === Enum) {
45+
throw new TypeError("Cannot instantiate abstract Enum class directly");
46+
}
47+
48+
this.#name = name;
49+
this.#value = value;
50+
this.#ordinal = this.constructor.size;
51+
52+
// Register instance
53+
if (!Enum.#instances.has(this.constructor)) {
54+
Enum.#instances.set(this.constructor, new Map());
55+
}
56+
57+
const instanceMap = Enum.#instances.get(this.constructor);
58+
59+
if (Enum.#frozen.has(this.constructor)) {
60+
throw new Error(`Cannot add new enum constant ${name} to frozen enum ${this.constructor.name}`);
61+
}
62+
63+
if (instanceMap.has(name)) {
64+
throw new Error(`Duplicate enum constant name: ${name}`);
65+
}
66+
67+
for (const [, instance] of instanceMap) {
68+
if (instance.value === value) {
69+
throw new Error(`Duplicate enum value: ${value} for constant ${name}`);
70+
}
71+
}
72+
73+
instanceMap.set(name, this);
74+
}
75+
76+
/**
77+
* Gets the name of the enum constant
78+
* @returns {string} The enum constant's name
79+
*/
80+
get name() {
81+
return this.#name;
82+
}
83+
84+
/**
85+
* Gets the value of the enum constant
86+
* @returns {*} The enum constant's value
87+
*/
88+
get value() {
89+
return this.#value;
90+
}
91+
92+
/**
93+
* Gets the ordinal (position) of the enum constant
94+
* @returns {number} The enum constant's ordinal
95+
*/
96+
get ordinal() {
97+
return this.#ordinal;
98+
}
99+
100+
/**
101+
* Gets the number of constants in the enum
102+
* @returns {number} The number of enum constants
103+
*/
104+
static get size() {
105+
return this.values().length;
106+
}
107+
108+
/**
109+
* Gets all constants of the enum
110+
* @returns {Enum[]} Array of all enum constants
111+
*/
112+
static values() {
113+
const instanceMap = Enum.#instances.get(this) || new Map();
114+
return Array.from(instanceMap.values());
115+
}
116+
117+
/**
118+
* Gets all constant names of the enum
119+
* @returns {string[]} Array of all enum constant names
120+
*/
121+
static names() {
122+
const instanceMap = Enum.#instances.get(this) || new Map();
123+
return Array.from(instanceMap.keys());
124+
}
125+
126+
/**
127+
* Gets an enum constant by name
128+
* @param {string} name - The name of the enum constant
129+
* @returns {Enum} The enum constant
130+
* @throws {Error} If no constant exists with the given name
131+
*/
132+
static valueOf(name) {
133+
const instanceMap = Enum.#instances.get(this);
134+
if (!instanceMap || !instanceMap.has(name)) {
135+
throw new Error(`No enum constant ${this.name}.${name}`);
136+
}
137+
return instanceMap.get(name);
138+
}
139+
140+
/**
141+
* Gets an enum constant by value
142+
* @param {*} value - The value to look up
143+
* @returns {Enum} The enum constant
144+
* @throws {Error} If no constant exists with the given value
145+
*/
146+
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}`);
150+
}
151+
return instance;
152+
}
153+
154+
/**
155+
* Freezes the enum, preventing further constant additions
156+
* @returns {typeof Enum} The enum class
157+
*/
158+
static freeze() {
159+
Enum.#frozen.add(this);
160+
return this;
161+
}
162+
163+
/**
164+
* Checks if the enum is frozen
165+
* @returns {boolean} True if the enum is frozen
166+
*/
167+
static isFrozen() {
168+
return Enum.#frozen.has(this);
169+
}
170+
171+
/**
172+
* Gets string representation of the enum constant
173+
* @returns {string} String representation
174+
*/
175+
toString() {
176+
return `${this.constructor.name}.${this.name}`;
177+
}
178+
179+
/**
180+
* Gets JSON representation of the enum constant
181+
* @returns {{name: string, value: *, ordinal: number}} JSON representation
182+
*/
183+
toJSON() {
184+
return {
185+
name: this.name,
186+
value: this.value,
187+
ordinal: this.ordinal
188+
};
189+
}
190+
191+
/**
192+
* Converts enum constant to primitive type
193+
* @param {string} hint - The type hint ('string', 'number', or 'default')
194+
* @returns {string|number|*} The primitive value
195+
*/
196+
[Symbol.toPrimitive](hint) {
197+
switch (hint) {
198+
case 'number':
199+
return this.value;
200+
case 'string':
201+
return this.toString();
202+
default:
203+
return this.value;
204+
}
205+
}
206+
207+
/**
208+
* Checks if this enum constant equals another
209+
* @param {Enum} other - The enum constant to compare with
210+
* @returns {boolean} True if the constants are equal
211+
*/
212+
equals(other) {
213+
if (!(other instanceof this.constructor)) {
214+
return false;
215+
}
216+
return this.name === other.name && this.value === other.value;
217+
}
218+
219+
/**
220+
* Compares this enum constant with another by ordinal
221+
* @param {Enum} other - The enum constant to compare with
222+
* @returns {number} Negative if this comes before other, positive if after
223+
* @throws {TypeError} If comparing with different enum type
224+
*/
225+
compareTo(other) {
226+
if (!(other instanceof this.constructor)) {
227+
throw new TypeError(`Cannot compare ${this.constructor.name} with ${other?.constructor?.name}`);
228+
}
229+
return this.ordinal - other.ordinal;
230+
}
231+
}
232+
233+
234+
235+
// npx -p typescript tsc ./src/enum.js --declaration --allowJs --emitDeclarationOnly --lib ESNext --outDir ./dist

0 commit comments

Comments
 (0)