-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description - [x] centralizes remaining uses of lodash in central utilities - [x] in the central utilities replace the lodash array functions with native alternatives that work in our context - [x] in the central utilities replace the lodash object functions with native alternatives that work in our context - [x] removes lodash from the package manifest ## Motivation and Context lodash is a great library, however - it's not really maintained anymore - it has fairly large download size (1.4Mb [according to pkg-size](https://pkg-size.dev/lodash)) - while we only use a small subset of it ## How Has This Been Tested? - [x] green ci ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] Documentation only change - [x] Refactor (non-breaking change which fixes an issue without changing functionality) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist - [x] 📖 - My change doesn't require a documentation update, or ... - it _does_ and I have updated it - [x] ⚖️ - The contribution will be subject to [The MIT license](https://github.com/sverweij/dependency-cruiser/blob/main/LICENSE), and I'm OK with that. - The contribution is my own original work. - I am ok with the stuff in [**CONTRIBUTING.md**](https://github.com/sverweij/dependency-cruiser/blob/main/.github/CONTRIBUTING.md).
- Loading branch information
Showing
16 changed files
with
137 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* 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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |