@@ -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+ */
120131export 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+ */
125149export 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+ */
129160export 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+ */
143181export 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+ */
147192export 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+ */
153206export 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+ */
160221export 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+ */
171235export 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 */
185255export 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 */
203273export 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+ */
220293export 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 */
234313export 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 */
253332export function getQueryLocale (
254333 query : string | URL | URLSearchParams ,
0 commit comments