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

intl-parse-accept-language

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

intl-parse-accept-language

Returns an array of `locale` strings, sorted by quality, given the string from an HTTP `Accept-Language` header.

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
56K
increased by3.41%
Maintainers
1
Weekly downloads
 
Created
Source

intl-parse-accept-language

Returns an array of locale strings, sorted by quality, given the string from an HTTP Accept-Language header.

  • Returned value is compatible with the locales argument of Intl.DateTimeFormat
  • Written in TypeScript and fully typed
  • 100% test coverage

Example

import { parseAcceptLanguage } from 'intl-parse-accept-language';

const locales = acceptLanguage('en-US,es;q=0.6,en;q=0.8,*;q=0.1');
// => ['en-US', 'en', 'es']

Configuration

parseAcceptLanguage accepts an optional configuration object as the second argument

optiondescription
validateA validate callback that is called for each locale. If the locale is valid, return the locale as a string. Otherwise return undefined, null, or an empty Array. Should the callback throw an error, the error will be caught and the locale will be ignored.
ignoreWildcardA boolean that if set to true, the wildcard locale * will be returned in the array. If set to false, the wildcard locale * will be ignored. Defaults to true.

Pro Tip

If you're using the locales array to do date formatting, you may want to use Intl.DateTimeFormat.supportedLocalesOf as the validate callback. This way only locales that are valid for date formatting will be returned.

For example:

import { parseAcceptLanguage } from 'intl-parse-accept-language';

const locales = parseAcceptLanguage('en-US,foo-BAR', {
  validate: Intl.DateTimeFormat.supportedLocalesOf,
});
// => ['en-US']

Getting Started

Install the library with your package manager of choice, e.g.:

npm i intl-parse-accept-language

or

yarn add intl-parse-accept-language

Other works

  • parse-accept-language
  • accept-language-parser

License

© 2022 Donavon West. Released under MIT license.

Keywords

FAQs

Package last updated on 24 Mar 2022

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