Registry / serialization / subtag

subtag

JSON →
library0.5.0jsnpmunverified

The `subtag` library, currently at version 0.5.0 (last updated March 2017), provides a lightweight utility for parsing BCP 47 language tags (e.g., `en-US`, `zh-Hant-HK`) into their constituent components like language, extended language, script, and region. It offers methods to parse a tag into a structured object, an array of subtags, or extract individual subtags directly. The parsing logic is implemented using regular expressions, making it efficient for common use cases but it does not perform comprehensive validation against the full IANA language subtag registry. This package differentiates itself through its simplicity and focused parsing capabilities. It supports both Node.js and ES5+ browsers, though its unmaintained status means it may not be suitable for modern applications requiring full BCP 47 compliance or active support.

npm install subtag
INSTALL
IMPORT
SIG · SUBTAG
S
subtag
serializationjavascriptv0.5.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

subtag
import subtag from 'subtag'
import { subtag } from 'subtag'
This library uses a default export for its primary functionality in ESM environments.
subtag (CommonJS)
const subtag = require('subtag')
import subtag from 'subtag'
For CommonJS environments, use `require` to import the module's default export.
subtag.language
import subtag from 'subtag'; const lang = subtag.language('en-US')
import { language } from 'subtag'
Individual subtag parsers like `language` are properties of the default `subtag` export, not named exports directly from the package.

Demonstrates parsing various BCP 47 language tags into structured objects, arrays of subtags, extracting specific components, and using the exposed regex patterns for validation.

import subtag from 'subtag'; // Parse a simple language tag const tag1 = 'en-US'; console.log(`--- Parsing '${tag1}' ---`); console.log('Object representation:', subtag(tag1)); console.log('Split into array:', subtag.split(tag1)); console.log('Primary language:', subtag.language(tag1)); console.log('Region:', subtag.region(tag1)); // Parse a more complex language tag const tag2 = 'zh-Hant-HK'; console.log(`\n--- Parsing '${tag2}' ---`); console.log('Object representation:', subtag(tag2)); console.log('Split into array:', subtag.split(tag2)); console.log('Primary language:', subtag.language(tag2)); console.log('Script:', subtag.script(tag2)); // Demonstrating exposed regex patterns for validation console.log(`\n--- Pattern Tests ---`); console.log('Is "en" a valid language? ', subtag.language.pattern.test('en')); console.log('Is "ast" a valid language? ', subtag.language.pattern.test('ast')); console.log('Is "90210" a valid region? ', subtag.region.pattern.test('90210'));
Debug
Known issues
breakingThe `subtag` package has not been updated since March 2017 (version 0.5.0). It is considered abandoned and will not receive bug fixes, security updates, or new features. Using unmaintained software carries inherent risks.
fix
For production applications or any new development, it is strongly recommended to use an actively maintained BCP 47 language tag parser library (e.g., `language-tags`, `negotiator`, or browser's `Intl.Locale` if supported) to ensure up-to-date standards compliance, security, and ongoing support.
affects: >=0.5.0
gotchaThe library's parsing is based on simple regular expressions and does not perform comprehensive validation against the full IANA language subtag registry. It may incorrectly parse, or fail to parse, valid but complex BCP 47 tags, or conversely, accept invalid tags that happen to match its regex patterns.
fix
For strict BCP 47 validation, including handling of complex subtag sequences (variants, extensions, privateuse), and robust checking against the official IANA registry, utilize a library specifically designed for such rigorous compliance.
affects: >=0.1.0
gotchaThe main `subtag(tag)` function returns an object structured only for `language`, `extlang`, `script`, and `region`. It does not explicitly parse or provide separate fields for 'variant', 'extension', or 'privateuse' subtags within this object.
fix
If access to all components of a BCP 47 tag (including variants, extensions, and privateuse) is required, use `subtag.split(tag)` to get an array of all subtags, and then manually process the remaining parts beyond the primary components.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: subtag is not a function
This error typically occurs when attempting to use the `subtag` function without correctly importing it. This can happen due to a mismatch between CommonJS `require` and ES module `import` syntax, or incorrect transpilation settings.
fix
Ensure you are using `import subtag from 'subtag'` for ES Modules or `const subtag = require('subtag')` for CommonJS environments. If using TypeScript, verify that `esModuleInterop` is enabled in your `tsconfig.json` if you are mixing import styles.
subtag.language('en-90210') returns 'en' (or other unexpected partial parsing)
The library's regex-based parsing is designed for common subtag patterns and may not fully validate or extract components from non-standard or syntactically incorrect language tags, leading to incomplete or surprising results for unrecognised components.
fix
The library is primarily a basic parser. For applications requiring strict validation against the BCP 47 standard and comprehensive handling of all subtag types (including privateuse, extensions, and valid but uncommon region codes), consider a more robust and actively maintained internationalization library.
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Resources
subtag — npm install subtag · libregistry