Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Oct 19, 2023
1 parent e92abd9 commit a4dff1e
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 119 deletions.
21 changes: 6 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ If you are directly using `@intlify/utils` as a dependency:
}
```

**Note:** Make sure to recreate lockfile and `node_modules` after reinstall to
avoid hoisting issues.
**Note:** Make sure to recreate lockfile and `node_modules` after reinstall to avoid hoisting issues.

</details>

Expand Down Expand Up @@ -148,29 +147,21 @@ You can do `import { ... } from '@intlify/utils'` the above utilities
- `getPathLocale`
- `getQueryLocale`

The about utilies functions accpet Web APIs such as
[Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) and
[Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) that is
supported by JS environments (such as Deno, Bun, and Browser)
The about utilies functions accpet Web APIs such as [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) that is supported by JS environments (such as Deno, Bun, and Browser)

#### Specialized environments

If you will use Node.js and H3, You can do
`import { ... } from '@intlify/utils/{ENV}'` the above utilities.
If you will use Node.js and H3, You can do `import { ... } from '@intlify/utils/{ENV}'` the above utilities.

The namespace `{ENV}` is one of the following:

- `node`: accpet `IncomingMessage` and `Outgoing` by Node.js
[http](https://nodejs.org/api/http.html) module
- `node`: accpet `IncomingMessage` and `Outgoing` by Node.js [http](https://nodejs.org/api/http.html) module
- `h3`: accept `H3Event` by HTTP framework [h3](https://github.com/unjs/h3)
- `hono`: accept `Context` by edge-side web framework [hono](https://github.com/honojs/hono)

## 🙌 Contributing guidelines

If you are interested in contributing to `@intlify/utils`, I highly recommend
checking out [the contributing guidelines](/CONTRIBUTING.md) here. You'll find
all the relevant information such as
[how to make a PR](/CONTRIBUTING.md#pull-request-guidelines),
[how to setup development](/CONTRIBUTING.md#development-setup)) etc., there.
If you are interested in contributing to `@intlify/utils`, I highly recommend checking out [the contributing guidelines](/CONTRIBUTING.md) here. You'll find all the relevant information such as [how to make a PR](/CONTRIBUTING.md#pull-request-guidelines), [how to setup development](/CONTRIBUTING.md#development-setup)) etc., there.

## ©️ License

Expand Down
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"include": [
],
*/
"exclude": ["node_modules", "dist"],
"lineWidth": 100,
"exclude": ["node_modules", "dist", "**/*.md"],
"semiColons": false,
"singleQuote": true
},
Expand Down
17 changes: 3 additions & 14 deletions src/h3.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ACCEPT_LANGUAGE_HEADER,
DEFAULT_COOKIE_NAME,
DEFAULT_LANG_TAG,
} from './constants.ts'
import { ACCEPT_LANGUAGE_HEADER, DEFAULT_COOKIE_NAME, DEFAULT_LANG_TAG } from './constants.ts'
import {
getHeaderLanguagesWithGetter,
getLocaleWithGetter,
Expand All @@ -16,12 +12,7 @@ import { pathLanguageParser } from './shared.ts'
import { getCookie, getHeaders, getRequestURL, setCookie } from 'h3'

import type { H3Event } from 'h3'
import type {
CookieOptions,
HeaderOptions,
PathOptions,
QueryOptions,
} from './http.ts'
import type { CookieOptions, HeaderOptions, PathOptions, QueryOptions } from './http.ts'

/**
* get languages from header
Expand Down Expand Up @@ -164,9 +155,7 @@ export function getHeaderLocale(
parser = parseDefaultHeader,
}: HeaderOptions & { lang?: string } = {},
): Intl.Locale {
return getLocaleWithGetter(() =>
getHeaderLanguages(event, { name, parser })[0] || lang
)
return getLocaleWithGetter(() => getHeaderLanguages(event, { name, parser })[0] || lang)
}

/**
Expand Down
10 changes: 2 additions & 8 deletions src/hono.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
ACCEPT_LANGUAGE_HEADER,
DEFAULT_COOKIE_NAME,
DEFAULT_LANG_TAG,
} from './constants.ts'
import { ACCEPT_LANGUAGE_HEADER, DEFAULT_COOKIE_NAME, DEFAULT_LANG_TAG } from './constants.ts'
import {
getHeaderLanguagesWithGetter,
getLocaleWithGetter,
Expand Down Expand Up @@ -165,9 +161,7 @@ export function getHeaderLocale(
parser = parseDefaultHeader,
}: HeaderOptions & { lang?: string } = {},
): Intl.Locale {
return getLocaleWithGetter(() =>
getHeaderLanguages(context, { name, parser })[0] || lang
)
return getLocaleWithGetter(() => getHeaderLanguages(context, { name, parser })[0] || lang)
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { describe, expect, test } from 'vitest'
import {
getPathLanguage,
getPathLocale,
getQueryLanguage,
getQueryLocale,
} from './http.ts'
import { getPathLanguage, getPathLocale, getQueryLanguage, getQueryLocale } from './http.ts'

describe('getPathLanguage', () => {
test('basic', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ export function getHeaderLanguagesWithGetter(
const langString = getter()
return langString
? name === ACCEPT_LANGUAGE_HEADER
? parser === parseDefaultHeader
? parseAcceptLanguage(langString)
: parser(langString)
? parser === parseDefaultHeader ? parseAcceptLanguage(langString) : parser(langString)
: parser(langString)
: []
}
Expand Down
50 changes: 22 additions & 28 deletions src/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ export type ParseUnicodeRegionSubtag<
: [never, 7, RestChunks] // require characters length
: ThroughErrorWithChunks<RestChunks, [never, 7, RestChunks]> // require characters length

type ThroughErrorWithChunks<Chunks extends unknown[], Result> =
Length<Chunks> extends 0 ? Result : [never, never, Chunks]
type ThroughErrorWithChunks<Chunks extends unknown[], Result> = Length<Chunks> extends 0 ? Result
: [never, never, Chunks]

/**
* parse unicode variant subtag
Expand Down Expand Up @@ -402,11 +402,10 @@ type ParseUnicodeExtensions<
Chunks,
Extensions
>,
Result extends [Omit<UnicodeLocaleId, 'lang'>, number, unknown[]] =
Length<Chunks> extends 0 ? [{ extensions: [] }, never, Chunks]
: IsNever<ResultExtensions[1]> extends false
? [{ extensions: [] }, ResultExtensions[1], Chunks]
: [ResultExtensions[0], never, ResultExtensions[2]],
Result extends [Omit<UnicodeLocaleId, 'lang'>, number, unknown[]] = Length<Chunks> extends 0
? [{ extensions: [] }, never, Chunks]
: IsNever<ResultExtensions[1]> extends false ? [{ extensions: [] }, ResultExtensions[1], Chunks]
: [ResultExtensions[0], never, ResultExtensions[2]],
> = Result

// type p1 = ParseUnicodeExtensions<['x', '1234']>
Expand All @@ -427,12 +426,11 @@ type _ParseUnicodeExtensions<
// : never,

// parse for PuExtension
ResultParsePu extends [PuExtension, number, unknown[]] =
_ParseUnicodeExtensionsPu<
RestChunks,
Type,
ExistPuExtension
>,
ResultParsePu extends [PuExtension, number, unknown[]] = _ParseUnicodeExtensionsPu<
RestChunks,
Type,
ExistPuExtension
>,
_ExtensionsPu extends UnicodeLocaleId['extensions'] = Push<
Extensions,
ResultParsePu[0]
Expand Down Expand Up @@ -467,8 +465,7 @@ type _ParseUnicodeExtensions<
NextExistPuExtension extends PuExtension = ResultParsePu[0],
> = IsNever<Error> extends false ? [never, Error, Chunks]
: Length<RestChunks> extends 0 ? [{ extensions: Extensions }, never, Chunks]
: Length<NextChunks> extends 0
? [{ extensions: NextExtensions }, never, NextChunks]
: Length<NextChunks> extends 0 ? [{ extensions: NextExtensions }, never, NextChunks]
: _ParseUnicodeExtensions<
NextChunks,
NextExtensions,
Expand All @@ -482,12 +479,11 @@ type _ParseUnicodeExtensionsPu<
Chunks extends unknown[],
Type extends string,
ExistPuExtension extends PuExtension = never,
ResultParsePuExtension extends unknown[] =
CheckExtensionType<Type, ['x', 'X']> extends true
? ParsePuExtension<[...Chunks]>
: never,
_PuExtension extends PuExtension = ResultParsePuExtension[0] extends
PuExtension ? ResultParsePuExtension[0]
ResultParsePuExtension extends unknown[] = CheckExtensionType<Type, ['x', 'X']> extends true
? ParsePuExtension<[...Chunks]>
: never,
_PuExtension extends PuExtension = ResultParsePuExtension[0] extends PuExtension
? ResultParsePuExtension[0]
: never,
RestChunks extends unknown[] = ResultParsePuExtension[2] extends unknown[]
? ResultParsePuExtension[2]
Expand Down Expand Up @@ -516,21 +512,19 @@ type _ParseUnicodeExtensionsOther<
Chunks extends unknown[],
Type extends string,
ExistOtherExtensions extends unknown[] = never,
MalformedError extends number =
Includes<UnionToTuple<OtherExtensions>, Type> extends false ? 16
: never,
MalformedError extends number = Includes<UnionToTuple<OtherExtensions>, Type> extends false ? 16
: never,
Error extends number = MalformedError extends number ? MalformedError
: Includes<ExistOtherExtensions, Type> extends true ? 17
: never,
ResultParseOtherExtension extends [string, unknown[]] = IsNever<Error> extends
true ? ParseOtherExtension<[...Chunks]>
ResultParseOtherExtension extends [string, unknown[]] = IsNever<Error> extends true
? ParseOtherExtension<[...Chunks]>
: [never, Chunks],
RestChunks extends unknown[] = ResultParseOtherExtension[1] extends unknown[]
? ResultParseOtherExtension[1]
: Chunks,
Result extends [OtherExtension, number, unknown[]] = [
ResultParseOtherExtension[0] extends string
? { type: 'a'; value: ResultParseOtherExtension[0] }
ResultParseOtherExtension[0] extends string ? { type: 'a'; value: ResultParseOtherExtension[0] }
: never,
Error,
RestChunks,
Expand Down
17 changes: 3 additions & 14 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,10 @@ import {
parseDefaultHeader,
validateLocale,
} from './http.ts'
import {
ACCEPT_LANGUAGE_HEADER,
DEFAULT_COOKIE_NAME,
DEFAULT_LANG_TAG,
} from './constants.ts'
import { ACCEPT_LANGUAGE_HEADER, DEFAULT_COOKIE_NAME, DEFAULT_LANG_TAG } from './constants.ts'
import { normalizeLanguageName, pathLanguageParser } from './shared.ts'

import type {
CookieOptions,
HeaderOptions,
PathOptions,
QueryOptions,
} from './http.ts'
import type { CookieOptions, HeaderOptions, PathOptions, QueryOptions } from './http.ts'

/**
* get languages from header
Expand Down Expand Up @@ -171,9 +162,7 @@ export function getHeaderLocale(
parser = parseDefaultHeader,
}: HeaderOptions & { lang?: string } = {},
): Intl.Locale {
return getLocaleWithGetter(() =>
getHeaderLanguages(request, { name, parser })[0] || lang
)
return getLocaleWithGetter(() => getHeaderLanguages(request, { name, parser })[0] || lang)
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ export function createPathIndexLanguageParser(
}
}

export let pathLanguageParser: PathLanguageParser =
/* #__PURE__*/ createPathIndexLanguageParser()
export let pathLanguageParser: PathLanguageParser = /* #__PURE__*/ createPathIndexLanguageParser()

/**
* register the path language parser
Expand Down
27 changes: 13 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
export type IsNever<T> = [T] extends [never] ? true : false
export type Split<S extends string, SEP extends string> = string extends S
? string[]
: S extends `${infer A}${SEP}${infer B}`
? [A, ...(B extends '' ? [] : Split<B, SEP>)]
export type Split<S extends string, SEP extends string> = string extends S ? string[]
: S extends `${infer A}${SEP}${infer B}` ? [A, ...(B extends '' ? [] : Split<B, SEP>)]
: SEP extends '' ? []
: [S]
export type Join<T extends unknown[], U extends string | number> = T extends
[infer F, ...infer R] ? R['length'] extends 0 ? `${F & string}`
export type Join<T extends unknown[], U extends string | number> = T extends [infer F, ...infer R]
? R['length'] extends 0 ? `${F & string}`
: `${F & string}${U}${Join<R, U>}`
: never
export type Shift<T extends unknown[]> = T extends [unknown, ...infer U] ? U
Expand All @@ -15,8 +13,9 @@ export type First<T extends unknown[]> = T extends [infer A, ...infer rest] ? A
: never
export type Last<T extends unknown[]> = [unknown, ...T][T['length']]
export type Length<T extends readonly unknown[]> = T['length']
export type IsEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false
export type IsEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2)
? true
: false
export type All<T extends unknown[], U> = T extends [infer L, ...infer R]
? IsEqual<L, U> extends true ? All<R, U>
: false
Expand All @@ -42,13 +41,13 @@ export type LastInUnion<U> = UnionToIntersection<
: never
export type UnionToTuple<U, Last = LastInUnion<U>> = [U] extends [never] ? []
: [...UnionToTuple<Exclude<U, Last>>, Last]
export type TupleToUnion<T extends readonly unknown[]> = T extends
Array<infer R> ? R
export type TupleToUnion<T extends readonly unknown[]> = T extends Array<infer R> ? R
: never

export type StringToUnion<T extends string> = T extends
`${infer Letter}${infer Rest}` ? Letter | StringToUnion<Rest>
export type StringToUnion<T extends string> = T extends `${infer Letter}${infer Rest}`
? Letter | StringToUnion<Rest>
: never

export type StringToArray<T extends string> = T extends
`${infer Letter}${infer Rest}` ? [Letter, ...StringToArray<Rest>] : []
export type StringToArray<T extends string> = T extends `${infer Letter}${infer Rest}`
? [Letter, ...StringToArray<Rest>]
: []
17 changes: 3 additions & 14 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@ import {
validateLocale,
} from './http.ts'
import { pathLanguageParser } from './shared.ts'
import {
ACCEPT_LANGUAGE_HEADER,
DEFAULT_COOKIE_NAME,
DEFAULT_LANG_TAG,
} from './constants.ts'
import { ACCEPT_LANGUAGE_HEADER, DEFAULT_COOKIE_NAME, DEFAULT_LANG_TAG } from './constants.ts'

import type {
CookieOptions,
HeaderOptions,
PathOptions,
QueryOptions,
} from './http.ts'
import type { CookieOptions, HeaderOptions, PathOptions, QueryOptions } from './http.ts'

/**
* get languages from header
Expand Down Expand Up @@ -171,9 +162,7 @@ export function getHeaderLocale(
parser = parseDefaultHeader,
}: HeaderOptions & { lang?: string } = {},
): Intl.Locale {
return getLocaleWithGetter(() =>
getHeaderLanguages(request, { name, parser })[0] || lang
)
return getLocaleWithGetter(() => getHeaderLanguages(request, { name, parser })[0] || lang)
}

/**
Expand Down

0 comments on commit a4dff1e

Please sign in to comment.