@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
64 lines (62 loc) • 1.42 kB
JavaScript
import {
meta
} from "./chunk-WTFW6DLP.mjs";
import {
toList
} from "./chunk-A7C3XND3.mjs";
import {
ofGet
} from "./chunk-SJGQU3OG.mjs";
import {
isAn
} from "./chunk-ZHXKBOK2.mjs";
import {
isDefined
} from "./chunk-DEJ7A5PY.mjs";
// src/types/Enum.ts
var Enum = class {
constructor(name, id = name.toLowerCase(), code = id.toString()) {
this.name = name;
this.id = id;
this.code = code;
}
get isValid() {
return isDefined(this.id);
}
static all() {
return meta(this.allTuple()).values();
}
static allTuple() {
return meta(this).get(`all-${this.name}`) ?? meta(this).set(`all-${this.name}`, meta(this).values().filter(isEnum).toObject("id"));
}
static filter(p, params) {
return this.all().filter(p, params);
}
static first(p, params) {
return this.all().first(p, params);
}
static byIds(ids = []) {
return toList(ids).mapDefined((id) => this.byId(id)).distinct();
}
static byId(id, alt) {
return this.allTuple()[id] ?? ofGet(alt);
}
equals(other) {
return this.id === (isEnum(other) ? other.id : other);
}
isIn(...items) {
return items.some((i) => this.equals(i));
}
toJSON() {
return this.id;
}
toString() {
return this.id.toString();
}
};
var isEnum = (e) => isDefined(e) && e instanceof Enum && isAn(e, "name", "id", "code");
export {
Enum,
isEnum
};
//# sourceMappingURL=chunk-JVDKV5HE.mjs.map