Skip to content

Commit

Permalink
refactor: replace lodash - step 3: replaces remaining object methods …
Browse files Browse the repository at this point in the history
…has, get and set
  • Loading branch information
sverweij committed Jul 20, 2024
1 parent be85d85 commit 7a8310b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/report/dot/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import prepareFolderLevel from "./prepare-folder-level.mjs";
import prepareCustomLevel from "./prepare-custom-level.mjs";
import prepareFlatLevel from "./prepare-flat-level.mjs";
import { applyFilters } from "#graph-utl/filter-bank.mjs";

import { get } from "#utl/object-util.mjs";

// not importing EOL from "node:os" so output is the same on windows and unices
Expand Down
7 changes: 5 additions & 2 deletions src/report/dot/theming.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import DEFAULT_THEME from "./default-theme.mjs";

import { has, get } from "#utl/object-util.mjs";

function matchesRE(pValue, pRE) {
Expand All @@ -14,10 +13,14 @@ function matchesCriterion(pModuleKey, pCriterion) {

function moduleOrDependencyMatchesCriteria(pSchemeEntry, pModule) {
return Object.keys(pSchemeEntry.criteria).every((pKey) => {
// the keys can have paths in them like {"rules[0].severity": "info"}
// To get the criterion treat that key as a string and not as a path
// eslint-disable-next-line security/detect-object-injection
const lCriterion = pSchemeEntry.criteria[pKey];
// we use _.get here because in the criteria you can enter
// nested keys like "rules[0].severity" : "error", and _.get handles
// that for us
const lCriterion = get(pSchemeEntry.criteria, pKey);
// console.error(pSchemeEntry.criteria, pKey, pc.bold(lCriterion));
const lModuleKey = get(pModule, pKey);

if (!(lModuleKey || has(pModule, pKey))) {
Expand Down
43 changes: 40 additions & 3 deletions src/utl/object-util.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
export { default as has } from "lodash/has.js";
export { default as get } from "lodash/get.js";
export { default as set } from "lodash/set.js";
/* eslint-disable security/detect-object-injection */

export function get(pObject, pPath, pDefault) {
if (!pObject || !pPath) {
return pDefault;
}
// Regex explained: https://regexr.com/58j0k
const lPathArray = pPath.match(/([^[.\]])+/g);

const lReturnValue = lPathArray.reduce((pPreviousObject, pKey) => {
return pPreviousObject && pPreviousObject[pKey];
}, pObject);

if (!lReturnValue) {
return pDefault;
}
return lReturnValue;
}

export function set(pObject, pPath, pValue) {
const lPathArray = pPath.match(/([^[.\]])+/g);

lPathArray.reduce((pPreviousObject, pKey, pIndex) => {
if (pIndex === lPathArray.length - 1) {
pPreviousObject[pKey] = pValue;
} else if (!pPreviousObject[pKey]) {
pPreviousObject[pKey] = {};
}
return pPreviousObject[pKey];
}, pObject);
}

/**
* @param {any} pObject
* @param {string} pPath
* @returns {boolean}
*/
export function has(pObject, pPath) {
return Boolean(get(pObject, pPath));
}
1 change: 1 addition & 0 deletions test/cli/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function setModuleType(pTestPairs, pModuleType) {

function runFileBasedTests(pModuleType) {
setModuleType(TEST_PAIRS, pModuleType).forEach((pPair) => {
// if (pPair.description === "regular dot with default theme")
it(pPair.description, async () => {
const lExitCode = await cli([pPair.dirOrFile], pPair.options);

Expand Down
48 changes: 48 additions & 0 deletions test/utl/object-util.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable no-magic-numbers, no-undefined */
import { equal } from "node:assert/strict";
import { has, get, set } from "#utl/object-util.mjs";

describe("[U] object-util", () => {
describe("[U] has", () => {
it("should return true if the object has the specified path", () => {
const lObject = { a: { b: { c: 123 } } };
equal(has(lObject, "a.b.c"), true);
});

it("should return false if the object does not have the specified path", () => {
const lObject = { a: { b: { c: 123 } } };
equal(has(lObject, "a.b.d"), false);
});
});

describe("[U] get", () => {
it("should return the value at the specified path", () => {
const lObject = { a: { b: { c: 123 } } };
equal(get(lObject, "a.b.c"), 123);
});

it("should return the default value if the path does not exist", () => {
const lObject = { a: { b: { c: 123 } } };
equal(get(lObject, "a.b.d", "default"), "default");
});

it("should return undefined if the path does not exist and no default value is provided", () => {
const lObject = { a: { b: { c: 123 } } };
equal(get(lObject, "a.b.d"), undefined);
});
});

describe("[U] set", () => {
it("should set the value at the specified path", () => {
const lObject = { a: { b: { c: 123 } } };
set(lObject, "a.b.c", 456);
equal(lObject.a.b.c, 456);
});

it("should create nested objects if the path does not exist", () => {
const lObject = {};
set(lObject, "a.b.c", 123);
equal(lObject.a.b.c, 123);
});
});
});

0 comments on commit 7a8310b

Please sign in to comment.