-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
ea9bb62
commit 23ee270
Showing
3 changed files
with
52 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @file Unit Tests - getPropertyValue | ||
* @module tsconfig-utils/internal/tests/unit/getPropertyValue | ||
*/ | ||
|
||
import testSubject from '../get-property-value' | ||
|
||
describe('unit:internal/getPropertyValue', () => { | ||
it('should return extracted property value', () => { | ||
// Arrange | ||
const cases: [...Parameters<typeof testSubject>, unknown][] = [ | ||
[null, 'property', undefined], | ||
[{ target: 'es2020' }, 'target', 'es2020'] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([object, property, expected]) => { | ||
expect(testSubject(object, property)).to.equal(expected) | ||
}) | ||
}) | ||
}) |
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,30 @@ | ||
/** | ||
* @file Internal - getPropertyValue | ||
* @module tsconfig-utils/internal/getPropertyValue | ||
*/ | ||
|
||
import type { Nilable, ObjectUnknown } from '@flex-development/tutils' | ||
|
||
/** | ||
* Extracts a property value from the given `object`. | ||
* | ||
* Returns `undefined` if the given `property` is not defined. | ||
* | ||
* **Note**: If `object` is `null` or `undefined`, it will be converted to an | ||
* empty object first. | ||
* | ||
* @template T - Object type | ||
* @template K - Object property name | ||
* | ||
* @param {Nilable<T>} object - Object to evaluate, `null`, or `undefined` | ||
* @param {string} property - Object property name | ||
* @return {T[K] | undefined} Property value or `undefined` | ||
*/ | ||
function getPropertyValue<T extends ObjectUnknown, K extends string = string>( | ||
object: Nilable<T>, | ||
property: K | ||
): T[K] | undefined { | ||
return Object.getOwnPropertyDescriptor(object ?? {}, property)?.value as T[K] | ||
} | ||
|
||
export default getPropertyValue |
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