@cedx/enum
Advanced tools
Comparing version 8.1.0 to 9.0.0
@@ -22,11 +22,4 @@ /** | ||
*/ | ||
export declare function getEntries<T extends object>(enumType: T): Map<string, T[keyof T]>; | ||
export declare function entries<T extends object>(enumType: T): Map<string, T[keyof T]>; | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @returns The zero-based position of the constant that has the specified value, or `-1` if no such value is found. | ||
*/ | ||
export declare function getIndex<T extends object>(enumType: T, value: unknown): number; | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
@@ -39,2 +32,16 @@ * @param enumType An enumerated type. | ||
/** | ||
* Gets a value indicating whether a constant with the specified name exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param name The name to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified name, otherwise `false`. | ||
*/ | ||
export declare function has<T extends object>(enumType: T, name: string): boolean; | ||
/** | ||
* Gets a value indicating whether a constant with the specified value exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
export declare function hasValue<T extends object>(enumType: T, value: unknown): value is T[keyof T]; | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
@@ -44,3 +51,3 @@ * @param enumType An enumerated type. | ||
*/ | ||
export declare function getNames<T extends object>(enumType: T): string[]; | ||
export declare function keys<T extends object>(enumType: T): string[]; | ||
/** | ||
@@ -51,10 +58,3 @@ * Gets an array of the values of the constants in the specified enumeration. | ||
*/ | ||
export declare function getValues<T extends object>(enumType: T): T[keyof T][]; | ||
/** | ||
* Gets a value indicating whether a constant with a specified value exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
export declare function isDefined<T extends object>(enumType: T, value: unknown): value is T[keyof T]; | ||
export declare function values<T extends object>(enumType: T): T[keyof T][]; | ||
//# sourceMappingURL=implementation.d.ts.map |
@@ -9,3 +9,3 @@ /** | ||
export function assert(enumType, value) { | ||
if (isDefined(enumType, value)) | ||
if (hasValue(enumType, value)) | ||
return value; | ||
@@ -22,3 +22,3 @@ throw TypeError(`Invalid enumerated value: ${value}`); | ||
export function coerce(enumType, value, defaultValue) { | ||
return isDefined(enumType, value) ? value : assert(enumType, defaultValue); | ||
return hasValue(enumType, value) ? value : assert(enumType, defaultValue); | ||
} | ||
@@ -30,24 +30,33 @@ /** | ||
*/ | ||
export function getEntries(enumType) { | ||
export function entries(enumType) { | ||
return new Map(Object.entries(enumType)); | ||
} | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @returns The zero-based position of the constant that has the specified value, or `-1` if no such value is found. | ||
* @returns The name of the constant that has the specified value, or an empty string if no such value is found. | ||
*/ | ||
export function getIndex(enumType, value) { | ||
return getValues(enumType).indexOf(value); | ||
export function getName(enumType, value) { | ||
return keys(enumType).find(name => Reflect.get(enumType, name) === value) ?? ""; | ||
} | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
* Gets a value indicating whether a constant with the specified name exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @returns The name of the constant that has the specified value, or an empty string if no such value is found. | ||
* @param name The name to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified name, otherwise `false`. | ||
*/ | ||
export function getName(enumType, value) { | ||
return getNames(enumType).find(name => Reflect.get(enumType, name) === value) ?? ""; | ||
export function has(enumType, name) { | ||
return keys(enumType).includes(name); | ||
} | ||
/** | ||
* Gets a value indicating whether a constant with the specified value exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
export function hasValue(enumType, value) { | ||
return values(enumType).includes(value); | ||
} | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
@@ -57,3 +66,3 @@ * @param enumType An enumerated type. | ||
*/ | ||
export function getNames(enumType) { | ||
export function keys(enumType) { | ||
return Object.keys(enumType); | ||
@@ -66,13 +75,4 @@ } | ||
*/ | ||
export function getValues(enumType) { | ||
export function values(enumType) { | ||
return Object.values(enumType); | ||
} | ||
/** | ||
* Gets a value indicating whether a constant with a specified value exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
export function isDefined(enumType, value) { | ||
return getValues(enumType).includes(value); | ||
} |
@@ -0,3 +1,3 @@ | ||
import type { Enum } from "./interface.js"; | ||
export * from "./interface.js"; | ||
import type { Enum } from "./interface.js"; | ||
/** | ||
@@ -4,0 +4,0 @@ * Creates an enumeration from the specified type definition. |
@@ -0,3 +1,3 @@ | ||
import * as methods from "./implementation.js"; | ||
export * from "./interface.js"; | ||
import * as methods from "./implementation.js"; | ||
/** | ||
@@ -13,6 +13,7 @@ * Creates an enumeration from the specified type definition. | ||
if (scalarTypes.includes(typeof value)) | ||
Reflect.defineProperty(enumType, key, { enumerable: true, value }); | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
Object.entries(methods).forEach(([key, value]) => Reflect.defineProperty(enumType, key, { value: value.bind(methods, enumType) })); | ||
Reflect.defineProperty(enumType, key, { enumerable: true, value: value }); | ||
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-unsafe-assignment | ||
for (const [key, value] of Object.entries(methods)) | ||
Reflect.defineProperty(enumType, key, { value: value.bind(methods, enumType) }); | ||
return Object.freeze(enumType); | ||
} |
@@ -23,10 +23,4 @@ /** | ||
*/ | ||
getEntries: () => Map<string, T[keyof T]>; | ||
entries: () => Map<string, T[keyof T]>; | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* @param value The value of a constant in the enumerated type. | ||
* @returns The zero-based position of the constant that has the specified value, or `-1` if no such value is found. | ||
*/ | ||
getIndex: (value: unknown) => number; | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
@@ -38,6 +32,19 @@ * @param value The value of a constant in the enumerated type. | ||
/** | ||
* Gets a value indicating whether a constant with the specified name exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param name The name to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified name, otherwise `false`. | ||
*/ | ||
has: (name: string) => boolean; | ||
/** | ||
* Gets a value indicating whether a constant with the specified value exists in the specified enumeration. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
hasValue: (value: unknown) => value is T[keyof T]; | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
* @returns The names of the constants in the specified enumeration. | ||
*/ | ||
getNames: () => string[]; | ||
keys: () => string[]; | ||
/** | ||
@@ -47,10 +54,4 @@ * Gets an array of the values of the constants in the specified enumeration. | ||
*/ | ||
getValues: () => T[keyof T][]; | ||
/** | ||
* Gets a value indicating whether a constant with a specified value exists in the specified enumeration. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
isDefined: (value: unknown) => value is T[keyof T]; | ||
values: () => T[keyof T][]; | ||
} | ||
//# sourceMappingURL=interface.d.ts.map |
{ | ||
"bugs": "https://github.com/cedx/enum.js/issues", | ||
"description": "A simple implementation of enumerated types.", | ||
"homepage": "https://github.com/cedx/enum.js", | ||
"homepage": "https://docs.belin.io/enum.js", | ||
"license": "MIT", | ||
@@ -9,3 +9,3 @@ "name": "@cedx/enum", | ||
"type": "module", | ||
"version": "8.1.0", | ||
"version": "9.0.0", | ||
"author": { | ||
@@ -17,8 +17,12 @@ "email": "cedric@belin.io", | ||
"devDependencies": { | ||
"@types/node": "^20.11.6", | ||
"@typescript-eslint/eslint-plugin": "^6.19.1", | ||
"@typescript-eslint/parser": "^6.19.1", | ||
"eslint": "^8.56.0", | ||
"typedoc": "^0.25.7", | ||
"typescript": "^5.3.3" | ||
"@types/eslint__js": "^8.42.3", | ||
"@types/gulp": "^4.0.17", | ||
"@types/node": "^20.12.2", | ||
"del": "^7.1.0", | ||
"eslint": "^8.57.0", | ||
"execa": "^8.0.1", | ||
"gulp": "^5.0.0", | ||
"typedoc": "^0.25.12", | ||
"typescript": "^5.4.3", | ||
"typescript-eslint": "^7.4.0" | ||
}, | ||
@@ -36,8 +40,2 @@ "engines": { | ||
], | ||
"imports": { | ||
"#enum": { | ||
"types": "./lib/index.d.ts", | ||
"import": "./lib/index.js" | ||
} | ||
}, | ||
"keywords": [ | ||
@@ -50,11 +48,5 @@ "enum", | ||
"scripts": { | ||
"build": "tsc --project src/tsconfig.json", | ||
"clean": "node tool/clean.js", | ||
"doc": "typedoc --options etc/typedoc.js && node tool/doc.js", | ||
"dist": "npm run clean && npm run build", | ||
"lint": "tsc --project tsconfig.json && eslint --config=etc/eslint.cjs example src test tool", | ||
"postpublish": "node tool/publish.js", | ||
"prepack": "npm run dist", | ||
"test": "npm run build && node --test --test-reporter=spec" | ||
"prepack": "gulp", | ||
"test": "gulp build && node --test --test-reporter=spec" | ||
} | ||
} |
@@ -7,4 +7,4 @@ # Enums for JS | ||
## Documentation | ||
- [User guide](https://cedx.github.io/enum.js) | ||
- [API reference](https://cedx.github.io/enum.js/api) | ||
- [User guide](https://docs.belin.io/enum.js) | ||
- [API reference](https://docs.belin.io/enum.js/api) | ||
@@ -11,0 +11,0 @@ ## Development |
@@ -9,3 +9,3 @@ /** | ||
export function assert<T extends object>(enumType: T, value: unknown): T[keyof T] { | ||
if (isDefined(enumType, value)) return value; | ||
if (hasValue(enumType, value)) return value; | ||
throw TypeError(`Invalid enumerated value: ${value}`); | ||
@@ -22,3 +22,3 @@ } | ||
export function coerce<T extends object>(enumType: T, value: unknown, defaultValue: T[keyof T]): T[keyof T] { | ||
return isDefined(enumType, value) ? value : assert(enumType, defaultValue); | ||
return hasValue(enumType, value) ? value : assert(enumType, defaultValue); | ||
} | ||
@@ -31,27 +31,37 @@ | ||
*/ | ||
export function getEntries<T extends object>(enumType: T): Map<string, T[keyof T]> { | ||
return new Map(Object.entries(enumType)); | ||
export function entries<T extends object>(enumType: T): Map<string, T[keyof T]> { | ||
return new Map<string, T[keyof T]>(Object.entries(enumType)); | ||
} | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @returns The zero-based position of the constant that has the specified value, or `-1` if no such value is found. | ||
* @returns The name of the constant that has the specified value, or an empty string if no such value is found. | ||
*/ | ||
export function getIndex<T extends object>(enumType: T, value: unknown): number { | ||
return getValues(enumType).indexOf(value as any); | ||
export function getName<T extends object>(enumType: T, value: unknown): string { | ||
return keys(enumType).find(name => Reflect.get(enumType, name) === value) ?? ""; | ||
} | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
* Gets a value indicating whether a constant with the specified name exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value of a constant in the enumerated type. | ||
* @returns The name of the constant that has the specified value, or an empty string if no such value is found. | ||
* @param name The name to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified name, otherwise `false`. | ||
*/ | ||
export function getName<T extends object>(enumType: T, value: unknown): string { | ||
return getNames(enumType).find(name => Reflect.get(enumType, name) === value) ?? ""; | ||
export function has<T extends object>(enumType: T, name: string): boolean { | ||
return keys(enumType).includes(name); | ||
} | ||
/** | ||
* Gets a value indicating whether a constant with the specified value exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
export function hasValue<T extends object>(enumType: T, value: unknown): value is T[keyof T] { | ||
return values(enumType).includes(value as T[keyof T]); | ||
} | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
@@ -61,3 +71,3 @@ * @param enumType An enumerated type. | ||
*/ | ||
export function getNames<T extends object>(enumType: T): string[] { | ||
export function keys<T extends object>(enumType: T): string[] { | ||
return Object.keys(enumType); | ||
@@ -71,14 +81,4 @@ } | ||
*/ | ||
export function getValues<T extends object>(enumType: T): T[keyof T][] { | ||
return Object.values(enumType); | ||
export function values<T extends object>(enumType: T): T[keyof T][] { | ||
return Object.values(enumType) as T[keyof T][]; | ||
} | ||
/** | ||
* Gets a value indicating whether a constant with a specified value exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
export function isDefined<T extends object>(enumType: T, value: unknown): value is T[keyof T] { | ||
return getValues(enumType).includes(value as any); | ||
} |
@@ -1,5 +0,6 @@ | ||
export * from "./interface.js"; | ||
import * as methods from "./implementation.js"; | ||
import type {Enum} from "./interface.js"; | ||
export * from "./interface.js"; | ||
/** | ||
@@ -11,10 +12,10 @@ * Creates an enumeration from the specified type definition. | ||
export default function createEnum<T extends object>(typedef: T): Readonly<Enum<T> & T> { | ||
const enumType = Object.create(null); | ||
const enumType = Object.create(null) as Enum<T> & T; | ||
const scalarTypes = ["bigint", "boolean", "number", "string", "symbol"]; | ||
for (const [key, value] of Object.entries(typedef)) | ||
if (scalarTypes.includes(typeof value)) Reflect.defineProperty(enumType, key, {enumerable: true, value}); | ||
if (scalarTypes.includes(typeof value)) Reflect.defineProperty(enumType, key, {enumerable: true, value: value as T[keyof T]}); | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
Object.entries(methods).forEach(([key, value]) => Reflect.defineProperty(enumType, key, {value: (value as Function).bind(methods, enumType)})); | ||
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-unsafe-assignment | ||
for (const [key, value] of Object.entries(methods)) Reflect.defineProperty(enumType, key, {value: (value as Function).bind(methods, enumType)}) | ||
return Object.freeze(enumType); | ||
} |
@@ -26,12 +26,5 @@ /** | ||
*/ | ||
getEntries: () => Map<string, T[keyof T]>; | ||
entries: () => Map<string, T[keyof T]>; | ||
/** | ||
* Gets the zero-based position of the constant in the specified enumeration that has the specified value. | ||
* @param value The value of a constant in the enumerated type. | ||
* @returns The zero-based position of the constant that has the specified value, or `-1` if no such value is found. | ||
*/ | ||
getIndex: (value: unknown) => number; | ||
/** | ||
* Gets the name of the constant in the specified enumeration that has the specified value. | ||
@@ -44,6 +37,21 @@ * @param value The value of a constant in the enumerated type. | ||
/** | ||
* Gets a value indicating whether a constant with the specified name exists in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @param name The name to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified name, otherwise `false`. | ||
*/ | ||
has: (name: string) => boolean; | ||
/** | ||
* Gets a value indicating whether a constant with the specified value exists in the specified enumeration. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
hasValue: (value: unknown) => value is T[keyof T]; | ||
/** | ||
* Gets an array of the names of the constants in the specified enumeration. | ||
* @returns The names of the constants in the specified enumeration. | ||
*/ | ||
getNames: () => string[]; | ||
keys: () => string[]; | ||
@@ -54,10 +62,3 @@ /** | ||
*/ | ||
getValues: () => T[keyof T][]; | ||
/** | ||
* Gets a value indicating whether a constant with a specified value exists in the specified enumeration. | ||
* @param value The value to check. | ||
* @returns `true` if a constant in the specified enumeration has the specified value, otherwise `false`. | ||
*/ | ||
isDefined: (value: unknown) => value is T[keyof T]; | ||
values: () => T[keyof T][]; | ||
} |
{ | ||
"extends": "../tsconfig", | ||
"include": ["**/*.ts"], | ||
"include": ["index.ts"], | ||
"compilerOptions": { | ||
@@ -5,0 +5,0 @@ "declaration": true, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
360
0
22174
10
1