diff --git a/deno/mod.ts b/deno/mod.ts new file mode 100644 index 0000000..fc5653f --- /dev/null +++ b/deno/mod.ts @@ -0,0 +1 @@ +export * from '../src/index.ts' diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..0e9b1b3 --- /dev/null +++ b/src/index.ts @@ -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]' +} diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..ad5cc2a --- /dev/null +++ b/test/index.test.ts @@ -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) + }) +})