-
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.
refactor: replace lodash - step 3: replaces remaining object methods …
…has, get and set
- Loading branch information
Showing
5 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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)); | ||
} |
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,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); | ||
}); | ||
}); | ||
}); |