Skip to content

Commit 5edd9bd

Browse files
committed
feat: rewrite README and expand test coverage
This commit rewrites the project documentation and expands the test suite. - The README.md is updated to include installation instructions, usage examples for basic and lookup modes, an API reference, and explanations of BCP 47 and RFC 4647 standards. - TSDoc comments are added to `src/index.ts` to document functions, parameters, and options. - The test suite in `src/index.spec.ts` is expanded with test cases for malformed inputs, edge cases, and real-world browser headers. - The local `uniq` utility is replaced with the implementation from the `es-toolkit` library.
1 parent 32b7d6f commit 5edd9bd

File tree

5 files changed

+317
-86
lines changed

5 files changed

+317
-86
lines changed

README.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,82 @@
1-
# accept-language-parser
1+
# @escapace/accept-language-parser
22

3-
Parses the accept-language header from an HTTP request and produces an array of
4-
language tags.
3+
Parses HTTP Accept-Language header and returns matched language tags using RFC 4647 algorithms and BCP 47 normalization standards.
4+
5+
## Installation
6+
7+
```bash
8+
pnpm install @escapace/accept-language-parser
9+
```
10+
11+
## Usage
12+
13+
```typescript
14+
import { pick } from '@escapace/accept-language-parser'
15+
16+
// Basic language matching with quality scores
17+
pick('en-US;q=0.6', ['en', 'pl'])
18+
// Returns: ['en']
19+
20+
// Lookup algorithm for single best match
21+
pick('en-US,en;q=0.9,fr;q=0.8', ['en-GB', 'en', 'fr'], { type: 'lookup' })
22+
// Returns: ['en']
23+
24+
// Empty result when no matches found
25+
pick('', ['en'])
26+
// Returns: []
27+
```
28+
29+
## API
30+
31+
### `pick(acceptLanguage, tags, options?)`
32+
33+
Parses an Accept-Language header and returns matching language tags in preference order.
34+
35+
**Parameters:**
36+
37+
- `acceptLanguage` (string): The Accept-Language header value
38+
- `tags` (string[]): Array of supported BCP 47 language tags
39+
- `options` (Options, optional): Configuration options
40+
41+
**Returns:** `string[]` - Array of matched language tags ordered by preference
42+
43+
**Options:**
44+
45+
- `forgiving` (boolean, default: false): Handle malformed language tags gracefully
46+
- `type` ('basic' | 'lookup', default: 'basic'): Language matching algorithm
47+
48+
**Language Matching Algorithms:**
49+
50+
- `'basic'`: Implements RFC 4647 Section 3.3.1 Basic Filtering. Returns all matching language tags using prefix matching. More specific tags match less specific ranges (e.g., 'en' range matches 'en-GB' tag). Processes all available language tags and returns every tag that matches the Accept-Language preferences.
51+
52+
- `'lookup'`: Implements RFC 4647 Section 3.4 Lookup matching. Returns the single best matching language tag, prioritizing more specific matches. Finds and returns only the most appropriate match from the available options, following language tag hierarchy and preference order.
53+
54+
## Accept-Language Header Format
55+
56+
The Accept-Language header specifies language preferences using quality scores:
57+
58+
```
59+
Accept-Language: en-US,en;q=0.9,fr;q=0.8,*;q=0.1
60+
```
61+
62+
- Languages without quality scores default to `q=1.0` (highest priority)
63+
- Quality scores range from 0.0 to 1.0
64+
- The `*` wildcard matches any language
65+
66+
## BCP 47 Language Tags
67+
68+
Follows BCP 47 standards for language tag formatting and matching. Language tags use formats like:
69+
70+
- `en` (language)
71+
- `en-US` (language-region)
72+
- `zh-Hant-CN` (language-script-region)
73+
74+
Tags are automatically normalized for consistent matching using Unicode CLDR recommendations.
75+
76+
## Credits
77+
78+
Uses functionality from:
79+
80+
- **[bcp-47-normalize](https://github.com/wooorm/bcp-47-normalize)** - BCP 47 language tag normalization and canonicalization
81+
- **[bcp-47-match](https://github.com/wooorm/bcp-47-match)** - RFC 4647 compliant language tag matching algorithms
82+
- **[Hono](https://github.com/honojs/hono)** - Accept header parsing utilities ([accept.ts](https://github.com/honojs/hono/blob/main/src/utils/accept.ts))

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@escapace/accept-language-parser",
3-
"description": "",
3+
"description": "Parse HTTP Accept-Language header and return matched language tags",
44
"version": "0.0.0",
55
"author": {
66
"name": "escapace",
@@ -17,6 +17,7 @@
1717
"@escapace/pnpm-pack": "0.6.0",
1818
"@ls-lint/ls-lint": "2.3.1",
1919
"@vitest/coverage-v8": "3.2.4",
20+
"es-toolkit": "1.39.8",
2021
"eslint": "9.32.0",
2122
"eslint-config-escapace": "5.8.0",
2223
"esroll": "0.4.5",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)