@cedx/enum
Advanced tools
Comparing version 9.1.0 to 10.0.0
@@ -1,73 +0,50 @@ | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise throws an error. | ||
* @param enumType An enumerated type. | ||
* @param value The value to check. | ||
* @returns The specified value if it exists in the specified enumeration. | ||
* @throws `TypeError` if no such enumerated value was found. | ||
*/ | ||
export function assert(enumType, value) { | ||
if (hasValue(enumType, value)) | ||
return value; | ||
// Returns the specified value if it exists in the specified enumeration, otherwise throws an error. | ||
export var assert = function(enumType, value) { | ||
if (hasValue(enumType, value)) { | ||
return value; | ||
} else { | ||
throw TypeError(`Invalid enumerated value: ${value}`); | ||
} | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise returns the given default value. | ||
* @param enumType An enumerated type. | ||
* @param value The value to coerce. | ||
* @param defaultValue The value to return if the specified value does not exist in the enumeration. | ||
* @returns The specified value if it exists in the enumeration, otherwise the given default value. | ||
*/ | ||
export function coerce(enumType, value, defaultValue) { | ||
return hasValue(enumType, value) ? value : assert(enumType, defaultValue); | ||
} | ||
/** | ||
* Gets a map of the names and values of the constants in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @returns The names and values of the constants in the enumeration. | ||
*/ | ||
export function entries(enumType) { | ||
return new Map(Object.entries(enumType)); | ||
} | ||
/** | ||
* 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 name of the constant that has the specified value, or an empty string if no such value is found. | ||
*/ | ||
export function getName(enumType, value) { | ||
return keys(enumType).find(name => Reflect.get(enumType, name) === value) ?? ""; | ||
} | ||
/** | ||
* 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 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. | ||
* @param enumType An enumerated type. | ||
* @returns The names of the constants in the specified enumeration. | ||
*/ | ||
export function keys(enumType) { | ||
return Object.keys(enumType); | ||
} | ||
/** | ||
* Gets an array of the values of the constants in the specified enumeration. | ||
* @param enumType An enumerated type. | ||
* @returns The values of the constants in the specified enumeration. | ||
*/ | ||
export function values(enumType) { | ||
return Object.values(enumType); | ||
} | ||
} | ||
}; | ||
// Returns the specified value if it exists in the specified enumeration, otherwise returns the given default value. | ||
export var coerce = function(enumType, value, defaultValue) { | ||
if (hasValue(enumType, value)) { | ||
return value; | ||
} else { | ||
return assert(enumType, defaultValue); | ||
} | ||
}; | ||
// Gets a map of the names and values of the constants in the specified enumeration. | ||
export var entries = function(enumType) { | ||
return new Map(Object.entries(enumType)); | ||
}; | ||
// Gets the name of the constant in the specified enumeration that has the specified value. | ||
export var getName = function(enumType, value) { | ||
var ref; | ||
return (ref = keys(enumType).find(function(name) { | ||
return enumType[name] === value; | ||
})) != null ? ref : ""; | ||
}; | ||
// Gets a value indicating whether a constant with the specified name exists in the specified enumeration. | ||
export var has = function(enumType, name) { | ||
return keys(enumType).includes(name); | ||
}; | ||
// Gets a value indicating whether a constant with the specified value exists in the specified enumeration. | ||
export var hasValue = function(enumType, value) { | ||
return values(enumType).includes(value); | ||
}; | ||
// Gets an array of the names of the constants in the specified enumeration. | ||
export var keys = function(enumType) { | ||
return Object.keys(enumType); | ||
}; | ||
// Gets an array of the values of the constants in the specified enumeration. | ||
export var values = function(enumType) { | ||
return Object.values(enumType); | ||
}; |
@@ -0,3 +1,4 @@ | ||
import {Enum} from "./interface.js"; | ||
export * from "./interface.js"; | ||
import type { Enum } from "./interface.js"; | ||
/** | ||
@@ -8,3 +9,2 @@ * Creates an enumeration from the specified type definition. | ||
*/ | ||
export default function createEnum<T extends object>(typedef: T): Readonly<Enum<T> & T>; | ||
//# sourceMappingURL=index.d.ts.map | ||
export function Enum<T extends object>(typedef: T): Readonly<Enum<T> & T>; |
@@ -1,18 +0,30 @@ | ||
export * from "./interface.js"; | ||
var scalarTypes; | ||
import * as methods from "./implementation.js"; | ||
/** | ||
* Creates an enumeration from the specified type definition. | ||
* @param typedef A plain object defining the shape of the enumerated type. | ||
* @returns The newly created enumeration. | ||
*/ | ||
export default function createEnum(typedef) { | ||
const enumType = Object.create(null); | ||
const scalarTypes = new Set(["bigint", "boolean", "number", "string", "symbol"]); | ||
for (const [key, value] of Object.entries(typedef)) | ||
if (scalarTypes.has(typeof value)) | ||
Reflect.defineProperty(enumType, key, { enumerable: true, value: value }); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-function-type | ||
for (const [key, value] of Object.entries(methods)) | ||
Reflect.defineProperty(enumType, key, { value: value.bind(methods, enumType) }); | ||
return Object.freeze(enumType); | ||
} | ||
// The list of supported scalar types. | ||
scalarTypes = new Set(["bigint", "boolean", "number", "string", "symbol"]); | ||
// Creates an enumeration from the specified type definition. | ||
export var Enum = function(typedef) { | ||
var enumType, key, ref, ref1, value, x, y; | ||
enumType = Object.create(null); | ||
ref = Object.entries(typedef); | ||
for (x of ref) { | ||
[key, value] = x; | ||
if (scalarTypes.has(typeof value)) { | ||
Object.defineProperty(enumType, key, { | ||
enumerable: true, | ||
value: value | ||
}); | ||
} | ||
} | ||
ref1 = Object.entries(methods); | ||
for (y of ref1) { | ||
[key, value] = y; | ||
Object.defineProperty(enumType, key, { | ||
value: value.bind(methods, enumType) | ||
}); | ||
} | ||
return Object.freeze(enumType); | ||
}; |
@@ -5,49 +5,56 @@ /** | ||
export interface Enum<T extends object> { | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise throws an error. | ||
* @param value The value to check. | ||
* @returns The specified value if it exists in the specified enumeration. | ||
*/ | ||
assert: (value: unknown) => T[keyof T]; | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise returns the given default value. | ||
* @param value The value to coerce. | ||
* @param defaultValue The value to return if the specified value does not exist in the enumeration. | ||
* @returns The specified value if it exists in the enumeration, otherwise the given default value. | ||
*/ | ||
coerce: (value: unknown, defaultValue: T[keyof T]) => T[keyof T]; | ||
/** | ||
* Gets a map of the names and values of the constants in the specified enumeration. | ||
* @returns The names and values of the constants in the enumeration. | ||
*/ | ||
entries: () => Map<string, T[keyof T]>; | ||
/** | ||
* Gets the name 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 name of the constant that has the specified value, or an empty string if no such value is found. | ||
*/ | ||
getName: (value: unknown) => string; | ||
/** | ||
* Gets a value indicating whether a constant with the specified name exists in the specified enumeration. | ||
* @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. | ||
*/ | ||
keys: () => string[]; | ||
/** | ||
* Gets an array of the values of the constants in the specified enumeration. | ||
* @returns The values of the constants in the specified enumeration. | ||
*/ | ||
values: () => T[keyof T][]; | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise throws an error. | ||
* @param value The value to check. | ||
* @returns The specified value if it exists in the specified enumeration. | ||
*/ | ||
assert(value: unknown): T[keyof T]; | ||
/** | ||
* Returns the specified value if it exists in the specified enumeration, otherwise returns the given default value. | ||
* @param value The value to coerce. | ||
* @param defaultValue The value to return if the specified value does not exist in the enumeration. | ||
* @returns The specified value if it exists in the enumeration, otherwise the given default value. | ||
*/ | ||
coerce(value: unknown, defaultValue: T[keyof T]): T[keyof T]; | ||
/** | ||
* Gets a map of the names and values of the constants in the specified enumeration. | ||
* @returns The names and values of the constants in the enumeration. | ||
*/ | ||
entries(): Map<string, T[keyof T]>; | ||
/** | ||
* Gets the name 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 name of the constant that has the specified value, or an empty string if no such value is found. | ||
*/ | ||
getName(value: unknown): string; | ||
/** | ||
* Gets a value indicating whether a constant with the specified name exists in the specified enumeration. | ||
* @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. | ||
*/ | ||
keys(): Array<string>; | ||
/** | ||
* Gets an array of the values of the constants in the specified enumeration. | ||
* @returns The values of the constants in the specified enumeration. | ||
*/ | ||
values(): Array<T[keyof T]>; | ||
} | ||
//# sourceMappingURL=interface.d.ts.map |
@@ -9,3 +9,3 @@ { | ||
"type": "module", | ||
"version": "9.1.0", | ||
"version": "10.0.0", | ||
"author": { | ||
@@ -17,10 +17,5 @@ "email": "cedric@belin.io", | ||
"devDependencies": { | ||
"@types/eslint__js": "^8.42.3", | ||
"@types/gulp": "^4.0.17", | ||
"@types/node": "^20.14.12", | ||
"del": "^7.1.0", | ||
"execa": "^9.3.0", | ||
"gulp": "^5.0.0", | ||
"typescript": "^5.5.4", | ||
"typescript-eslint": "^8.0.0-alpha.54" | ||
"@coffeelint/cli": "^5.2.11", | ||
"@types/node": "^22.9.0", | ||
"coffeescript": "^2.7.0" | ||
}, | ||
@@ -45,5 +40,4 @@ "engines": { | ||
"scripts": { | ||
"prepack": "gulp", | ||
"test": "gulp build && node --test --test-reporter=spec" | ||
"test": "cake test" | ||
} | ||
} |
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
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
3
11892
10
129