-
-
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.
feat: support
Intl.Locale
instance checking (#1)
- Loading branch information
Showing
3 changed files
with
26 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 @@ | ||
export * from '../src/index.ts' |
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,13 @@ | ||
const objectToString = Object.prototype.toString | ||
const toTypeString = (value: unknown): string => objectToString.call(value) | ||
|
||
/** | ||
* check whether the value is a {@link Intl.Locale} instance | ||
* | ||
* @param {unknown} val The locale value | ||
* | ||
* @returns {boolean} Returns `true` if the value is a {@link Intl.Locale} instance, else `false`. | ||
*/ | ||
export function isLocale(val: unknown): val is Intl.Locale { | ||
return toTypeString(val) === '[object Intl.Locale]' | ||
} |
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,12 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { isLocale } from '../src/index.ts' | ||
|
||
describe('isLocale', () => { | ||
test('Locale instance', () => { | ||
expect(isLocale(new Intl.Locale('en-US'))).toBe(true) | ||
}) | ||
|
||
test('not Locale instance', () => { | ||
expect(isLocale('en-US')).toBe(false) | ||
}) | ||
}) |