Skip to content

Commit

Permalink
feat(internal): getPropertyValue
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Feb 6, 2023
1 parent ea9bb62 commit 23ee270
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/internal/__tests__/get-property-value.spec.ts
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)
})
})
})
30 changes: 30 additions & 0 deletions src/internal/get-property-value.ts
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
1 change: 1 addition & 0 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module tsconfig-utils/internal
*/

export { default as getPropertyValue } from './get-property-value'
export { default as isDirectory } from './is-directory'
export { default as isFile } from './is-file'
export { default as parseJSON } from './parse-json'
Expand Down

0 comments on commit 23ee270

Please sign in to comment.