-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move DecodeableMap to a new file
- Loading branch information
Showing
2 changed files
with
81 additions
and
74 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,78 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
/** | ||
* This is an extension of the Map class that treats keys as the same by matching first normally, | ||
* then decoded. Decoding the key before comparing can solve some edge cases in component fullNames | ||
* such as Layouts. See: https://github.com/forcedotcom/cli/issues/1683 | ||
* | ||
* Examples: | ||
* | ||
* Given a map with entries: | ||
* ```javascript | ||
* 'layout#Layout__Broker__c-v1%2E1 Broker Layout' : {...} | ||
* 'layout#Layout__Broker__c-v9.2 Broker Layout' : {...} | ||
* ``` | ||
* | ||
* `decodeableMap.has('layout#Layout__Broker__c-v1.1 Broker Layout')` --> returns `true` | ||
* `decodeableMap.has('layout#Layout__Broker__c-v9%2E2 Broker Layout')` --> returns `true` | ||
*/ | ||
export class DecodeableMap<K extends string, V> extends Map<string, V> { | ||
/** | ||
* boolean indicating whether an element with the specified key (matching decoded) exists or not. | ||
*/ | ||
public has(key: K): boolean { | ||
return super.has(key) || this.hasDecoded(key); | ||
} | ||
|
||
/** | ||
* Returns a specified element from the Map object. If the value that is associated to | ||
* the provided key (matching decoded) is an object, then you will get a reference to | ||
* that object and any change made to that object will effectively modify it inside the Map. | ||
*/ | ||
public get(key: K): V | undefined { | ||
return super.get(key) ?? this.getDecoded(key); | ||
} | ||
|
||
/** | ||
* Adds a new element with a specified key and value to the Map. If an element with the | ||
* same key (matching decoded) already exists, the element will be updated. | ||
*/ | ||
public set(key: K, value: V): this { | ||
const sKey = this.getExistingKey(key) ?? key; | ||
return super.set(sKey, value); | ||
} | ||
|
||
/** | ||
* true if an element in the Map existed (matching decoded) and has been removed, or false | ||
* if the element does not exist. | ||
*/ | ||
public delete(key: K): boolean { | ||
const sKey = this.getExistingKey(key) ?? key; | ||
return super.delete(sKey); | ||
} | ||
|
||
// Returns true if the passed `key` matches an existing key entry when both keys are decoded. | ||
private hasDecoded(key: string): boolean { | ||
return !!this.getExistingKey(key); | ||
} | ||
|
||
// Returns the value of an entry matching on decoded keys. | ||
private getDecoded(key: string): V | undefined { | ||
const existingKey = this.getExistingKey(key); | ||
return existingKey ? super.get(existingKey) : undefined; | ||
} | ||
|
||
// Returns the key as it is in the map, matching on decoded keys. | ||
private getExistingKey(key: string): string | undefined { | ||
for (const compKey of this.keys()) { | ||
if (decodeURIComponent(compKey) === decodeURIComponent(key)) { | ||
return compKey; | ||
} | ||
} | ||
} | ||
} |
9d24d82
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
eda-componentSetCreate-linux
2307
ms2961
ms0.78
eda-sourceToMdapi-linux
8275
ms8470
ms0.98
eda-sourceToZip-linux
7539
ms8326
ms0.91
eda-mdapiToSource-linux
5123
ms6712
ms0.76
lotsOfClasses-componentSetCreate-linux
17776
ms22910
ms0.78
lotsOfClasses-sourceToMdapi-linux
25207
ms31958
ms0.79
lotsOfClasses-sourceToZip-linux
23512
ms30345
ms0.77
lotsOfClasses-mdapiToSource-linux
20931
ms26598
ms0.79
lotsOfClassesOneDir-componentSetCreate-linux
65913
ms83757
ms0.79
lotsOfClassesOneDir-sourceToMdapi-linux
74781
ms94341
ms0.79
lotsOfClassesOneDir-sourceToZip-linux
74046
ms92174
ms0.80
lotsOfClassesOneDir-mdapiToSource-linux
70466
ms94160
ms0.75
This comment was automatically generated by workflow using github-action-benchmark.
9d24d82
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
eda-componentSetCreate-win32
8920
ms7723
ms1.15
eda-sourceToMdapi-win32
17956
ms16210
ms1.11
eda-sourceToZip-win32
14906
ms14599
ms1.02
eda-mdapiToSource-win32
14398
ms12390
ms1.16
lotsOfClasses-componentSetCreate-win32
83853
ms72369
ms1.16
lotsOfClasses-sourceToMdapi-win32
96948
ms92158
ms1.05
lotsOfClasses-sourceToZip-win32
92351
ms78463
ms1.18
lotsOfClasses-mdapiToSource-win32
91697
ms81838
ms1.12
lotsOfClassesOneDir-componentSetCreate-win32
278916
ms232567
ms1.20
lotsOfClassesOneDir-sourceToMdapi-win32
293641
ms252707
ms1.16
lotsOfClassesOneDir-sourceToZip-win32
285803
ms245403
ms1.16
lotsOfClassesOneDir-mdapiToSource-win32
287577
ms247082
ms1.16
This comment was automatically generated by workflow using github-action-benchmark.