Skip to content

Commit

Permalink
Replace lodash.set (#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore authored Jan 10, 2025
1 parent 398a6af commit 116619d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 40 deletions.
14 changes: 7 additions & 7 deletions bin/browsertime.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node
import merge from 'lodash.merge';
import get from 'lodash.get';
import set from 'lodash.set';
import { getLogger } from '@sitespeed.io/log';
import { existsSync, mkdirSync } from 'node:fs';
import path from 'node:path';
Expand All @@ -15,6 +14,7 @@ import { configure } from '../lib/support/logging.js';
import { parseCommandLine } from '../lib/support/cli.js';
import { StorageManager } from '../lib/support/storageManager.js';
import { loadScript } from '../lib/support/engineUtils.js';
import { setProperty } from '../lib/support/util.js';
import { isAndroidConfigured } from '../lib/android/index.js';

const log = getLogger('browsertime');
Expand Down Expand Up @@ -47,21 +47,21 @@ async function preWarmServer(urls, options) {
const safariDeviceUDID = get(options, 'safari.deviceUDID ');

if (isAndroidConfigured(options) && options.browser === 'chrome') {
set(preWarmOptions, 'chrome.android.package', 'com.android.chrome');
setProperty(preWarmOptions, 'chrome.android.package', 'com.android.chrome');
}
if (chromeDevice) {
set(preWarmOptions, 'chrome.android.deviceSerial', chromeDevice);
setProperty(preWarmOptions, 'chrome.android.deviceSerial', chromeDevice);
} else if (firefoxDevice) {
set(preWarmOptions, 'firefox.android.deviceSerial', firefoxDevice);
setProperty(preWarmOptions, 'firefox.android.deviceSerial', firefoxDevice);
}

if (safariIos) {
set(preWarmOptions, 'safari.ios', true);
setProperty(preWarmOptions, 'safari.ios', true);
if (safariDeviceName) {
set(preWarmOptions, 'safari.deviceName', safariDeviceName);
setProperty(preWarmOptions, 'safari.deviceName', safariDeviceName);
}
if (safariDeviceUDID) {
set(preWarmOptions, 'safari.deviceUDID', safariDeviceUDID);
setProperty(preWarmOptions, 'safari.deviceUDID', safariDeviceUDID);
}
}

Expand Down
34 changes: 17 additions & 17 deletions lib/support/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { readFileSync, statSync } from 'node:fs';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import get from 'lodash.get';
import set from 'lodash.set';
import {
framerate,
crf,
Expand All @@ -14,6 +13,7 @@ import {
import { screenshotDefaults } from '../screenshot/defaults.js';
import { geckoProfilerDefaults } from '../firefox/settings/geckoProfilerDefaults.js';
import { findUpSync } from './fileUtil.js';
import { setProperty } from './util.js';
import { execaSync } from 'execa';

const configPath = findUpSync(['.browsertime.json']);
Expand Down Expand Up @@ -1352,13 +1352,13 @@ export function parseCommandLine() {
argv.browser = 'firefox';
if (argv.android.enabled) {
// TODO add support for Firefox dev
set(
setProperty(
argv,
'firefox.android.package',
argv.firefox.nightly ? 'org.mozilla.fenix' : 'org.mozilla.firefox_beta'
);

set(
setProperty(
argv,
'firefox.android.activity',
'org.mozilla.fenix.IntentReceiverActivity'
Expand All @@ -1367,22 +1367,22 @@ export function parseCommandLine() {
}

if (argv.ios) {
set(argv, 'safari.ios', true);
setProperty(argv, 'safari.ios', true);
} else if (argv.android.enabled) {
if (argv.browser === 'chrome') {
// Default to Chrome Android.
set(
setProperty(
argv,
'chrome.android.package',
get(argv, 'chrome.android.package', 'com.android.chrome')
);
} else if (argv.browser === 'edge') {
set(
setProperty(
argv,
'chrome.android.package',
get(argv, 'chrome.android.package', 'com.microsoft.emmx')
);
set(
setProperty(
argv,
'chrome.android.activity',
get(argv, 'chrome.android.activity', 'com.microsoft.ruby.Main')
Expand All @@ -1391,12 +1391,12 @@ export function parseCommandLine() {
}

if (argv.safari && argv.safari.useSimulator) {
set(argv, 'connectivity.engine', 'throttle');
setProperty(argv, 'connectivity.engine', 'throttle');
}

// Always use hash by default when you configure spa
if (argv.spa) {
set(argv, 'useHash', true);
setProperty(argv, 'useHash', true);
}

if (argv.urlAlias) {
Expand All @@ -1408,28 +1408,28 @@ export function parseCommandLine() {
for (const [index, url] of urls.entries()) {
urlMetaData[url] = argv.urlAlias[index];
}
set(argv, 'urlMetaData', urlMetaData);
setProperty(argv, 'urlMetaData', urlMetaData);
}

// Simplest way to just to get CPU metrics
if (argv.cpu) {
if (argv.browser === 'chrome' || argv.browser === 'edge') {
set(argv, 'chrome.timeline', true);
setProperty(argv, 'chrome.timeline', true);
} else if (argv.browser === 'firefox') {
set(argv, 'firefox.geckoProfiler', true);
setProperty(argv, 'firefox.geckoProfiler', true);
}
}

if (argv.docker) {
set(argv, 'video', get(argv, 'video', true));
set(argv, 'visualMetrics', get(argv, 'visualMetrics', true));
setProperty(argv, 'video', get(argv, 'video', true));
setProperty(argv, 'visualMetrics', get(argv, 'visualMetrics', true));
}

// Set the timeouts to a maximum while debugging
if (argv.debug) {
set(argv, 'timeouts.pageload', 2_147_483_647);
set(argv, 'timeouts.script', 2_147_483_647);
set(argv, 'timeouts.pageCompleteCheck', 2_147_483_647);
setProperty(argv, 'timeouts.pageload', 2_147_483_647);
setProperty(argv, 'timeouts.script', 2_147_483_647);
setProperty(argv, 'timeouts.pageCompleteCheck', 2_147_483_647);
}

return {
Expand Down
10 changes: 6 additions & 4 deletions lib/support/statistics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import get from 'lodash.get';
import set from 'lodash.set';
import { Stats } from 'fast-stats';
import { getLogger } from '@sitespeed.io/log';

import { setProperty } from './util.js';

const log = getLogger('browsertime');

function validateType(value, type, message) {
Expand Down Expand Up @@ -31,7 +33,7 @@ function isNumeric(n) {
function addNumber(target, key, value) {
let stats = get(target, key, new Stats());
stats.push(value);
set(target, key, stats);
setProperty(target, key, stats);
}

export class Statistics {
Expand Down Expand Up @@ -147,7 +149,7 @@ export class Statistics {
let name = percentileName(p);
node[name] = Number.parseFloat(stats.percentile(p).toFixed(decimals));
}
set(results, [key], node);
setProperty(results, [key], node);

return results;
}, {});
Expand Down Expand Up @@ -199,7 +201,7 @@ export class Statistics {

function summarizeRecursive(target, keyPrefix, data) {
if (data instanceof Stats) {
set(target, keyPrefix, summarize(data));
setProperty(target, keyPrefix, summarize(data));
} else if (typeof data === 'object') {
for (const key of Object.keys(data)) {
const path = [...keyPrefix, key];
Expand Down
24 changes: 24 additions & 0 deletions lib/support/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,27 @@ function groupBy(array, property) {
}
return grouped;
}

export function setProperty(object, path, value) {
if (typeof path === 'string') {
path = path.split('.');
}

if (!Array.isArray(path) || path.length === 0) {
return;
}

let current = object;

for (let index = 0; index < path.length - 1; index++) {
const key = path[index];

if (current[key] === undefined) {
current[key] = {};
}

current = current[key];
}

current[path.at(-1)] = value;
}
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"ff-test-bidi-har-export": "0.0.17",
"lodash.get": "4.4.2",
"lodash.merge": "4.6.2",
"lodash.set": "4.3.2",
"selenium-webdriver": "4.27.0",
"yargs": "17.7.2"
},
Expand Down

0 comments on commit 116619d

Please sign in to comment.