Skip to content

Commit 1f96af5

Browse files
committed
fix: wrong jsdoc
1 parent 906212a commit 1f96af5

File tree

16 files changed

+776
-438
lines changed

16 files changed

+776
-438
lines changed

deno/constants.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
* @license MIT
44
*/
55

6+
/**
7+
* The default language tag
8+
*/
69
export const DEFAULT_LANG_TAG = 'en-US'
10+
11+
/**
12+
* The default cookie name
13+
*/
714
export const DEFAULT_COOKIE_NAME = 'i18n_locale'
15+
16+
/**
17+
* The Accept-Language header name
18+
*/
819
export const ACCEPT_LANGUAGE_HEADER = 'accept-language'

deno/http.ts

Lines changed: 102 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ interface CookieSerializeOptions {
4141
* no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete
4242
* it on a condition like exiting a web browser application.
4343
*
44-
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
44+
* Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
4545
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
4646
* possible not all clients by obey this, so if both are set, they should
4747
* point to the same date and time.
@@ -52,7 +52,7 @@ interface CookieSerializeOptions {
5252
* When truthy, the `HttpOnly` attribute is set, otherwise it is not. By
5353
* default, the `HttpOnly` attribute is not set.
5454
*
55-
* *Note* be careful when setting this to true, as compliant clients will
55+
* Note* be careful when setting this to true, as compliant clients will
5656
* not allow client-side JavaScript to see the cookie in `document.cookie`.
5757
*/
5858
httpOnly?: boolean | undefined
@@ -61,7 +61,7 @@ interface CookieSerializeOptions {
6161
* `Set-Cookie` attribute. The given number will be converted to an integer
6262
* by rounding down. By default, no maximum age is set.
6363
*
64-
* *Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
64+
* Note* the {@link https://tools.ietf.org/html/rfc6265#section-5.3|cookie storage model specification}
6565
* states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is
6666
* possible not all clients by obey this, so if both are set, they should
6767
* point to the same date and time.
@@ -101,31 +101,62 @@ interface CookieSerializeOptions {
101101
*
102102
* More information about the different enforcement levels can be found in {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.2.7|the specification}.
103103
*
104-
* *note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
104+
* note* This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.
105105
*/
106106
sameSite?: true | false | 'lax' | 'strict' | 'none' | undefined
107107
/**
108108
* Specifies the boolean value for the {@link https://tools.ietf.org/html/rfc6265#section-5.2.5|`Secure` `Set-Cookie` attribute}. When truthy, the
109109
* `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
110110
*
111-
* *Note* be careful when setting this to `true`, as compliant clients will
111+
* Note* be careful when setting this to `true`, as compliant clients will
112112
* not send the cookie back to the server in the future if the browser does
113113
* not have an HTTPS connection.
114114
*/
115115
secure?: boolean | undefined
116116
}
117117

118-
export type CookieOptions = CookieSerializeOptions & { name?: string }
118+
/**
119+
* Cookie options type
120+
*/
121+
export type CookieOptions = CookieSerializeOptions & {
122+
/**
123+
* Cookie name
124+
*/
125+
name?: string
126+
}
119127

128+
/**
129+
* Header options type
130+
*/
120131
export type HeaderOptions = {
132+
/**
133+
* Header name
134+
*/
121135
name?: string
136+
/**
137+
* Header parser function
138+
*/
122139
parser?: typeof parseAcceptLanguage
123140
}
124141

142+
/**
143+
* default header parser
144+
*
145+
* @param input - the header string
146+
*
147+
* @returns The array that include the input string
148+
*/
125149
export function parseDefaultHeader(input: string): string[] {
126150
return [input]
127151
}
128152

153+
/**
154+
* get languages from header with getter function
155+
*
156+
* @param getter - the header string getter function
157+
*
158+
* @returns The array of language tags
159+
*/
129160
export function getHeaderLanguagesWithGetter(
130161
getter: () => string | null | undefined,
131162
{ name = ACCEPT_LANGUAGE_HEADER, parser = parseDefaultHeader }: HeaderOptions = {}
@@ -140,23 +171,53 @@ export function getHeaderLanguagesWithGetter(
140171
: []
141172
}
142173

174+
/**
175+
* get locale from language tag with getter function
176+
*
177+
* @param getter - the language tag getter function
178+
*
179+
* @returns The {@link Intl.Locale} object
180+
*/
143181
export function getLocaleWithGetter(getter: () => string): Intl.Locale {
144182
return toLocale(getter())
145183
}
146184

185+
/**
186+
* validate the locale
187+
*
188+
* @param locale - the locale to validate
189+
*
190+
* @throws {SyntaxError} Throws the {@linkcode SyntaxError} if the locale is invalid.
191+
*/
147192
export function validateLocale(locale: string | Intl.Locale): void {
148193
if (!(isLocale(locale) || (typeof locale === 'string' && validateLangTag(locale)))) {
149194
throw new SyntaxError(`locale is invalid: ${locale.toString()}`)
150195
}
151196
}
152197

198+
/**
199+
* map to locale from language tag with getter function
200+
*
201+
* @param getter - the language tag getter function
202+
* @param args - the arguments for the getter function
203+
*
204+
* @returns The array of {@linkcode Intl.Locale} objects
205+
*/
153206
export function mapToLocaleFromLanguageTag(
154207
getter: (...args: unknown[]) => string[],
155208
...args: unknown[]
156209
): Intl.Locale[] {
157210
return Reflect.apply(getter, null, args).map(lang => getLocaleWithGetter(() => lang))
158211
}
159212

213+
/**
214+
* get existing cookies by name with getter function
215+
*
216+
* @param name - cookie name
217+
* @param getter - cookie getter function
218+
*
219+
* @returns The array of existing cookies
220+
*/
160221
export function getExistCookies(name: string, getter: () => unknown) {
161222
let setCookies = getter()
162223
if (!Array.isArray(setCookies)) {
@@ -168,19 +229,28 @@ export function getExistCookies(name: string, getter: () => unknown) {
168229
return setCookies as string[]
169230
}
170231

232+
/**
233+
* path options type
234+
*/
171235
export type PathOptions = {
236+
/**
237+
* The language tag, which is as default `'en-US'`. optional
238+
*/
172239
lang?: string
240+
/**
241+
* The path language parser, optional
242+
*/
173243
parser?: PathLanguageParser
174244
}
175245

176246
/**
177247
* get the language from the path
178248
*
179-
* @param {string | URL} path the target path
180-
* @param {PathOptions['lang']} options.lang the language tag, which is as default `'en-US'`. optional
181-
* @param {PathOptions['parser']} options.parser the path language parser, optional
249+
* @param path - the target path
250+
* @param options.lang - the language tag, which is as default `'en-US'`. optional
251+
* @param options.parser - the path language parser, optional
182252
*
183-
* @returns {string} the language that is parsed by the path language parser, if the language is not detected, return a `options.lang` value
253+
* @returns the language that is parsed by the path language parser, if the language is not detected, return a `options.lang` value
184254
*/
185255
export function getPathLanguage(
186256
path: string | URL,
@@ -192,13 +262,13 @@ export function getPathLanguage(
192262
/**
193263
* get the locale from the path
194264
*
195-
* @param {string | URL} path the target path
196-
* @param {PathOptions['lang']} options.lang the language tag, which is as default `'en-US'`. optional
197-
* @param {PathOptions['parser']} options.parser the path language parser, optional
265+
* @param path - the target path
266+
* @param options.lang - the language tag, which is as default `'en-US'`. optional
267+
* @param options.parser - the path language parser, optional
198268
*
199269
* @throws {RangeError} Throws the {@link RangeError} if the language in the path, that is not a well-formed BCP 47 language tag.
200270
*
201-
* @returns {Intl.Locale} The locale that resolved from path
271+
* @returns The locale that resolved from path
202272
*/
203273
export function getPathLocale(
204274
path: string | URL,
@@ -217,19 +287,28 @@ function getURLSearchParams(input: string | URL | URLSearchParams): URLSearchPar
217287
}
218288
}
219289

290+
/**
291+
* query options type
292+
*/
220293
export type QueryOptions = {
294+
/**
295+
* The language tag, which is as default `'en-US'`. optional
296+
*/
221297
lang?: string
298+
/**
299+
* The query param name, default `'lang'`. optional
300+
*/
222301
name?: string
223302
}
224303

225304
/**
226305
* get the language from the query
227306
*
228-
* @param {string | URL | URLSearchParams} query the target query
229-
* @param {QueryOptions['lang']} options.lang the language tag, which is as default `'en-US'`. optional
230-
* @param {QueryOptions['name']} options.name the query param name, default `'lang'`. optional
307+
* @param query - the target query
308+
* @param options.lang - the language tag, which is as default `'en-US'`. optional
309+
* @param options.name - the query param name, default `'lang'`. optional
231310
*
232-
* @returns {string} the language from query, if the language is not detected, return an `options.lang` option string.
311+
* @returns the language from query, if the language is not detected, return an `options.lang` option string.
233312
*/
234313
export function getQueryLanguage(
235314
query: string | URL | URLSearchParams,
@@ -242,13 +321,13 @@ export function getQueryLanguage(
242321
/**
243322
* get the locale from the query
244323
*
245-
* @param {string | URL | URLSearchParams} query the target query
246-
* @param {QueryOptions['lang']} options.lang the language tag, which is as default `'en-US'`. optional
247-
* @param {QueryOptions['name']} options.name the query param name, default `'locale'`. optional
324+
* @param query - the target query
325+
* @param options.lang - the language tag, which is as default `'en-US'`. optional
326+
* @param options.name - the query param name, default `'locale'`. optional
248327
*
249-
* @throws {RangeError} Throws the {@link RangeError} if the language in the query, that is not a well-formed BCP 47 language tag.
328+
* @throws {RangeError} Throws the {@linkcode RangeError} if the language in the query, that is not a well-formed BCP 47 language tag.
250329
*
251-
* @returns {Intl.Locale} The locale that resolved from query
330+
* @returns The locale that resolved from query
252331
*/
253332
export function getQueryLocale(
254333
query: string | URL | URLSearchParams,

deno/shared.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,34 @@
66
const objectToString = Object.prototype.toString // eslint-disable-line @typescript-eslint/unbound-method -- ignore
77
const toTypeString = (value: unknown): string => objectToString.call(value)
88

9+
/**
10+
* check whether the value is a {@linkcode URL} instance
11+
*
12+
* @param val - The value to be checked
13+
*
14+
* @returns Returns `true` if the value is a {@linkcode URL} instance, else `false`.
15+
*/
916
export function isURL(val: unknown): val is URL {
1017
return toTypeString(val) === '[object URL]'
1118
}
1219

20+
/**
21+
* check whether the value is a {@linkcode URLSearchParams} instance
22+
*
23+
* @param val - The value to be checked
24+
*
25+
* @returns Returns `true` if the value is a {@linkcode URLSearchParams} instance, else `false`.
26+
*/
1327
export function isURLSearchParams(val: unknown): val is URLSearchParams {
1428
return toTypeString(val) === '[object URLSearchParams]'
1529
}
1630

1731
/**
18-
* check whether the value is a {@link Intl.Locale} instance
32+
* check whether the value is a {@linkcode Intl.Locale} instance
1933
*
20-
* @param {unknown} val The locale value
34+
* @param val - The locale value
2135
*
22-
* @returns {boolean} Returns `true` if the value is a {@link Intl.Locale} instance, else `false`.
36+
* @returns Returns `true` if the value is a {@linkcode Intl.Locale} instance, else `false`.
2337
*/
2438
export function isLocale(val: unknown): val is Intl.Locale {
2539
return toTypeString(val) === '[object Intl.Locale]'
@@ -28,11 +42,11 @@ export function isLocale(val: unknown): val is Intl.Locale {
2842
/**
2943
* returns the {@link Intl.Locale | locale}
3044
*
31-
* @param {string | Intl.Locale} val The value for which the 'locale' is requested.
45+
* @param val - The value for which the 'locale' is requested.
3246
*
33-
* @throws {RangeError} Throws the {@link RangeError} if `val` is not a well-formed BCP 47 language tag.
47+
* @throws {RangeError} Throws the {@linkcode RangeError} if `val` is not a well-formed BCP 47 language tag.
3448
*
35-
* @returns {Intl.Locale} The locale
49+
* @returns The locale
3650
*/
3751
export function toLocale(val: string | Intl.Locale): Intl.Locale {
3852
return isLocale(val) ? val : new Intl.Locale(val)
@@ -41,9 +55,9 @@ export function toLocale(val: string | Intl.Locale): Intl.Locale {
4155
/**
4256
* validate the language tag whether is a well-formed {@link https://datatracker.ietf.org/doc/html/rfc4646#section-2.1 | BCP 47 language tag}.
4357
*
44-
* @param {string} lang a language tag
58+
* @param lang - a language tag
4559
*
46-
* @returns {boolean} Returns `true` if the language tag is valid, else `false`.
60+
* @returns Returns `true` if the language tag is valid, else `false`.
4761
*/
4862
export function validateLangTag(lang: string): boolean {
4963
try {
@@ -58,9 +72,9 @@ export function validateLangTag(lang: string): boolean {
5872
/**
5973
* parse `accept-language` header string
6074
*
61-
* @param {string} value The accept-language header string
75+
* @param value - The accept-language header string
6276
*
63-
* @returns {Array<string>} The array of language tags, if `*` (any language) or empty string is detected, return an empty array.
77+
* @returns The array of language tags, if `*` (any language) or empty string is detected, return an empty array.
6478
*/
6579
export function parseAcceptLanguage(value: string): string[] {
6680
return value
@@ -82,9 +96,9 @@ export function parseAcceptLanguage(value: string): string[] {
8296
* conosle.log(langTag) // en-US
8397
* ```
8498
*
85-
* @param langName The target language name
99+
* @param langName - The target language name
86100
*
87-
* @returns {string} The normalized language tag
101+
* @returns The normalized language tag
88102
*/
89103
export function normalizeLanguageName(langName: string): string {
90104
const [lang] = langName.split('.')
@@ -98,19 +112,19 @@ export interface PathLanguageParser {
98112
/**
99113
* parse the path that is include language
100114
*
101-
* @param {string | URL} path the target path
115+
* @param path - the target path
102116
*
103-
* @returns {string} the language, if it cannot parse the path is not found, you need to return empty string (`''`)
117+
* @returns The language, if it cannot parse the path is not found, you need to return empty string (`''`)
104118
*/
105119
(path: string | URL): string
106120
}
107121

108122
/**
109123
* create a parser, which can split with slash `/`
110124
*
111-
* @param index An index of locale, which is included in path
125+
* @param index - An index of locale, which is included in path
112126
*
113-
* @returns A return a parser, which has {@link PathLanguageParser} interface
127+
* @returns A return a parser, which has {@linkcode PathLanguageParser} interface
114128
*/
115129
export function createPathIndexLanguageParser(index = 0): PathLanguageParser {
116130
return (path: string | URL): string => {
@@ -137,7 +151,7 @@ export let pathLanguageParser: PathLanguageParser = /* #__PURE__*/ createPathInd
137151
*
138152
* @description register a parser to be used in the `getPathLanguage` utility function
139153
*
140-
* @param {PathLanguageParser} parser the path language parser
154+
* @param parser - the {@link PathLanguageParser | path language parser}
141155
*/
142156
export function registerPathLanguageParser(parser: PathLanguageParser): void {
143157
pathLanguageParser = parser

0 commit comments

Comments
 (0)