Skip to content

Commit 26f7c30

Browse files
committed
test: add check for inherited Object property names in StrEnum
1 parent c77662e commit 26f7c30

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/StrEnum.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ describe("StrEnum", () => {
3737
expect(Examples.indexOf(v)).to.eql(idx);
3838
});
3939
}
40+
41+
it("does not accept inherited Object property names", () => {
42+
for (const v of ["constructor", "toString", "__proto__", "valueOf"]) {
43+
expect(Examples.has(v)).to.eql(false);
44+
expect(Examples.includes(v)).to.eql(false);
45+
expect(Examples.indexOf(v)).to.eql(undefined);
46+
}
47+
});
48+
4049
for (const { v, exp } of fixtures) {
4150
it(`.toValid(${v}) -> ${exp ? v : "undefined"}`, () => {
4251
expect(Examples.toValid(v)).to.eql(exp ? v : undefined);

src/StrEnum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export function strEnum<T extends string>(...o: T[]): StrEnum<T> {
204204
const lcToValue = new Map<string, T>(
205205
values.map((ea) => [ea.toLowerCase(), ea]),
206206
);
207-
const valueToIndex = Object.fromEntries(
207+
const valueToIndex = new Map<string, number>(
208208
values.map((ea, idx) => [ea as string, idx]),
209209
);
210210

@@ -218,7 +218,7 @@ export function strEnum<T extends string>(...o: T[]): StrEnum<T> {
218218
s == null ? undefined : lcToValue.get(s?.toLowerCase());
219219

220220
const indexOf = (s: Nullable<string>) =>
221-
s != null ? valueToIndex[s] : undefined;
221+
s != null ? valueToIndex.get(s) : undefined;
222222

223223
const ordinal = (s: Nullable<string>) => indexOf(s) ?? values.length;
224224

0 commit comments

Comments
 (0)