Skip to content

Commit 8914365

Browse files
authored
Merge pull request #971 from ecomfe/hashmap
feat(util): add `hasKey` method into `HashMap` for ECharts.
2 parents 7db99bc + e91e7cb commit 8914365

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/core/util.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -655,22 +655,26 @@ export function isPrimitive(obj: any): boolean {
655655

656656
interface MapInterface<T, KEY extends string | number = string | number> {
657657
delete(key: KEY): boolean;
658-
get(key: KEY): T | undefined
659-
set(key: KEY, value: T): this
658+
has(key: KEY): boolean;
659+
get(key: KEY): T | undefined;
660+
set(key: KEY, value: T): this;
660661
keys(): KEY[];
661-
forEach(callback: (value: T, key: KEY) => void): void
662+
forEach(callback: (value: T, key: KEY) => void): void;
662663
}
663664

664665
class MapPolyfill<T, KEY extends string | number = string | number> implements MapInterface<T, KEY> {
665666
private data: Record<KEY, T> = {} as Record<KEY, T>;
666667

667668
delete(key: KEY): boolean {
668-
const existed = this.data.hasOwnProperty(key);
669+
const existed = this.has(key);
669670
if (existed) {
670671
delete this.data[key];
671672
}
672673
return existed;
673674
}
675+
has(key: KEY): boolean {
676+
return this.data.hasOwnProperty(key);
677+
}
674678
get(key: KEY): T | undefined {
675679
return this.data[key];
676680
}
@@ -726,9 +730,10 @@ export class HashMap<T, KEY extends string | number = string | number> {
726730
}
727731
}
728732

729-
// Do not provide `has` method to avoid defining what is `has`.
730-
// (We usually treat `null` and `undefined` as the same, different
731-
// from ES6 Map).
733+
// `hasKey` instead of `has` for potential misleading.
734+
hasKey(key: KEY): boolean {
735+
return this.data.has(key);
736+
}
732737
get(key: KEY): T {
733738
return this.data.get(key);
734739
}
@@ -749,13 +754,11 @@ export class HashMap<T, KEY extends string | number = string | number> {
749754
});
750755
}
751756
keys(): KEY[] {
752-
// Native map return an iterator so we need to convert it to an array
753-
if (isNativeMapSupported) {
754-
return Array.from(this.data.keys());
755-
}
756-
757-
// Polyfilled map return and Array<KEYS> for keys
758-
return this.data.keys();
757+
const keys = this.data.keys();
758+
return isNativeMapSupported
759+
// Native map returns an iterator so we need to convert it to an array
760+
? Array.from(keys)
761+
: keys;
759762
}
760763
// Do not use this method if performance sensitive.
761764
removeKey(key: KEY): void {

0 commit comments

Comments
 (0)