-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add algorithm for Map.prototype.mapKeys
Resolves #24
- Loading branch information
Showing
2 changed files
with
18 additions
and
28 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 |
---|---|---|
@@ -1,19 +1,20 @@ | ||
<!DOCTYPE html> | ||
<emu-note type="editor"> | ||
This code serves as placeholder until formal spec is written for this method. | ||
TODO: how to handle duplicate keys? Obvious way is to use `Map.prototype.set` through `Map` constructor, but that would break convention set by `Map.groupBy`. | ||
</emu-note> | ||
|
||
<pre><code class="javascript"> | ||
function mapKeys(callbackFn, thisArg) { | ||
assert(isObject(this), 'this is not an object'); | ||
assert(isCallable(callbackFn), 'callback is not a function'); | ||
const Ctor = SpeciesConstructor(this, Map); | ||
const retObj = new Ctor(); | ||
const _set = retObj.set; | ||
for(const [key, value] of this) { | ||
const newKey = Reflect.apply(callbackFn, thisArg, [value, key, this]) | ||
Reflect.apply(_set, retObj, [newKey, value]); | ||
} | ||
return retObj; | ||
} | ||
</code></pre> | ||
<emu-alg> | ||
1. Let _M_ be the *this* value. | ||
1. Perform ? RequireInternalSlot(_M_, [[MapData]]). | ||
1. If IsCallable(_callbackfn_) is **false**, throw a *TypeError* exception. | ||
1. Let _result_ be ! Construct(%Map%). | ||
1. Let _resultKeys_ be a new empty List. | ||
1. For each Record { [[Key]], [[Value]] } _e_ of _M_.[[MapData]], do | ||
1. If _e_.[[Key]] is not ~empty~, then | ||
1. Let _newKey_ be CanonicalizeKeyedCollectionKey(? Call(_callbackfn_, _T_, « _e_.[[Value]], _e_.[[Key]], _map_ »)) | ||
1. Let _p_ be the Record { [[Key]]: _newKey_, [[Value]]: _e_.[[Value]] } | ||
1. If _newKey_ is present in _resultKeys_, throw **RangeError** | ||
1. Append _p_ to _result_.[[MapData]] | ||
1. Append _newKey_ to _resultKeys_ | ||
1. Return _result_. | ||
</emu-alg> |