Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@escapace/accept-language-parser

Package Overview
Dependencies
Maintainers
0
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@escapace/accept-language-parser

Parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality.

  • 1.6.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
713
increased by177.43%
Maintainers
0
Weekly downloads
 
Created
Source

accept-language-parser

Parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality.

Installation

npm install @escapace/accept-language-parser

API

parse(acceptLanguageHeader)
import { parse } from '@escapace/accept-language-parser'

const languages = parse('en-GB,en;q=0.8')

console.log(languages)

Output will be:

;[
  {
    code: 'en',
    region: 'GB',
    quality: 1.0,
  },
  {
    code: 'en',
    region: undefined,
    quality: 0.8,
  },
]

Output is always sorted in quality order from highest -> lowest. As per the HTTP spec, omitting the quality value implies 1.0.

pick(supportedLangugagesArray, acceptLanguageHeader, options = {})
import { pick } from '@escapace/accept-language-parser'

const language = pick(['fr-CA', 'fr-FR', 'fr'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8')

console.log(language)

Output will be:

'fr-CA'

The options supports the loose flag which allows partial matching on supported languages.

For example:

pick(['fr', 'en'], 'en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8', {
  loose: true,
})

Would return:

'fr'

In loose mode the order of supportedLanguagesArray matters, as it is the first partially matching language that is returned. It means that if you want to pick more specific langauges first, you should list it first as well.

For example:

;['fr-CA', 'fr']

FAQs

Package last updated on 14 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc