Skip to content

Commit 1dd8c44

Browse files
committed
Update docs
1 parent 9a600f5 commit 1dd8c44

File tree

3 files changed

+115
-35
lines changed

3 files changed

+115
-35
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
/**
2+
* @typedef {import('bcp-47').Options} Options
3+
* @typedef {import('bcp-47').Warning} Warning
4+
*/
5+
16
export {bcp47Normalize} from './lib/index.js'

lib/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,23 @@ function removeLikelySubtags(schema) {
100100
}
101101

102102
/**
103-
* @param {string} value
103+
* Normalize the given BCP 47 tag according to Unicode CLDR suggestions.
104+
*
105+
* @param {string} tag
106+
* BCP 47 tag.
104107
* @param {Options} [options]
108+
* Configuration (optional).
105109
* @returns {string}
110+
* Normal, canonical, and pretty BCP 47 tag.
106111
*/
107-
export function bcp47Normalize(value, options) {
112+
export function bcp47Normalize(tag, options) {
108113
const settings = options || {}
109114
// 1. normalize and lowercase the tag (`sgn-be-fr` -> `sfb`).
110-
const schema = parse(String(value || '').toLowerCase(), settings)
111-
const tag = stringify(schema)
115+
const schema = parse(String(tag || '').toLowerCase(), settings)
116+
const value = stringify(schema)
112117

113-
if (!tag) {
114-
return tag
118+
if (!value) {
119+
return value
115120
}
116121

117122
let index = -1
@@ -124,7 +129,7 @@ export function bcp47Normalize(value, options) {
124129
from = schema.language + from.slice(3)
125130
}
126131

127-
if (extendedFilter(tag, from).length > 0) {
132+
if (extendedFilter(value, from).length > 0) {
128133
replace(schema, from, matches[index].to)
129134
}
130135
}

readme.md

Lines changed: 98 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,57 @@
77

88
Normalize, canonicalize, and format [BCP 47][spec] tags.
99

10-
## Install
10+
## Contents
11+
12+
* [What is this?](#what-is-this)
13+
* [When should I use this?](#when-should-i-use-this)
14+
* [Install](#install)
15+
* [Use](#use)
16+
* [API](#api)
17+
* [`bcp47Normalize(tag[, options])`](#bcp47normalizetag-options)
18+
* [Types](#types)
19+
* [Compatibility](#compatibility)
20+
* [Security](#security)
21+
* [Related](#related)
22+
* [Contribute](#contribute)
23+
* [License](#license)
24+
25+
## What is this?
26+
27+
This package takes BCP 47 tags and makes them uniform.
28+
It removes unneeded info (`en-us` -> `en`) and replaces deprecated,
29+
overlong, and otherwise unpreferred values with preferred values
30+
(`en-bu` -> `en-MM`).
31+
It works by applying [Unicode CLDR suggestions][alias].
32+
33+
## When should I use this?
34+
35+
You can use this package when dealing with user-provided language tags and want
36+
to normalize and clean them.
1137

12-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
13-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
38+
## Install
1439

15-
[npm][]:
40+
This package is [ESM only][esm].
41+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
1642

1743
```sh
1844
npm install bcp-47-normalize
1945
```
2046

47+
In Deno with [Skypack][]:
48+
49+
```js
50+
import {bcp47Normalize} from 'https://cdn.skypack.dev/bcp-47-normalize@2?dts'
51+
```
52+
53+
In browsers with [Skypack][]:
54+
55+
```html
56+
<script type="module">
57+
import {bcp47Normalize} from 'https://cdn.skypack.dev/bcp-47-normalize@2?min'
58+
</script>
59+
```
60+
2161
## Use
2262

2363
```js
@@ -56,51 +96,73 @@ zh-hans-cn -> zh
5696

5797
## API
5898

59-
This package exports the following identifiers: `bcp47Normalize`.
99+
This package exports the following identifier: `bcp47Normalize`.
60100
There is no default export.
61101

62102
### `bcp47Normalize(tag[, options])`
63103

64104
Normalize the given BCP 47 tag according to [Unicode CLDR suggestions][alias].
65105

66-
###### `options.forgiving`
106+
###### Parameters
67107

68-
Passed to `bcp-47` as [`options.forgiving`][forgiving].
108+
* `tag` (`string`)
109+
— BCP 47 tag
110+
* `options.forgiving` (`boolean`, default: `false`)
111+
— passed to `bcp-47` as [`options.forgiving`][forgiving]
112+
* `options.warning` (`Function?`, default: `undefined`)
113+
— passed to `bcp-47` as [`options.warning`][warning]
69114

70-
###### `options.warning`
115+
One additional warning is given:
71116

72-
Passed to `bcp-47` as [`options.warning`][warning].
117+
| code | reason |
118+
| :--- | :--------------------------------------------------------- |
119+
| 7 | Deprecated region `CURRENT`, expected one of `SUGGESTIONS` |
73120

74-
One additional warning is given:
121+
This warning is only given if the region cannot be automatically fixed (when
122+
regions split into multiple regions).
75123

76-
| code | reason |
77-
| :--- | :--------------------------------------------------------- |
78-
| 7 | Deprecated region `CURRENT`, expected one of `SUGGESTIONS` |
124+
###### Returns
79125

80-
This warning is only given if the region cannot be automatically fixed (when
81-
regions split into multiple regions).
126+
Normal, canonical, and pretty [BCP 47][spec] tag (`string`).
82127

83-
###### Returns
128+
## Types
84129

85-
`string` — Normal, canonical, and pretty [BCP 47][spec] tag.
130+
This package is fully typed with [TypeScript][].
131+
It exports additional `Options` and `Warning` types that model their respective
132+
interfaces.
133+
134+
## Compatibility
135+
136+
This package is at least compatible with all maintained versions of Node.js.
137+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
138+
It also works in Deno and modern browsers.
139+
140+
## Security
141+
142+
This package is safe.
86143

87144
## Related
88145

89-
* [`bcp-47`](https://github.com/wooorm/bcp-47-match)
90-
Parse and stringify BCP 47 language tags
91-
* [`bcp-47-match`](https://github.com/wooorm/bcp-47-match)
92-
Match BCP 47 language tags with language ranges per RFC 4647
93-
* [`iso-3166`](https://github.com/wooorm/iso-3166)
146+
* [`wooorm/bcp-47`](https://github.com/wooorm/bcp-47-match)
147+
parse and stringify BCP 47 language tags
148+
* [`wooorm/bcp-47-match`](https://github.com/wooorm/bcp-47-match)
149+
match BCP 47 language tags with language ranges per RFC 4647
150+
* [`wooorm/iso-3166`](https://github.com/wooorm/iso-3166)
94151
— ISO 3166 codes
95-
* [`iso-639-2`](https://github.com/wooorm/iso-639-2)
152+
* [`wooorm/iso-639-2`](https://github.com/wooorm/iso-639-2)
96153
— ISO 639-2 codes
97-
* [`iso-639-3`](https://github.com/wooorm/iso-639-3)
154+
* [`wooorm/iso-639-3`](https://github.com/wooorm/iso-639-3)
98155
— ISO 639-3 codes
99-
* [`iso-15924`](https://github.com/wooorm/iso-15924)
156+
* [`wooorm/iso-15924`](https://github.com/wooorm/iso-15924)
100157
— ISO 15924 codes
101-
* [`un-m49`](https://github.com/wooorm/un-m49)
158+
* [`wooorm/un-m49`](https://github.com/wooorm/un-m49)
102159
— UN M49 codes
103160

161+
## Contribute
162+
163+
Yes please!
164+
See [How to Contribute to Open Source][contribute].
165+
104166
## License
105167

106168
[MIT][license] © [Titus Wormer][author]
@@ -125,13 +187,21 @@ regions split into multiple regions).
125187

126188
[npm]: https://docs.npmjs.com/cli/install
127189

190+
[skypack]: https://www.skypack.dev
191+
128192
[license]: license
129193

130194
[author]: https://wooorm.com
131195

132-
[spec]: https://tools.ietf.org/html/bcp47
196+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
197+
198+
[typescript]: https://www.typescriptlang.org
199+
200+
[contribute]: https://opensource.guide/how-to-contribute/
201+
202+
[spec]: https://tools.ietf.org/rfc/bcp/bcp47.html
133203

134-
[alias]: https://github.com/unicode-org/cldr/blob/4b1225ead2ca9bc7a969a271b9931f137040d2bf/common/supplemental/supplementalMetadata.xml#L32
204+
[alias]: https://github.com/unicode-org/cldr/blob/142b327/common/supplemental/supplementalMetadata.xml#L32
135205

136206
[forgiving]: https://github.com/wooorm/bcp-47#optionsforgiving
137207

0 commit comments

Comments
 (0)