Skip to content

Commit

Permalink
firstMapValue util function [refactor]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 10, 2023
1 parent fbfaab8 commit 5fdd9e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/serialize/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const {
createRecord, createDependency, createAssignment, createBlock, createScope
} = require('./records.js'),
{addStrictDirectiveToFunction} = require('./strict.js'),
{replaceRecordNode, getNodeWithinWrapperParent, setAddFrom, deleteFirst} = require('./utils.js');
{
replaceRecordNode, getNodeWithinWrapperParent, setAddFrom, firstMapValue, deleteFirst
} = require('./utils.js');

// Exports

Expand Down Expand Up @@ -788,7 +790,7 @@ function createMissingScopes(block, parentBlock) {
possibleParentScopes = possibleScopes;
}

parentScope = possibleParentScopes.values().next().value;
parentScope = firstMapValue(possibleParentScopes);

// If some missing scopes don't exist, create them
for (; index < missingBlocks.length; index++) {
Expand Down
10 changes: 10 additions & 0 deletions lib/serialize/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
replaceRecordNode,
getNodeWithinWrapperParent,
setAddFrom,
firstMapValue,
deleteItem,
deleteFirst
};
Expand Down Expand Up @@ -152,6 +153,15 @@ function setAddFrom(set, from) {
return set;
}

/**
* Get first value of Map.
* @param {Map} map - Map
* @returns {*} - First value of map (or `undefined` if map has no entries)
*/
function firstMapValue(map) {
return map.values().next().value;
}

/**
* Delete first instance of value from array, and return deleted value.
* @param {Array} arr - Array
Expand Down

0 comments on commit 5fdd9e2

Please sign in to comment.