-
-
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 <[email protected]>
- Loading branch information
1 parent
a725570
commit 87b7a09
Showing
6 changed files
with
212 additions
and
7 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,41 @@ | ||
/** | ||
* @file Test Fixtures - Person | ||
* @module tests/fixtures/Person | ||
*/ | ||
|
||
/** | ||
* Object representing a person. | ||
*/ | ||
interface Person { | ||
/** | ||
* Person's age. | ||
*/ | ||
age: number | ||
|
||
/** | ||
* Person's friends. | ||
*/ | ||
friends?: Person[] | ||
|
||
/** | ||
* Object representing a person's name. | ||
*/ | ||
name: { | ||
/** | ||
* First name. | ||
*/ | ||
first: string | ||
|
||
/** | ||
* Last name. | ||
*/ | ||
last: string | ||
|
||
/** | ||
* Middle name. | ||
*/ | ||
middle?: string | ||
} | ||
} | ||
|
||
export type { Person as default } |
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,22 @@ | ||
/** | ||
* @file Type Tests - get | ||
* @module tutils/utils/tests/unit-d/get | ||
*/ | ||
|
||
import type Person from '#fixtures/person.interface' | ||
import type { EmptyString, Get } from '#src/types' | ||
import type testSubject from '../get' | ||
|
||
describe('unit-d:utils/get', () => { | ||
it('should return Get<T, P, F>', () => { | ||
// Arrange | ||
type T = Person | ||
type P = 'friends.0.name.middle' | ||
type F = EmptyString | ||
|
||
// Expect | ||
expectTypeOf<typeof testSubject<T, P, F>>().returns.toEqualTypeOf< | ||
Get<T, P, F> | ||
>() | ||
}) | ||
}) |
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,62 @@ | ||
/** | ||
* @file Unit Tests - get | ||
* @module tutils/utils/tests/unit/get | ||
*/ | ||
|
||
import type Person from '#fixtures/person.interface' | ||
import type { EmptyString } from '#src/types' | ||
import testSubject from '../get' | ||
|
||
describe('unit:utils/get', () => { | ||
let person: Person | ||
|
||
beforeAll(() => { | ||
person = { | ||
age: faker.number.int({ max: 25, min: 18 }), | ||
friends: [ | ||
{ | ||
age: faker.number.int({ max: 25, min: 18 }), | ||
name: { | ||
first: faker.person.firstName(), | ||
last: faker.person.lastName() | ||
} | ||
} | ||
], | ||
name: { | ||
first: faker.person.firstName(), | ||
last: faker.person.lastName(), | ||
middle: faker.person.middleName() | ||
} | ||
} | ||
}) | ||
|
||
it('should return fallback if indexed value is undefined', () => { | ||
// Arrange | ||
const fallback: EmptyString = '' | ||
|
||
// Act | ||
const result = testSubject(person, 'friends.0.name.middle', fallback) | ||
|
||
// Expect | ||
expect(result).to.deep.equal(fallback) | ||
}) | ||
|
||
it('should return dynamically indexed value', () => { | ||
// Arrange | ||
const cases: [...Parameters<typeof testSubject>, unknown][] = [ | ||
['person', 0, undefined, 'p'], | ||
[null, 'age', undefined, null], | ||
[person, 'age', undefined, person.age], | ||
[person, 'age.', undefined, person.age], | ||
[person, 'friends', undefined, person.friends], | ||
[person, 'friends.0.name.last', undefined, person.friends![0]!.name.last], | ||
[person, 'name.first', undefined, person.name.first], | ||
[person, 'name.first.-1', undefined, person.name.first.at(-1)] | ||
] | ||
|
||
// Act + Expect | ||
cases.forEach(([value, path, fallback, expected]) => { | ||
expect(testSubject(value, path, fallback)).to.deep.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,74 @@ | ||
/** | ||
* @file Utilities - get | ||
* @module tutils/utils/get | ||
*/ | ||
|
||
import type { Get, NumberString } from '#src/types' | ||
import at from './at' | ||
import cast from './cast' | ||
import isArray from './is-array' | ||
import isEmptyString from './is-empty-string' | ||
import isNIL from './is-nil' | ||
import isNumeric from './is-numeric' | ||
import isString from './is-string' | ||
import isUndefined from './is-undefined' | ||
import trim from './trim' | ||
|
||
/** | ||
* Dynamically indexes `data` at `path`. | ||
* | ||
* If the indexed value is `undefined`, `fallback` will be returned instead. | ||
* | ||
* Supports dot-notation for nested paths and array indexing. | ||
* | ||
* @template T - Value to index | ||
* @template P - Index path | ||
* @template F - Fallback value type | ||
* | ||
* @param {T} data - Value to index | ||
* @param {P} path - Index path | ||
* @param {F} [fallback] - Fallback value | ||
* @return {Get<T, P, F>} Dynamically indexed value or `fallback` | ||
*/ | ||
function get<T, P extends NumberString, F = undefined>( | ||
data: T, | ||
path: P, | ||
fallback?: F | ||
): Get<T, P, F> { | ||
/** | ||
* Path segments. | ||
* | ||
* @const {string[]} segments | ||
*/ | ||
const segments: string[] = `${path}`.split(/[.[\]]/g).map(trim) | ||
|
||
/** | ||
* Dynamically indexed value. | ||
* | ||
* @var {unknown} value | ||
*/ | ||
let value: unknown = data | ||
|
||
// dynamically index data | ||
for (const key of segments) { | ||
// exit early if indexed value is null or undefined | ||
if (isNIL(value)) break | ||
|
||
// do nothing if key is an empty string | ||
if (isEmptyString(key)) continue | ||
|
||
// reset indexed value | ||
switch (true) { | ||
case (isArray(value) || isString(value)) && isNumeric(key): | ||
value = at(cast<string | readonly unknown[]>(value), +key) | ||
break | ||
default: | ||
value = cast<Record<string, any>>(value)[key] | ||
break | ||
} | ||
} | ||
|
||
return (isUndefined(value) ? fallback : value) as Get<T, P, F> | ||
} | ||
|
||
export default get |
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