A robust enumeration implementation for JavaScript/TypeScript that provides Java-style enums with additional features for TLS 1.3 protocol definitions. @version 0.10.0
- Type-safe enumerations
- Frozen constants to prevent modifications
- Value and name lookups
- Ordinal-based comparison
- JSON serialization
- Built-in TLS 1.3 protocol enumerations
deno add 'jsr:@tls/enum';import{Enum}from'jsr:@tls/enum';classColorextendsEnum{staticRED=newColor('RED','#FF0000');staticGREEN=newColor('GREEN','#00FF00');staticBLUE=newColor('BLUE','#0000FF');static{this.freeze();// Prevent further modifications}}// Usage examplesconsole.log(Color.RED.value);// '#FF0000'console.log(Color.RED.name);// 'RED'console.log(Color.RED.ordinal);// 0console.log(Color.values());// [Color.RED, Color.GREEN, Color.BLUE]console.log(Color.valueOf('GREEN'));// Color.GREENconsole.log(Color.fromValue('#0000FF'));// Color.BLUEimport{SignatureScheme}from'jsr:@tls/enum';// Access signature schemesconsole.log(SignatureScheme.RSA_PKCS1_SHA256.value);// 0x0401console.log(SignatureScheme.ED25519.name);// 'ED25519'console.log(SignatureScheme.FromValue(0x0403));// 'ECDSA_SECP256R1_SHA256'console.log(SignatureScheme.Values());// [0x0401, 0x0501, ...]import{ExtensionType}from'jsr:@tls/enum';// Access extension typesconsole.log(ExtensionType.SERVER_NAME.value);// 0console.log(ExtensionType.SUPPORTED_VERSIONS.name);// 'SUPPORTED_VERSIONS'console.log(ExtensionType.fromValue(43));// ExtensionType.SUPPORTED_VERSIONSsize: Get the number of enum constantsvalues(): Get all enum constantsnames(): Get all enum constant namesvalueOf(name): Get enum constant by namefromValue(value): Get enum constant by valuefreeze(): Prevent further modificationsisFrozen(): Check if enum is frozen
name: The name of the enum constantvalue: The value associated with the enum constantordinal: The position of the constant in the enum
toString(): Get string representationtoJSON(): Get JSON representationequals(other): Compare with another enum constantcompareTo(other): Compare ordinal positions[Symbol.toPrimitive](hint): Convert to primitive type
classHttpStatusextendsEnum{staticOK=newHttpStatus('OK',200);staticNOT_FOUND=newHttpStatus('NOT_FOUND',404);staticINTERNAL_ERROR=newHttpStatus('INTERNAL_ERROR',500);static{this.freeze();}}// Usageif(response.status===HttpStatus.OK.value){console.log('Request successful');}Contributions to improve the library are welcome. Please open an issue or pull request on the GitHub repository.
This project is licensed under the MIT License - see the LICENSE file for details.