
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
npm i ts-jenum
import {Enum, EnumType} from "ts-jenum";
@Enum("text")
export class State extends EnumType<State>() {
static readonly NEW = new State(1, "New");
static readonly ACTIVE = new State(2, "Active");
static readonly BLOCKED = new State(3, "Blocked");
private constructor(readonly code: number, readonly text: string) {
super();
}
}
// Usage example
console.log("" + State.ACTIVE); // "Active"
console.log("" + State.BLOCKED); // "Blocked"
console.log(State.values()); // [State.NEW, State.ACTIVE, State.BLOCKED]
console.log(State.valueOf("New")); // State.NEW
State.valueOf("Unknown") // throw Error(...)
console.log(State.valueByName("NEW")); // State.NEW
console.log(State.ACTIVE.enumName); // ACTIVE
const first = state => state.code === 1;
console.log(State.find("New")); // State.NEW
console.log(State.find(first)); // State.NEW
console.log(State.find("Unknown")); // null
const last = state => state.code === 3;
console.log(State.filter(last)) // [State.BLOCKED]
console.log(State.keys()) // ["NEW", "ACTIVE", "BLOCKED"]
// be "NEW" | "ACTIVE" | "BLOCKED"
type StateNameUnion = EnumConstNames<typeof State>;
import {EnumTools} from "ts-jenum";
// plain json like enum
const Colors = {
WHITE: "#FFFFFF",
GRAY: "#808080",
BLACK: "#000000"
};
// to be ["WHITE", "GRAY", "BLACK"]
const keys = EnumTools.keys(Colors);
// to be ["#FFFFFF", "#808080", "#000000"]
const values = EnumTools.values(Colors);
/**
* to be {
* "#FFFFFF": "WHITE",
* "#808080": "GRAY",
* "#000000": "BLACK"
* };
*/
const rStruct = EnumTools.reverse(Colors);
/**
* to be: [
* {key: "WHITE", value: "#FFFFFF"},
* {key: "GRAY", value: "#808080"},
* {key: "BLACK", value: "#000000"}
* ]
*/
const pairs = EnumTools.pairs(Colors);
/**
* To be class like:
* @Enum<ColorEnum>("key")
* class ColorEnum extends EnumType<ColorEnum>() {
* static readonly WHITE = new ColorEnum("WHITE", "#FFFFFF");
* static readonly GRAY = new ColorEnum("GRAY", "#808080");
* static readonly BLACK = new ColorEnum("BLACK", "#000000");
* private constructor(readonly key: string, readonly value: string | number) {
* super();
* }
* }
* ColorEnum has all IDE hint for developer, type checking and type safety
*/
const ColorEnum = EnumTools.toClass(Colors);
Details. Type safety. In example above, you can write "tExt" or "txt" instead of "text" as @Enum decorator argument and no exception happen. In example below this problem is absent. Add an expression <State> to @Enum decorator
import {Enum, EnumConstNames, EnumType} from "ts-jenum";
@Enum<State>("text")
export class State extends EnumType<State>() {
static readonly NEW = new State(1, "New");
static readonly ACTIVE = new State(2, "Active");
static readonly BLOCKED = new State(3, "Blocked");
private constructor(readonly code: number, readonly text: string) {
super();
}
}
// Get Enum Names
// be "NEW" | "ACTIVE" | "BLOCKED"
type StateNameUnion = EnumConstNames<typeof State>;
Powerful typing.
@Enum<Person>("id")
class Person<IdType extends 1 | 2 = 1 | 2,
NameType extends "Ivan" | "John" = "Ivan" | "John"
> extends EnumType<Person>() {
static readonly IVAN = new Person(1 as const, "Ivan" as const);
static readonly JOHN = new Person(2 as const, "John" as const);
private constructor(readonly id: IdType, readonly name: NameType) {
super();
}
static doSomeWork(): void {
// type to be: "Ivan". Not string!
const name = Person.IVAN.name;
// to be: error
// if (name === "cat")
// ^ This condition will always return 'false' since the types '"Ivan"' and '"cat"' have no overlap.
// type to be: 1. Not number!
const id = Person.IVAN.id;
// to be: error
// if (id === 3)
// ^ This condition will always return 'false' since the types '1' and '3' have no overlap
}
}
FAQs
TypeScript Java Enum
The npm package ts-jenum receives a total of 1,163 weekly downloads. As such, ts-jenum popularity was classified as popular.
We found that ts-jenum demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.