This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move internal state from objects to maps (#41)
* feat: add `CaseInsensitiveMap` * chore: use new map in `SnapKeyring` * chore: lower coverage (improved in next PR)
- Loading branch information
Showing
4 changed files
with
128 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { CaseInsensitiveMap } from './CaseInsensitiveMap'; | ||
|
||
describe('CaseInsensitiveMap', () => { | ||
it('should set and get values case-insensitively', () => { | ||
const map = new CaseInsensitiveMap<number>(); | ||
map.set('foo', 1); | ||
map.set('BAR', 2); | ||
expect(map.get('foo')).toBe(1); | ||
expect(map.get('FOO')).toBe(1); | ||
expect(map.get('bar')).toBe(2); | ||
expect(map.get('BaR')).toBe(2); | ||
}); | ||
|
||
it('should check for keys case-insensitively', () => { | ||
const map = new CaseInsensitiveMap<number>(); | ||
map.set('foo', 1); | ||
expect(map.has('foo')).toBe(true); | ||
expect(map.has('FOO')).toBe(true); | ||
expect(map.has('bar')).toBe(false); | ||
expect(map.has('BaR')).toBe(false); | ||
}); | ||
|
||
it('should delete keys case-insensitively', () => { | ||
const map = new CaseInsensitiveMap<number>(); | ||
map.set('foo', 1); | ||
map.set('BAR', 2); | ||
expect(map.delete('foo')).toBe(true); | ||
expect(map.has('foo')).toBe(false); | ||
expect(map.has('FOO')).toBe(false); | ||
expect(map.has('bar')).toBe(true); | ||
expect(map.has('BaR')).toBe(true); | ||
expect(map.delete('BaR')).toBe(true); | ||
expect(map.has('bar')).toBe(false); | ||
expect(map.has('BaR')).toBe(false); | ||
}); | ||
|
||
it('should be able to construct from items', () => { | ||
const map = new CaseInsensitiveMap<number>([ | ||
['foo', 1], | ||
['BAR', 2], | ||
]); | ||
expect(map.get('foo')).toBe(1); | ||
expect(map.get('FOO')).toBe(1); | ||
expect(map.get('bar')).toBe(2); | ||
expect(map.get('BaR')).toBe(2); | ||
}); | ||
|
||
it('should convert the map to an object', () => { | ||
const map = new CaseInsensitiveMap<number>([ | ||
['foo', 1], | ||
['BAR', 2], | ||
]); | ||
const obj = map.toObject(); | ||
expect(obj).toStrictEqual({ foo: 1, bar: 2 }); | ||
}); | ||
|
||
it('should create a map from an object', () => { | ||
const obj = { foo: 1, BAR: 2 }; | ||
const map = CaseInsensitiveMap.fromObject(obj); | ||
expect(map.get('foo')).toBe(1); | ||
expect(map.get('FOO')).toBe(1); | ||
expect(map.get('bar')).toBe(2); | ||
expect(map.get('BaR')).toBe(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export class CaseInsensitiveMap<T> extends Map<string, T> { | ||
static fromObject<T>(obj: Record<string, T>): CaseInsensitiveMap<T> { | ||
return new CaseInsensitiveMap(Object.entries(obj)); | ||
} | ||
|
||
toObject(): Record<string, T> { | ||
return Object.fromEntries(this.entries()); | ||
} | ||
|
||
get(key: string): T | undefined { | ||
return super.get(key.toLowerCase()); | ||
} | ||
|
||
has(key: string): boolean { | ||
return super.has(key.toLowerCase()); | ||
} | ||
|
||
set(key: string, value: T): this { | ||
return super.set(key.toLowerCase(), value); | ||
} | ||
|
||
delete(key: string): boolean { | ||
return super.delete(key.toLowerCase()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters