-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getMeasureKind() function for validating inputs
Co-authored-by: Jonah Snider <jonah@jonahsnider.com>
- Loading branch information
1 parent
9f2a97d
commit dd535cc
Showing
5 changed files
with
75 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { MeasureKind } from '../conversions/types'; | ||
import { getMeasureKind } from './get-measure-kind'; | ||
|
||
describe('get measure kind', () => { | ||
test('returns measure kind for valid units', () => { | ||
expect(getMeasureKind('m')).toBe(MeasureKind.Length); | ||
expect(getMeasureKind('s')).toBe(MeasureKind.Time); | ||
expect(getMeasureKind('kg')).toBe(MeasureKind.Mass); | ||
}); | ||
|
||
test('returns undefined on invalid units', () => { | ||
expect(getMeasureKind('invalid')).toBeUndefined(); | ||
expect(getMeasureKind('__proto__')).toBeUndefined(); | ||
}); | ||
}); |
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,50 @@ | ||
import type { MeasureKind } from '../conversions/types'; | ||
import { unitsObject } from '../generated/parse-unit'; | ||
import type { MeasureKindByUnit, Unit } from '../types/units'; | ||
|
||
/** | ||
* Get the {@link MeasureKind} associated with a unit. | ||
* | ||
* @example | ||
* ```ts | ||
* getMeasure('m'); // MeasureKind.Length | ||
* ``` | ||
* @example | ||
* ```ts | ||
* getMeasure('invalid'); // undefined | ||
* ``` | ||
* | ||
* @param unit - The unit you want to get the measure kind of | ||
* @returns The {@link MeasureKind} corresponding to this unit of measure, or `undefined` if the unit is invalid | ||
* | ||
* @public | ||
*/ | ||
export function getMeasureKind<U extends Unit>(unit: U): MeasureKindByUnit<U>; | ||
/** | ||
* Get the {@link MeasureKind} associated with a unit. | ||
* | ||
* @example | ||
* ```ts | ||
* getMeasure('m'); // MeasureKind.Length | ||
* ``` | ||
* @example | ||
* ```ts | ||
* getMeasure('invalid'); // undefined | ||
* ``` | ||
* | ||
* @param unit - The unit you want to get the measure kind of | ||
* @returns The {@link MeasureKind} corresponding to this unit of measure, or `undefined` if the unit is invalid | ||
* | ||
* @public | ||
*/ | ||
export function getMeasureKind(unit: string): MeasureKind | undefined; | ||
export function getMeasureKind<U extends Unit>(unit: U): MeasureKindByUnit<U> | undefined { | ||
const unitObject = unitsObject[unit]; | ||
|
||
if (unitObject) { | ||
return unitObject[0] as MeasureKindByUnit<U>; | ||
} | ||
|
||
// Needed to appease tsc | ||
return; | ||
} |
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