Skip to content

Commit

Permalink
Replace lodash.reduce (#4398)
Browse files Browse the repository at this point in the history
* Replace lodash.reduce
  • Loading branch information
soulgalore authored Jan 9, 2025
1 parent c53ad53 commit 6a0889b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 86 deletions.
18 changes: 6 additions & 12 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { readFileSync, statSync } from 'node:fs';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import merge from 'lodash.merge';
import reduce from 'lodash.reduce';
import set from 'lodash.set';
import get from 'lodash.get';

Expand Down Expand Up @@ -2136,14 +2135,10 @@ export async function parseCommandLine() {
argv = parsed.argv;

// aliases are long options -> short option
const aliasLookup = reduce(
aliases,
(lookup, value, key) => {
lookup.set(value[0], key);
return lookup;
},
new Map()
);
const aliasLookup = new Map();
for (const [key, value] of Object.entries(aliases)) {
aliasLookup.set(value[0], key);
}

let explicitOptions = yargs(hideBin(process.argv)).argv;

Expand All @@ -2152,9 +2147,8 @@ export async function parseCommandLine() {
yargsInstance.getOptions().configObjects[0]
);

explicitOptions = reduce(
explicitOptions,
(result, value, key) => {
explicitOptions = Object.entries(explicitOptions).reduce(
(result, [key, value]) => {
if (aliasLookup.has(key)) {
const fullKey = aliasLookup.get(key);
result = set(result, fullKey, value);
Expand Down
16 changes: 7 additions & 9 deletions lib/plugins/domains/aggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { parse } from 'node:url';

import { Stats } from 'fast-stats';
import { getLogger } from '@sitespeed.io/log';
import reduce from 'lodash.reduce';

import { summarizeStats } from '../../support/statsHelpers.js';
import { isEmpty } from '../../support/util.js';

const log = getLogger('sitespeedio.plugin.domains');

Expand Down Expand Up @@ -39,22 +37,22 @@ function getDomain(domainName) {
}

function calc(domains) {
return reduce(
domains,
(summary, domainStats, domainName) => {
return Object.entries(domains).reduce(
(summary, [domainName, domainStats]) => {
const domainSummary = {
requestCount: domainStats.requestCount,
domainName
};

const stats = summarizeStats(domainStats.totalTime);
if (!isEmpty(stats)) {
if (stats && Object.keys(stats).length > 0) {
domainSummary.totalTime = stats;
}

for (const name of timingNames) {
const stats = summarizeStats(domainStats[name]);
if (!isEmpty(stats)) {
domainSummary[name] = stats;
const stat = summarizeStats(domainStats[name]);
if (stat && Object.keys(stat).length > 0) {
domainSummary[name] = stat;
}
}

Expand Down
30 changes: 13 additions & 17 deletions lib/plugins/graphite/data-generator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// eslint-disable-next-line unicorn/import-style
import util, { format } from 'node:util';
import reduce from 'lodash.reduce';
import {
getConnectivity,
toSafeKey,
Expand Down Expand Up @@ -65,7 +64,7 @@ export class GraphiteDataGenerator {
}

dataFromMessage(message, time, alias) {
let timestamp = Math.round(time.valueOf() / 1000);
const timestamp = Math.round(time.valueOf() / 1000);

const keypath = keyPathFromMessage(
message,
Expand All @@ -74,20 +73,19 @@ export class GraphiteDataGenerator {
alias
);

return reduce(
flattenMessageData(message),
(entries, value, key) => {
return Object.entries(flattenMessageData(message)).reduce(
(entries, [key, value]) => {
if (message.type === 'browsertime.run') {
if (key.includes('timings') && key.includes('marks')) {
key = key.replace(/marks\.(\d+)/, function (match, index) {
key = key.replace(/marks\.(\d+)/, (match, index) => {
return (
'marks.' + message.data.timings.userTimings.marks[index].name
);
});
}

if (key.includes('timings') && key.includes('measures')) {
key = key.replace(/measures\.(\d+)/, function (match, index) {
key = key.replace(/measures\.(\d+)/, (match, index) => {
return (
'measures.' +
message.data.timings.userTimings.measures[index].name
Expand All @@ -96,20 +94,18 @@ export class GraphiteDataGenerator {
}
}
if (message.type === 'pagexray.run' && key.includes('assets')) {
key = key.replace(
/assets\.(\d+)/,
function (match, index) {
let url = new URL(message.data.assets[index].url);
url.search = '';
return 'assets.' + toSafeKey(url.toString());
},
{}
);
key = key.replace(/assets\.(\d+)/, (match, index) => {
const url = new URL(message.data.assets[index].url);
url.search = '';
return 'assets.' + toSafeKey(url.toString());
});
}

const fullKey = format('%s.%s.%s', this.namespace, keypath, key);
const arguments_ = [formatEntry(this.entryFormat), fullKey, value];
this.entryFormat === GRAPHITE && arguments_.push(timestamp);
if (this.entryFormat === GRAPHITE) {
arguments_.push(timestamp);
}

entries.push(format.apply(util, arguments_));
return entries;
Expand Down
22 changes: 8 additions & 14 deletions lib/plugins/html/dataCollector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import merge from 'lodash.merge';
import get from 'lodash.get';
import reduce from 'lodash.reduce';
import set from 'lodash.set';

export class DataCollector {
Expand Down Expand Up @@ -51,9 +50,8 @@ export class DataCollector {
}

getWorkingUrls() {
return reduce(
this.urlPages,
(validPages, urlInfo, url) => {
return Object.entries(this.urlPages).reduce(
(validPages, [url, urlInfo]) => {
if (Object.keys(urlInfo.data).length > 0) {
validPages[url] = urlInfo;
}
Expand All @@ -64,16 +62,12 @@ export class DataCollector {
}

getErrorUrls() {
return reduce(
this.urlPages,
(errors, urlInfo, url) => {
if (urlInfo.errors) {
errors[url] = urlInfo.errors;
}
return errors;
},
{}
);
return Object.entries(this.urlPages).reduce((errors, [url, urlInfo]) => {
if (urlInfo.errors) {
errors[url] = urlInfo.errors;
}
return errors;
}, {});
}

getErrors() {
Expand Down
7 changes: 2 additions & 5 deletions lib/plugins/html/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import get from 'lodash.get';
import set from 'lodash.set';
import reduce from 'lodash.reduce';
import { SitespeedioPlugin } from '@sitespeed.io/plugin';
import { DataCollector } from './dataCollector.js';
import { HTMLBuilder } from './htmlBuilder.js';
Expand Down Expand Up @@ -115,8 +114,7 @@ export default class HTMLPlugin extends SitespeedioPlugin {

case 'aggregateassets.summary': {
if (message.group === 'total') {
const assetList = reduce(
message.data,
const assetList = Object.values(message.data).reduce(
(assetList, asset) => {
assetList.push(asset);
return assetList;
Expand Down Expand Up @@ -179,8 +177,7 @@ export default class HTMLPlugin extends SitespeedioPlugin {

case 'domains.summary': {
if (message.group === 'total') {
const domainList = reduce(
message.data,
const domainList = Object.values(message.data).reduce(
(domainList, domainStats) => {
domainList.push(domainStats);
return domainList;
Expand Down
14 changes: 6 additions & 8 deletions lib/plugins/influxdb/data-generator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import merge from 'lodash.merge';
import reduce from 'lodash.reduce';

import { flattenMessageData } from '../../support/flattenMessage.js';
import {
Expand Down Expand Up @@ -236,21 +235,20 @@ export class InfluxDBDataGenerator {

return tags;
}
return reduce(
flattenMessageData(message),
(entries, value, key) => {
return Object.entries(flattenMessageData(message)).reduce(
(entries, [key, value]) => {
const fieldAndSeriesName = getFieldAndSeriesName(key);
let tags = getTagsFromMessage(
message,
this.includeQueryParams,
this.options,
this.defaultTags
);
tags = merge(getAdditionalTags(key, message.type), tags);
let point = {
time: time.valueOf()
tags = { ...getAdditionalTags(key, message.type), ...tags };
const point = {
time: time.valueOf(),
[fieldAndSeriesName.field]: value
};
point[fieldAndSeriesName.field] = value;
entries.push({
tags,
seriesName: fieldAndSeriesName.seriesName,
Expand Down
22 changes: 8 additions & 14 deletions lib/support/metricsFilter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import get from 'lodash.get';
import set from 'lodash.set';
import merge from 'lodash.merge';
import reduce from 'lodash.reduce';

import { toArray, isEmpty } from './util.js';

Expand Down Expand Up @@ -30,20 +29,15 @@ export function filterMetrics(json, metricPaths) {
} else if (firstWildcard === 0) {
const leafPath = path.slice(2);

reduce(
json,
(result, value, key) => {
if (typeof value === 'object') {
const leaf = this.filterMetrics(value, leafPath);

if (!isEmpty(leaf)) {
result[key] = leaf;
}
Object.entries(json).reduce((result, [key, value]) => {
if (typeof value === 'object') {
const leaf = this.filterMetrics(value, leafPath);
if (leaf && Object.keys(leaf).length > 0) {
result[key] = leaf;
}
return result;
},
result
);
}
return result;
}, result);
} else {
let branchPath = path.slice(0, Math.max(0, firstWildcard));
if (branchPath.endsWith('.')) branchPath = branchPath.slice(0, -1);
Expand Down
6 changes: 0 additions & 6 deletions npm-shrinkwrap.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 @@ -99,7 +99,6 @@
"junit-report-builder": "3.2.1",
"lodash.get": "4.4.2",
"lodash.merge": "4.6.2",
"lodash.reduce": "4.6.0",
"lodash.set": "4.3.2",
"markdown": "0.5.0",
"node-scp": "0.0.23",
Expand Down

0 comments on commit 6a0889b

Please sign in to comment.