Socket
Socket
Sign inDemoInstall

subtag

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

subtag

Language tag parser

    0.5.0latest
    GitHub
    npm

Version published
Maintainers
1
Weekly downloads
146,566
increased by1.62%

Weekly downloads

Readme

Source

subtag

Language tag parser. Parse language tags into subtags.

api

  • subtag(tag) parse tag into subtags object
  • subtag.split(tag) split tag into subtags array
  • subtag.language(tag) get primary language subtag
  • subtag.extlang(tag) get extended language subtag
  • subtag.script(tag) get script subtag
  • subtag.region(tag) get region subtag

notes

  • parsing is done via regex
  • unpresent subtags will be an empty string
  • separator can be dashes (standard) or underscores

setup

install via npm or yarn

npm install subtag --save
yarn add subtag

usage

require usage

var subtag = require('subtag')

import usage

import subtag from 'subtag'

examples

objects
subtag('ja-JP') // {language: 'ja', extlang: '', script: '', region: 'JP'}
subtag('es-AR') // {language: 'es', extlang: '', script: '', region: 'AR'}
arrays
subtag.split('yue') // ["yue"]
subtag.split('es-419') // ["es", "419"]
subtag.split('zh-Hant-HK') // ["zh", "Hant", "HK"]
subtag.split('en-90210') // ["en"] because 90210 is fake
subtags
subtag.language('en') // 'en'
subtag.extlang('en') // ''
subtag.script('en') // ''
subtag.region('en') // ''

subtag.language('en-US') // 'en'
subtag.extlang('en-US') // ''
subtag.script('en-US') // ''
subtag.region('en-US') // 'US'

subtag.language('zh-yue') // 'zh'
subtag.extlang('zh-yue') // 'yue'
subtag.script('zh-yue') // ''
subtag.region('zh-yue') // ''

subtag.language('zh-Hans') // 'zh'
subtag.extlang('zh-Hans') // ''
subtag.script('zh-Hans') // 'Hans'
subtag.region('zh-Hans') // ''

structure

language-extlang-script-region-variant-extension-privateuse

typepatternconvention
language2-letter or 3-letterlowercase
extlang3-letterlowercase
script4-lettertitlecase
region2-letter or 3-numberuppercase

.pattern

Regex patterns are exposed for validation

subtag.language.pattern.test('en') // true
subtag.language.pattern.test('ast') // true
subtag.language.pattern.test('fake') // false
subtag.extlang.pattern.test('yue') // true
subtag.script.pattern.test('Hans') // true
subtag.region.pattern.test('US') // true
subtag.region.pattern.test('005') // true
subtag.region.pattern.test('90210') // false

compatibility

Works in Node.js and ES5+ browsers

Keywords

FAQs

Last updated on 30 Jul 2017

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc