Skip to content

Commit

Permalink
feat: support Intl.Locale instance checking (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Sep 19, 2023
1 parent 9aba4c7 commit e1cb3e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions deno/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../src/index.ts'
13 changes: 13 additions & 0 deletions src/index.ts
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]'
}
12 changes: 12 additions & 0 deletions test/index.test.ts
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)
})
})

0 comments on commit e1cb3e0

Please sign in to comment.