From 116619d6e881dec129e86cbd10d5e878b28cbf65 Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Fri, 10 Jan 2025 16:28:41 +0100 Subject: [PATCH] Replace lodash.set (#2250) --- bin/browsertime.js | 14 +++++++------- lib/support/cli.js | 34 +++++++++++++++++----------------- lib/support/statistics.js | 10 ++++++---- lib/support/util.js | 24 ++++++++++++++++++++++++ package-lock.json | 11 ----------- package.json | 1 - 6 files changed, 54 insertions(+), 40 deletions(-) diff --git a/bin/browsertime.js b/bin/browsertime.js index f6f675f7f..eef8c61fe 100755 --- a/bin/browsertime.js +++ b/bin/browsertime.js @@ -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'; @@ -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'); @@ -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); } } diff --git a/lib/support/cli.js b/lib/support/cli.js index c54a93155..ff1d1dccc 100644 --- a/lib/support/cli.js +++ b/lib/support/cli.js @@ -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, @@ -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']); @@ -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' @@ -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') @@ -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) { @@ -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 { diff --git a/lib/support/statistics.js b/lib/support/statistics.js index ed3e5e98d..85dae7453 100644 --- a/lib/support/statistics.js +++ b/lib/support/statistics.js @@ -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) { @@ -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 { @@ -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; }, {}); @@ -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]; diff --git a/lib/support/util.js b/lib/support/util.js index e0680e2c3..1d79f225f 100644 --- a/lib/support/util.js +++ b/lib/support/util.js @@ -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; +} diff --git a/package-lock.json b/package-lock.json index 6b57b05ed..d5f9e37ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,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" }, @@ -4289,11 +4288,6 @@ "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==" }, - "node_modules/lodash.set": { - "version": "4.3.2", - "resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz", - "integrity": "sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=" - }, "node_modules/lower-case": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-2.0.2.tgz", @@ -9895,11 +9889,6 @@ "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==" }, - "lodash.set": { - "version": "4.3.2", - "resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz", - "integrity": "sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=" - }, "lower-case": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/lower-case/-/lower-case-2.0.2.tgz", diff --git a/package.json b/package.json index 6edee1fc7..fb02711ef 100644 --- a/package.json +++ b/package.json @@ -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" },