Skip to content

Commit

Permalink
Enum#firstValue
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanPB committed Jan 20, 2022
1 parent 0146235 commit cb062c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,32 @@ export function valueIsSpecificKey<T>(enumerator: Enumerator<T>, key: keyof T, v
* @param search The value to be searched within the enum's values.
* @param comparator Function used to check the equality between `search` and the enum values. Default is a strict equality check (a === b).
*/
export function firstKeyWithValueOf<T, V>(
enumerator: Enumerator<T, V>,
export function firstKeyWithValueOf<K, V>(
enumerator: Enumerator<K, V>,
search: V | any,
comparator: (search: V, enumValue: V)=>boolean = (search, enumValue) => search === enumValue
): keyof T {
): keyof K {
const [key] = firstOrNull(Object.entries(enumerator), ([_, v]) => comparator(search, v))
?? throwExpr(new ValueNotFoundError(search, enumerator))

return key as keyof T
return key as keyof K
}

/**
* Casts a given string value into a TS comprehensible type
*
* Usually a shorthand for Enum[findFirstKeyWithValue(Enum, 'foo')]
*
* @param enumerator The enum.
* @param search The value to be searched within the enum's values.
* @param comparator Function used to check the equality between `search` and the enum values. Default is a strict equality check (a === b).
*/
export function firstValue<K, V>(
enumerator: Enumerator<K, V>,
search: V | any,
comparator: (search: V, enumValue: V)=>boolean = (search, enumValue) => search === enumValue
) : V {
return enumerator[firstKeyWithValueOf(enumerator, search, comparator)]
}

/**
Expand Down
18 changes: 17 additions & 1 deletion test/benchmark/enum.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import {benchmarkSuite} from "@nathanpb/jest-bench";
import {
firstKeyWithValueOf,
firstValue,
isKeyOf,
isValueOf,
keys,
Expand Down Expand Up @@ -54,5 +56,19 @@ benchmarkSuite('#valueIsSpecificKey', {
['best']: () => void valueIsSpecificKey(TestEnum, 'DEPOSIT', TestEnum.DEPOSIT),
['avg']: () => void valueIsSpecificKey(TestEnum, 'BAR', TestEnum.BAR),
['worst']: () => void valueIsSpecificKey(TestEnum, 'BOANOITE', TestEnum.BOANOITE),
['case not found']: () => { try { valueIsSpecificKey(TestEnum, 'BOANOITE', 'not container here') } catch(_) {} }
['case not found']: () => void valueIsSpecificKey(TestEnum, 'BOANOITE', 'not container here')
})

benchmarkSuite('#firstKeyWithValueOf', {
['best']: () => void firstKeyWithValueOf(TestEnum, TestEnum.DEPOSIT),
['avg']: () => void firstKeyWithValueOf(TestEnum, TestEnum.BAR),
['worst']: () => void firstKeyWithValueOf(TestEnum, TestEnum.BOANOITE),
['case not found']: () => { try { firstKeyWithValueOf(TestEnum, 'not container here') } catch(_) {} }
})

benchmarkSuite('#firstValue', {
['best']: () => void firstValue(TestEnum, TestEnum.DEPOSIT),
['avg']: () => void firstValue(TestEnum, TestEnum.BAR),
['worst']: () => void firstValue(TestEnum, TestEnum.BOANOITE),
['case not found']: () => { try { firstValue(TestEnum, 'not container here') } catch(_) {} }
})
16 changes: 16 additions & 0 deletions test/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import {
firstKeyWithValueOf,
firstValue,
isKeyOf,
isValueOf,
keys,
Expand Down Expand Up @@ -101,6 +102,21 @@ describe('#firstKeyWithValueOf', () => {
)
})

describe('#firstValue', () => {
it('Should be DEPOSIT', () =>
expect(firstValue(TestEnum, '00')).toEqual('00')
)

it('Should be WITHDRAW', () =>
expect(firstValue(TestEnum, '01')).toEqual('01')
)

it('Should throw because the value was not found', () =>
expect(() => firstValue(TestEnum, '02'))
.toThrowError(ValueNotFoundError)
)
})

describe('#regexKeyMatcher', () => {
describe('strict', () => {
const regex = regexKeyMatcher(TestEnum)
Expand Down

0 comments on commit cb062c4

Please sign in to comment.