From 53b385a86541d8fb576ece12a1461dd3bbf43dbe Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Sat, 1 Mar 2025 17:30:58 +0100 Subject: [PATCH] Replace lodash by modern JavaScript functions (#20) * Replace lodash by built-in functions * .flatMap -> .flat --- eslint.config.js | 1 + format/products-filter.js | 2 +- index.js | 3 ++- lib/request.js | 2 +- package-lock.json | 2 +- package.json | 1 - parse/polyline.js | 6 +++--- parse/remarks.js | 6 ++---- 8 files changed, 11 insertions(+), 12 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 275cd9fa..1bfdb7f8 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -29,6 +29,7 @@ const config = [ '@stylistic/multiline-comment-style': 'off', '@stylistic/multiline-ternary': ['error', 'always-multiline'], '@stylistic/newline-per-chained-call': ['error', {ignoreChainWithDepth: 1}], + '@stylistic/no-extra-parens': 'off', '@stylistic/no-mixed-operators': 'off', '@stylistic/no-tabs': 'off', '@stylistic/object-property-newline': 'off', diff --git a/format/products-filter.js b/format/products-filter.js index e3362e77..067f011e 100644 --- a/format/products-filter.js +++ b/format/products-filter.js @@ -1,4 +1,4 @@ -import isObj from 'lodash/isObject.js'; +const isObj = element => element !== null && 'object' === typeof element && !Array.isArray(element); const hasProp = (o, k) => Object.prototype.hasOwnProperty.call(o, k); diff --git a/index.js b/index.js index a74a4c0f..b747f8ae 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,10 @@ -import isObj from 'lodash/isObject.js'; import distance from 'gps-distance'; import {defaultProfile} from './lib/default-profile.js'; import {validateProfile} from './lib/validate-profile.js'; +const isObj = element => element !== null && 'object' === typeof element && !Array.isArray(element); + // background info: https://github.com/public-transport/hafas-client/issues/286 const FORBIDDEN_USER_AGENTS = [ 'my-awesome-program', // previously used in readme.md, p/*/readme.md & docs/*.md diff --git a/lib/request.js b/lib/request.js index 81d9b67c..4ff979b1 100644 --- a/lib/request.js +++ b/lib/request.js @@ -15,7 +15,7 @@ if (proxyAddress) { }); getAgent = () => agent; }); -} +} const randomBytesHexString = length => [...Array(length)].map(() => Math.floor(Math.random() * 16) .toString(16)) diff --git a/package-lock.json b/package-lock.json index 95a05002..31f70a75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,6 @@ "db-hafas-stations": "^1.0.0", "gps-distance": "0.0.4", "https-proxy-agent": "^7.0.0", - "lodash": "^4.17.5", "luxon": "^3.1.1", "qs": "^6.6.0", "slugg": "^1.2.0", @@ -4867,6 +4866,7 @@ "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==", + "dev": true, "license": "MIT" }, "node_modules/lodash-es": { diff --git a/package.json b/package.json index 8c065c40..babde088 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,6 @@ "db-hafas-stations": "^1.0.0", "gps-distance": "0.0.4", "https-proxy-agent": "^7.0.0", - "lodash": "^4.17.5", "luxon": "^3.1.1", "qs": "^6.6.0", "slugg": "^1.2.0", diff --git a/parse/polyline.js b/parse/polyline.js index 1a015bdd..0f4ee252 100644 --- a/parse/polyline.js +++ b/parse/polyline.js @@ -1,11 +1,11 @@ -import maxBy from 'lodash/maxBy.js'; - const parsePolyline = (ctx, p) => { // p = raw polylineGroup const desc = p.polylineDescriptions || p.polylineDesc; if (desc.length < 1) { return null; } - const points = maxBy(desc, d => d.coordinates.length).coordinates; // TODO initial and final poly? + const points = desc.reduce((max, d) => (d.coordinates.length > max.coordinates.length ? d : max), + ).coordinates; // TODO: initial and final poly? + if (points.length === 0) { return null; } diff --git a/parse/remarks.js b/parse/remarks.js index cd3d6ae7..a03f1b25 100644 --- a/parse/remarks.js +++ b/parse/remarks.js @@ -1,8 +1,6 @@ -import flatMap from 'lodash/flatMap.js'; - const parseRemarks = (ctx, ref) => { // TODO ereignisZusammenfassung, priorisierteMeldungen? - return flatMap([ + return [ ref.disruptions || [], ref.risNotizen || [], ref.echtzeitNotizen && ref.echtzeitNotizen.map(e => { @@ -18,7 +16,7 @@ const parseRemarks = (ctx, ref) => { ref.attributNotizen || [], ref.attributes || [], ref.verkehrsmittel?.zugattribute || [], - ]) + ].flat() .map(remark => { if (remark.kategorie || remark.priority) { const res = ctx.profile.parseHintByCode(remark);