1.6.6 • Published 8 months ago

@escapace/accept-language-parser v1.6.6

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
8 months ago

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']
1.6.6

8 months ago

1.6.5

8 months ago

1.6.4

11 months ago

1.6.3

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.0

1 year ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.3.5

2 years ago

1.4.0

2 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago