Skip to content

Commit

Permalink
Replace our lib function "extend" with Object.assign
Browse files Browse the repository at this point in the history
  • Loading branch information
philc committed May 21, 2020
1 parent bdd7126 commit fbe6f7a
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 75 deletions.
7 changes: 4 additions & 3 deletions background_scripts/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Commands = {
init() {
for (let command of Object.keys(commandDescriptions)) {
const [description, options] = commandDescriptions[command];
this.availableCommands[command] = extend((options || {}), {description});
this.availableCommands[command] = Object.assign((options || {}), {description});
}

Settings.postUpdateHooks["keyMappings"] = this.loadKeyMappings.bind(this);
Expand Down Expand Up @@ -34,7 +34,8 @@ const Commands = {
seen[key] = true;
const keySequence = this.parseKeySequence(key);
const options = this.parseCommandOptions(command, optionList);
this.keyToCommandRegistry[key] = extend({keySequence, command, options, optionList}, this.availableCommands[command]);
this.keyToCommandRegistry[key] =
Object.assign({keySequence, command, options, optionList}, this.availableCommands[command]);
}
}
break;
Expand Down Expand Up @@ -139,7 +140,7 @@ const Commands = {
} else if (index < (registryEntry.keySequence.length - 1)) {
currentMapping = currentMapping[key] != null ? currentMapping[key] : (currentMapping[key] = {});
} else {
currentMapping[key] = extend({}, registryEntry);
currentMapping[key] = Object.assign({}, registryEntry);
// We don't need these properties in the content scripts.
for (let prop of ["keySequence", "description"])
delete currentMapping[key][prop];
Expand Down
12 changes: 6 additions & 6 deletions background_scripts/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Suggestion {
// Other options set by individual completers include:
// - tabId (TabCompleter)
// - isSearchSuggestion, customSearchMode (SearchEngineCompleter)
extend(this, this.options);
Object.assign(this, this.options);
}

// Returns the relevancy score.
Expand Down Expand Up @@ -504,11 +504,11 @@ class SearchEngineCompleter {
preprocessRequest(request) {
SearchEngines.use(engines => {
const { queryTerms, query } = request;
extend(request, {searchEngines: engines, keywords: Object.keys(engines)});
Object.assign(request, {searchEngines: engines, keywords: Object.keys(engines)});
const keyword = queryTerms[0];
// Note. For a keyword "w", we match "w search terms" and "w ", but not "w" on its own.
if (keyword && engines[keyword] && ((1 < queryTerms.length) || /\S\s/.test(query))) {
extend(request, {
Object.assign(request, {
queryTerms: queryTerms.slice(1),
keyword,
engine: engines[keyword],
Expand Down Expand Up @@ -538,7 +538,7 @@ class SearchEngineCompleter {
if (!engine) { return onComplete([]); }

const { keyword, searchUrl, description } = engine;
extend(request, searchUrl, {customSearchMode: true});
Object.assign(request, searchUrl, {customSearchMode: true});

if (this.previousSuggestions[searchUrl] == null)
this.previousSuggestions[searchUrl] = [];
Expand Down Expand Up @@ -578,7 +578,7 @@ class SearchEngineCompleter {
if (!RankingUtils.matches(queryTerms, suggestion.title))
continue;
// Reset various fields, they may not be correct wrt. the current query.
extend(suggestion, {relevancy: null, html: null, queryTerms});
Object.assign(suggestion, {relevancy: null, html: null, queryTerms});
suggestion.relevancy = null;
previousSuggestions.push(suggestion);
}
Expand Down Expand Up @@ -1084,7 +1084,7 @@ HistoryCache.binarySearch = function(targetElement, array, compareFunction) {
return middle;
};

extend(global, {
Object.assign(global, {
Suggestion,
BookmarkCompleter,
MultiCompleter,
Expand Down
2 changes: 1 addition & 1 deletion background_scripts/completion_engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// options.example: an example object containing at least "keyword" and "searchUrl", and optional "description".
class BaseEngine {
constructor(options) {
extend(this, options);
Object.assign(this, options);
this.regexps = this.regexps.map(regexp => new RegExp(regexp));
}

Expand Down
23 changes: 14 additions & 9 deletions background_scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const completionHandlers = {
// This can happen, for example, when posting completion suggestions from the SearchEngineCompleter
// (which is done asynchronously).
try {
return port.postMessage(extend(request, extend(response, {handler: "completions"})));
return port.postMessage(Object.assign(request, response, {handler: "completions"}));
} catch (error) {}
});
},
Expand All @@ -86,7 +86,9 @@ chrome.runtime.onConnect.addListener(function(port) {
});

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
request = extend({count: 1, frameId: sender.frameId}, extend(request, {tab: sender.tab, tabId: sender.tab.id}));
request = Object.assign({count: 1, frameId: sender.frameId},
request,
{tab: sender.tab, tabId: sender.tab.id});
if (sendRequestHandlers[request.handler]) {
sendResponse(sendRequestHandlers[request.handler](request, sender));
}
Expand Down Expand Up @@ -163,7 +165,7 @@ const TabOperations = {

// clean position and active, so following `openUrlInNewTab(request)` will create a tab just next to this new tab
return chrome.tabs.create(tabConfig, tab =>
callback(extend(request, {tab, tabId: tab.id, position: "", active: false})));
callback(Object.assign(request, {tab, tabId: tab.id, position: "", active: false})));
},

// Opens request.url in new window and switches to it.
Expand Down Expand Up @@ -281,14 +283,17 @@ const BackgroundCommands = {
request.position = request.registryEntry.options.position;
return (openNextUrl = function(request) {
if (urls.length > 0)
return TabOperations.openUrlInNewTab((extend(request, {url: urls.pop()})), openNextUrl);
return TabOperations.openUrlInNewTab((Object.assign(request, {url: urls.pop()})), openNextUrl);
else
return callback(request);
})(request);
}
}),

duplicateTab: mkRepeatCommand((request, callback) => chrome.tabs.duplicate(request.tabId, tab => callback(extend(request, {tab, tabId: tab.id})))),
duplicateTab: mkRepeatCommand((request, callback) => {
return chrome.tabs.duplicate(request.tabId,
tab => callback(Object.assign(request, {tab, tabId: tab.id})))
}),

moveTabToNewWindow({count, tab}) {
chrome.tabs.query({currentWindow: true}, function(tabs) {
Expand Down Expand Up @@ -481,7 +486,7 @@ var Frames = {
}
}

return port.postMessage(extend(request, enabledState));
return port.postMessage(Object.assign(request, enabledState));
},

domReady({tabId, frameId}) {
Expand Down Expand Up @@ -537,7 +542,7 @@ var HintCoordinator = {
if (request == null)
request = {};
try {
return port.postMessage(extend(request, {handler: "linkHintsMessage", messageType}));
return port.postMessage(Object.assign(request, {handler: "linkHintsMessage", messageType}));
} catch (error) {
return this.unregisterFrame(tabId, frameId);
}
Expand Down Expand Up @@ -574,7 +579,7 @@ var HintCoordinator = {
for (frameId of Object.keys(this.tabState[tabId].ports || {})) {
const port = this.tabState[tabId].ports[frameId];
if (frameId in this.tabState[tabId].hintDescriptors) {
hintDescriptors = extend({}, this.tabState[tabId].hintDescriptors);
hintDescriptors = Object.assign({}, this.tabState[tabId].hintDescriptors);
// We do not send back the frame's own hint descriptors. This is faster (approx. speedup 3/2) for
// link-busy sites like reddit.
delete hintDescriptors[frameId];
Expand Down Expand Up @@ -709,4 +714,4 @@ chrome.runtime.onInstalled.addListener(function({reason}) {
chrome.storage.local.set({installDate: new Date().toString()});
});

extend(global, {TabOperations, Frames});
Object.assign(global, {TabOperations, Frames});
10 changes: 6 additions & 4 deletions background_scripts/marks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const Marks = {
// The front-end frame hasn't provided the scroll position (because it's not the top frame within its
// tab). We need to ask the top frame what its scroll position is.
return chrome.tabs.sendMessage(sender.tab.id, {name: "getScrollPosition"}, response => {
return this.saveMark(extend(markInfo, {scrollX: response.scrollX, scrollY: response.scrollY}));
return this.saveMark(Object.assign(markInfo,
{scrollX: response.scrollX, scrollY: response.scrollY}));
});
}
});
Expand Down Expand Up @@ -85,14 +86,15 @@ const Marks = {
if (tabs.length > 0) {
// We have at least one matching tab. Pick one and go to it.
return this.pickTab(tabs, tab => {
return this.gotoPositionInTab(extend(markInfo, {tabId: tab.id}));
return this.gotoPositionInTab(Object.assign(markInfo, {tabId: tab.id}));
});
} else {
// There is no existing matching tab, we'll have to create one.
return TabOperations.openUrlInNewTab((extend(req, {url: this.getBaseUrl(markInfo.url)})), tab => {
return TabOperations.openUrlInNewTab(Object.assign(req, {url: this.getBaseUrl(markInfo.url)}), tab => {
// Note. tabLoadedHandlers is defined in "main.js". The handler below will be called when the tab
// is loaded, its DOM is ready and it registers with the background page.
return tabLoadedHandlers[tab.id] = () => this.gotoPositionInTab(extend(markInfo, {tabId: tab.id}));
return tabLoadedHandlers[tab.id] =
() => this.gotoPositionInTab(Object.assign(markInfo, {tabId: tab.id}));
});
}
});
Expand Down
12 changes: 6 additions & 6 deletions content_scripts/link_hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const HintCoordinator = {

sendMessage(messageType, request) {
if (request == null) { request = {}; }
Frame.postMessage("linkHintsMessage", extend(request, {messageType}));
Frame.postMessage("linkHintsMessage", Object.assign(request, {messageType}));
},

prepareToActivateMode(mode, onExit) {
Expand Down Expand Up @@ -343,7 +343,7 @@ class LinkHintsMode {
el.style.top = el.rect.top + "px";
// Each hint marker is assigned a different z-index.
el.style.zIndex = this.getNextZIndex();
marker = extend(el, {
marker = Object.assign(el, {
className: "vimiumReset internalVimiumHintMarker vimiumHintMarker",
showLinkText: localHintDescriptor.showLinkText,
localHintDescriptor
Expand All @@ -352,7 +352,7 @@ class LinkHintsMode {
marker = {};
}

return extend(marker, {
return Object.assign(marker, {
hintDescriptor: desc,
isLocalMarker: desc.frameId === frameId,
linkText: desc.linkText,
Expand Down Expand Up @@ -446,7 +446,7 @@ class LinkHintsMode {
}

updateKeyState({hintKeystrokeQueue, linkTextKeystrokeQueue, tabCount}) {
extend(this.markerMatcher, {hintKeystrokeQueue, linkTextKeystrokeQueue});
Object.assign(this.markerMatcher, {hintKeystrokeQueue, linkTextKeystrokeQueue});

const {linksMatched, userMightOverType} =
this.markerMatcher.getMatchingHints(this.hintMarkers, tabCount, this.getNextZIndex.bind(this));
Expand Down Expand Up @@ -1163,7 +1163,7 @@ var LocalHints = {

if (Settings.get("filterLinkHints")) {
for (hint of localHints)
extend(hint, this.generateLinkText(hint));
Object.assign(hint, this.generateLinkText(hint));
}
return localHints;
},
Expand Down Expand Up @@ -1266,7 +1266,7 @@ class HoverMode extends Mode {
}
}

extend(global, {
Object.assign(global, {
LinkHints,
HintCoordinator,
// Exported for tests.
Expand Down
6 changes: 3 additions & 3 deletions content_scripts/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class SuppressAllKeyboardEvents extends Mode {
name: "suppressAllKeyboardEvents",
suppressAllKeyboardEvents: true
};
super.init(extend(defaults, options));
super.init(Object.assign(defaults, options));
}
}

Expand All @@ -287,7 +287,7 @@ class CacheAllKeydownEvents extends SuppressAllKeyboardEvents {
name: "cacheAllKeydownEvents",
keydown(event) { return keydownEvents.push(event); }
};
super(extend(defaults, options));
super(Object.assign(defaults, options));
this.keydownEvents = [];
}

Expand All @@ -296,4 +296,4 @@ class CacheAllKeydownEvents extends SuppressAllKeyboardEvents {
}
}

extend(global, {Mode, SuppressAllKeyboardEvents, CacheAllKeydownEvents});
Object.assign(global, {Mode, SuppressAllKeyboardEvents, CacheAllKeydownEvents});
4 changes: 2 additions & 2 deletions content_scripts/mode_find.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class FindMode extends Mode {
this.scrollY = window.scrollY;
}

super.init(extend(options, {
super.init(Object.assign(options, {
name: "find",
indicator: false,
exitOnClick: true,
Expand Down Expand Up @@ -211,7 +211,7 @@ class FindMode extends Mode {
// :options is an optional dict. valid parameters are 'caseSensitive' and 'backwards'.
static execute(query, options) {
let result = null;
options = extend({
options = Object.assign({
backwards: false,
caseSensitive: !this.query.ignoreCase,
colorSelection: true
Expand Down
2 changes: 1 addition & 1 deletion content_scripts/mode_insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class InsertMode extends Mode {
keydown: handleKeyEvent
};

super.init(extend(defaults, options));
super.init(Object.assign(defaults, options));

// Only for tests. This gives us a hook to test the status of the permanently-installed instance.
if (this.permanent)
Expand Down
2 changes: 1 addition & 1 deletion content_scripts/mode_key_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class KeyHandlerMode extends Mode {
}

init(options) {
const args = extend(options, {keydown: this.onKeydown.bind(this)});
const args = Object.assign(options, {keydown: this.onKeydown.bind(this)});
super.init(args);

this.commandHandler = options.commandHandler || (function() {});
Expand Down
8 changes: 4 additions & 4 deletions content_scripts/mode_normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NormalMode extends KeyHandlerMode {
commandHandler: this.commandHandler.bind(this)
};

super.init(extend(defaults, options));
super.init(Object.assign(defaults, options));

chrome.storage.local.get("normalModeKeyStateMapping",
(items) => this.setKeyMapping(items.normalModeKeyStateMapping));
Expand Down Expand Up @@ -258,7 +258,7 @@ var NormalModeCommands = {
};

if (typeof LinkHints !== 'undefined') {
extend(NormalModeCommands, {
Object.assign(NormalModeCommands, {
"LinkHints.activateMode": LinkHints.activateMode.bind(LinkHints),
"LinkHints.activateModeToOpenInNewTab": LinkHints.activateModeToOpenInNewTab.bind(LinkHints),
"LinkHints.activateModeToOpenInNewForegroundTab": LinkHints.activateModeToOpenInNewForegroundTab.bind(LinkHints),
Expand All @@ -270,7 +270,7 @@ if (typeof LinkHints !== 'undefined') {
}

if (typeof Vomnibar !== 'undefined') {
extend(NormalModeCommands, {
Object.assign(NormalModeCommands, {
"Vomnibar.activate": Vomnibar.activate.bind(Vomnibar),
"Vomnibar.activateInNewTab": Vomnibar.activateInNewTab.bind(Vomnibar),
"Vomnibar.activateTabSelection": Vomnibar.activateTabSelection.bind(Vomnibar),
Expand All @@ -282,7 +282,7 @@ if (typeof Vomnibar !== 'undefined') {
}

if (typeof Marks !== 'undefined') {
extend(NormalModeCommands, {
Object.assign(NormalModeCommands, {
"Marks.activateCreateMode": Marks.activateCreateMode.bind(Marks),
"Marks.activateGotoMode": Marks.activateGotoMode.bind(Marks)
});
Expand Down
10 changes: 5 additions & 5 deletions content_scripts/mode_visual.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ class VisualMode extends KeyHandlerMode {
} else { // keys.length == 2
if (keyMapping[keys[0]] == null)
keyMapping[keys[0]] = {};
extend(keyMapping[keys[0]], {[keys[1]]: {command: movement}});
Object.assign(keyMapping[keys[0]], {[keys[1]]: {command: movement}});
}
}

// Aliases and complex bindings.
extend(keyMapping, {
Object.assign(keyMapping, {
"B": keyMapping.b,
"W": keyMapping.w,
"<c-e>": {
Expand All @@ -245,7 +245,7 @@ class VisualMode extends KeyHandlerMode {
}
});

super.init(extend(options, {
super.init(Object.assign(options, {
name: options.name != null ? options.name : "visual",
indicator: options.indicator != null ? options.indicator : "Visual mode",
singleton: "visual-mode-group", // Visual mode, visual-line mode and caret mode each displace each other.
Expand Down Expand Up @@ -431,7 +431,7 @@ class VisualLineMode extends VisualMode {
init(options) {
if (options == null)
options = {};
super.init(extend(options, {name: "visual/line", indicator: "Visual mode (line)"}));
super.init(Object.assign(options, {name: "visual/line", indicator: "Visual mode (line)"}));
return this.extendSelection();
}

Expand Down Expand Up @@ -481,7 +481,7 @@ class CaretMode extends VisualMode {
init(options) {
if (options == null)
options = {};
super.init(extend(options, {name: "caret", indicator: "Caret mode", alterMethod: "move"}));
super.init(Object.assign(options, {name: "caret", indicator: "Caret mode", alterMethod: "move"}));

// Establish the initial caret.
switch (DomUtils.getSelectionType(this.selection)) {
Expand Down
2 changes: 1 addition & 1 deletion content_scripts/ui_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class UIComponent {
items => styleSheet.innerHTML = items.vimiumCSSInChromeStorage);

this.iframeElement = DomUtils.createElement("iframe");
extend(this.iframeElement, {
Object.assign(this.iframeElement, {
className,
seamless: "seamless"
});
Expand Down
Loading

0 comments on commit fbe6f7a

Please sign in to comment.