Installations
npm install bcp-47
Developer
wooorm
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
Yes
Node Version
17.0.1
NPM Version
8.1.0
Statistics
63 Stars
88 Commits
7 Forks
3 Watching
1 Branches
3 Contributors
Updated on 11 Nov 2024
Bundle Size
3.87 kB
Minified
1.45 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
32,841,342
Last day
-9%
50,429
Compared to previous day
Last week
5.4%
302,128
Compared to previous week
Last month
10.5%
1,214,171
Compared to previous month
Last year
57.7%
12,586,375
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
bcp-47
Parse and stringify BCP 47 language tags.
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This is a package that can parse BCP 47 language tags to an object representing them, and serialize those objects back into language tags. It supports a forgiving mode to handle incorrect BCP 47 tags and can emit warnings about problems in incorrect tags.
When should I use this?
You can use this package if you need to access the data stored in BCP 47 language tags. You can also use this package if you want to check (lint) or manipulate tags.
Install
This package is ESM only. In Node.js (version 14.14+, 16.0+), install with npm:
1npm install bcp-47
In Deno with esm.sh
:
1import * as bcp47 from 'https://esm.sh/bcp-47@2'
In browsers with esm.sh
:
1<script type="module"> 2 import * as bcp47 from 'https://esm.sh/bcp-47@2?bundle' 3</script>
Use
1import {parse, stringify} from 'bcp-47' 2 3const schema = parse('hy-Latn-IT-arevela') 4 5console.log(schema) 6console.log(stringify(schema))
Yields:
1{ language: 'hy', 2 extendedLanguageSubtags: [], 3 script: 'Latn', 4 region: 'IT', 5 variants: ['arevela'], 6 extensions: [], 7 privateuse: [], 8 irregular: null, 9 regular: null } 10'hy-Latn-IT-arevela'
API
This package exports the identifiers parse
and stringify
.
There is no default export.
parse(tag[, options])
Parse a BCP 47 tag into a language schema.
👉 Note: the algorithm is case insensitive.
options.normalize
Whether to normalize legacy tags when possible (boolean
, default:
true
).
For example, i-klingon
does not match the BCP 47 language algorithm but is
considered valid by BCP 47 nonetheless.
It is suggested to use tlh
instead (the ISO 639-3 code for Klingon).
When normalize
is true
, passing i-klingon
or other deprecated tags, is
handled as if their suggested valid tag was given instead.
options.forgiving
By default, when an error is encountered, an empty object is returned.
When in forgiving mode, all found values up to the point of the error
are included (boolean
, default: false
).
So, for example, where by default en-GB-abcdefghi
an empty object is returned
(as the language variant is too long), in forgiving
mode the language
of
schema
is populated with en
and the region
is populated with GB
.
options.warning
When given, warning
is called when an error is encountered
(Function
).
Returns
Parsed BCP 47 language tag (Schema
).
Throws
When tag
is null
or undefined
.
stringify(schema)
Compile a schema
to a BCP 47 language tag.
Returns
BCP 47 language tag (string
).
Schema
A schema represents a language tag.
A schema is deemed empty when it has neither language
, irregular
, regular
,
nor privateuse
(where an empty privateuse
array is handled as no
privateuse
as well).
schema.language
Two or three character ISO 639 language code, four character reserved
language code, or 5 to 8 (inclusive) characters registered language subtag
(string
).
For example, en
(English) or cmn
(Mandarin Chinese).
schema.extendedLanguageSubtags
Selected three-character ISO 639 codes (Array<string>
), such as
yue
in zh-yue-HK
(Chinese, Cantonese, as used in Hong Kong SAR).
schema.script
Four character ISO 15924 script code (string
), such as Latn
in
hy-Latn-IT-arevela
(Eastern Armenian written in Latin script, as used in
Italy).
schema.region
Two alphabetical character ISO 3166-1 code or three digit
UN M49 code (string
).
For example, CN
in cmn-Hans-CN
(Mandarin Chinese, Simplified script, as used
in China) or 419
in es-419
(Spanish as used in Latin America and the
Caribbean).
schema.variants
5 to 8 (inclusive) character language variants (Array<string>
), such as
rozaj
and biske
in sl-rozaj-biske
(San Giorgio dialect of Resian dialect
of Slovenian).
schema.extensions
List of extensions (Array<Object>
), each an object containing a one character
singleton
, and a list of extensions
(string
).
singleton
cannot be x
(case insensitive) and extensions
must be between
two and eight (inclusive) characters.
For example, an extension would be u-co-phonebk
in de-DE-u-co-phonebk
(German, as used in Germany, using German phonebook sort order), where u
is
the singleton
and co
and phonebk
are its extensions.
schema.privateuse
List of private-use subtags (Array<string>
), where each subtag must be between
one and eight (inclusive) characters.
schema.regular
One of the regular
tags (string
): tags that are seen as something different
by the algorithm.
Valid values are:
art-lojban
cel-gaulish
no-bok
no-nyn
zh-guoyu
zh-hakka
zh-min
zh-min-nan
zh-xiang
schema.irregular
One of the irregular
tags (string
): tags that are seen as invalid by the
algorithm).
Valid values are:
en-GB-oed
i-ami
i-bnn
i-default
i-enochian
i-hak
i-klingon
i-lux
i-mingo
i-navajo
i-pwn
i-tao
i-tay
i-tsu
sgn-BE-FR
sgn-BE-NL
sgn-CH-DE
function warning(reason, code, offset)
Called when an error occurs.
Parameters
reason
(string
) — reason for failure in Englishcode
(number
) — code for failureoffset
(number
) — index of place where the error occurred in the tag
Warnings
code | reason |
---|---|
1 | Too long variant, expected at most 8 characters |
2 | Too long extension, expected at most 8 characters |
3 | Too many extended language subtags, expected at most 3 subtags |
4 | Empty extension, extensions must have at least 2 characters of content |
5 | Too long private-use area, expected at most 8 characters |
6 | Found superfluous content after tag |
Types
This package is fully typed with TypeScript.
It exports the additional types Schema
, Extension
, Warning
, and
Options
.
Compatibility
This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. It also works in Deno and modern browsers.
Security
This package is safe.
Related
wooorm/bcp-47-match
— match BCP 47 language tags with language ranges per RFC 4647wooorm/bcp-47-normalize
— normalize, canonicalize, and format BCP 47 tagswooorm/iso-3166
— ISO 3166 codeswooorm/iso-639-2
— ISO 639-2 codeswooorm/iso-639-3
— ISO 639-3 codeswooorm/iso-15924
— ISO 15924 codeswooorm/un-m49
— UN M49 codes
Contribute
Yes please! See How to Contribute to Open Source.
License
MIT © Titus Wormer
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/wooorm/bcp-47/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/wooorm/bcp-47/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/wooorm/bcp-47/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:15
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
Project has not signed or included provenance with any releases.
Details
- Warn: release artifact 1.0.8 not signed: https://api.github.com/repos/wooorm/bcp-47/releases/33070905
- Warn: release artifact 1.0.7 not signed: https://api.github.com/repos/wooorm/bcp-47/releases/23142652
- Warn: release artifact 1.0.8 does not have provenance: https://api.github.com/repos/wooorm/bcp-47/releases/33070905
- Warn: release artifact 1.0.7 does not have provenance: https://api.github.com/repos/wooorm/bcp-47/releases/23142652
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Score
3.1
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More