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

bcp-47-match

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bcp-47-match - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

28

index.d.ts
export function basicFilter(
arg0: Tag | Tags,
arg1?: (Range | Ranges) | undefined
): Tags
tag: Tag | Tags,
ranges?: string | Ranges | undefined
): Tag
export function extendedFilter(
arg0: Tag | Tags,
arg1?: (Range | Ranges) | undefined
): Tags
tag: Tag | Tags,
ranges?: string | Ranges | undefined
): Tag
export function lookup(
arg0: Tag | Tags,
arg1?: (Range | Ranges) | undefined
tag: Tag | Tags,
ranges?: string | Ranges | undefined
): Tag

@@ -17,10 +17,10 @@ export type Tag = string

export type Ranges = Array<Range>
export type Check = (arg0: Tag, arg1: Range) => boolean
export type Check = (tag: Tag, range: Range) => boolean
export type Filter = (
arg0: Tag | string[],
arg1: (Range | Ranges) | undefined
) => Tags
tag: Tag | Tags,
ranges?: string | Ranges | undefined
) => Tag
export type Lookup = (
arg0: Tag | string[],
arg1: (Range | Ranges) | undefined
tag: Tag | Tags,
ranges?: string | Ranges | undefined
) => Tag
/**
* See https://tools.ietf.org/html/rfc4647#section-3.1
* See <https://tools.ietf.org/html/rfc4647#section-3.1>
* for more information on the algorithms.

@@ -8,11 +8,29 @@ */

* @typedef {string} Tag
* @typedef {Array.<Tag>} Tags
* @typedef {Array<Tag>} Tags
* @typedef {string} Range
* @typedef {Array.<Range>} Ranges
* @typedef {function(Tag, Range): boolean} Check
* @typedef {function(Tag|Tags, Range|Ranges=): Tags} Filter
* @typedef {function(Tag|Tags, Range|Ranges=): Tag} Lookup
* @typedef {Array<Range>} Ranges
*/
/**
* @callback Check
* @param {Tag} tag
* @param {Range} range
* @returns {boolean}
*/
/**
* @callback Filter
* @param {Tag|Tags} tag
* @param {Range|Ranges} [ranges]
* @returns {Tag}
*/
/**
* @callback Lookup
* @param {Tag|Tags} tag
* @param {Range|Ranges} [ranges]
* @returns {Tag}
*/
/**
* Factory to perform a filter or a lookup.

@@ -32,3 +50,3 @@ * This factory creates a function that accepts a list of tags and a list of

// prettier-ignore
var factory = (
const factory = (
/**

@@ -44,7 +62,7 @@ * @param {Check} check

* @param {Range|Ranges} [ranges='*']
* @returns {Tag|Tags}
* @returns {Tag|Tags|undefined}
*/
function match(tags, ranges) {
var left = cast(tags, 'tag')
var right = cast(
let left = cast(tags, 'tag')
const right = cast(
ranges === null || ranges === undefined ? '*' : ranges,

@@ -54,13 +72,7 @@ 'range'

/** @type {Tags} */
var matches = []
var rightIndex = -1
/** @type {Range} */
var range
/** @type {number} */
var leftIndex
/** @type {Tags} */
var next
const matches = []
let rightIndex = -1
while (++rightIndex < right.length) {
range = right[rightIndex].toLowerCase()
const range = right[rightIndex].toLowerCase()

@@ -70,4 +82,5 @@ // Ignore wildcards in lookup mode.

leftIndex = -1
next = []
let leftIndex = -1
/** @type {Tags} */
const next = []

@@ -101,3 +114,3 @@ while (++leftIndex < left.length) {

*/
export var basicFilter = factory(
export const basicFilter = factory(
/** @type {Check} */

@@ -118,10 +131,10 @@ function (tag, range) {

*/
export var extendedFilter = factory(
export const extendedFilter = factory(
/** @type {Check} */
function (tag, range) {
// 3.3.2.1
var left = tag.split('-')
var right = range.split('-')
var leftIndex = 0
var rightIndex = 0
const left = tag.split('-')
const right = range.split('-')
let leftIndex = 0
let rightIndex = 0

@@ -175,8 +188,6 @@ // 3.3.2.2

*/
export var lookup = factory(
export const lookup = factory(
/** @type {Check} */
function (tag, range) {
var right = range
/** @type {number} */
var index
let right = range

@@ -187,3 +198,3 @@ /* eslint-disable-next-line no-constant-condition */

index = right.lastIndexOf('-')
let index = right.lastIndexOf('-')

@@ -202,8 +213,8 @@ if (index < 0) return false

*
* @param {string|Array.<string>} values
* @param {string|Array<string>} values
* @param {string} name
* @returns {Array.<string>}
* @returns {Array<string>}
*/
function cast(values, name) {
var value = values && typeof values === 'string' ? [values] : values
const value = values && typeof values === 'string' ? [values] : values

@@ -210,0 +221,0 @@ if (!value || typeof value !== 'object' || !('length' in value)) {

{
"name": "bcp-47-match",
"version": "2.0.0",
"version": "2.0.1",
"description": "Match BCP 47 language tags with language ranges per RFC 4647",

@@ -42,4 +42,4 @@ "license": "MIT",

"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^9.0.0",
"rimraf": "^3.0.0",

@@ -49,10 +49,10 @@ "tape": "^5.0.0",

"typescript": "^4.0.0",
"xo": "^0.38.0"
"xo": "^0.46.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"prepublishOnly": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"

@@ -69,8 +69,3 @@ },

"xo": {
"prettier": true,
"rules": {
"import/no-mutable-exports": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
"prettier": true
},

@@ -95,4 +90,5 @@ "remarkConfig": {

"detail": true,
"strict": true
"strict": true,
"ignoreCatch": true
}
}

@@ -10,10 +10,8 @@ <!--lint disable no-html-->

Match [BCP 47][spec] language tags with “language ranges” per [RFC 4647][match],
as done by the `:lang()` pseudo-class in CSS, or the `Accept-Language` HTTP
header.
Match BCP 47 language tags with language ranges per RFC 4647.
Related to [`bcp-47`][bcp47].
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)

@@ -25,12 +23,26 @@ * [Use](#use)

* [`lookup(tags, ranges)`](#lookuptags-ranges)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)
## What is this?
This package can match [BCP 47][spec] language tags with “language ranges” per
[RFC 4647][match].
This is done by the `:lang()` pseudo class in CSS, the `Accept-Language` HTTP
header, and a few other places.
## When should I use this?
You can use this package if you want to choose a certain document based on
language tags.
## Install
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
instead of `require`d.
This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
[npm][]:
```sh

@@ -40,2 +52,16 @@ npm install bcp-47-match

In Deno with [Skypack][]:
```js
import * as bcp47Match from 'https://cdn.skypack.dev/bcp-47-match@2?dts'
```
In browsers with [Skypack][]:
```html
<script type="module">
import * as bcp47Match from 'https://cdn.skypack.dev/bcp-47-match@2?min'
</script>
```
## Use

@@ -71,3 +97,3 @@

This package exports the following identifiers: `basicFilter`, `extendedFilter`,
`lookup`.
and `lookup`.
There is no default export.

@@ -77,3 +103,4 @@

> [See Basic Filtering spec](https://tools.ietf.org/html/rfc4647#section-3.3.1)
> 👉 **Note**: See
> [Basic Filtering spec](https://tools.ietf.org/html/rfc4647#section-3.3.1)

@@ -87,21 +114,21 @@ Match language tags to a list of simple ranges.

| Basic Filter | * | de | de-CH | de-DE | de-*-DE | *-CH |
| Basic Filter | `*` | `de` | `de-CH` | `de-DE` | `de-*-DE` | `*-CH` |
| - | - | - | - | - | - | - |
| de | ✔︎ | ✔︎ | | | | |
| de-CH | ✔︎ | ✔︎ | ✔︎ | | | |
| de-CH-1996 | ✔︎ | ✔︎ | ✔︎ | | | |
| de-DE | ✔︎ | ✔︎ | | ✔︎ | | |
| de-DE-1996 | ✔︎ | ✔︎ | | ✔︎ | | |
| de-DE-x-goethe | ✔︎ | ✔︎ | | ✔︎ | | |
| de-Deva | ✔︎ | ✔︎ | | | | |
| de-Deva-DE | ✔︎ | ✔︎ | | | | |
| de-Latf-DE | ✔︎ | ✔︎ | | | | |
| de-Latn-DE | ✔︎ | ✔︎ | | | | |
| de-Latn-DE-1996 | ✔︎ | ✔︎ | | | | |
| de-x-DE | ✔︎ | ✔︎ | | | | |
| en | ✔︎ | | | | | |
| en-GB | ✔︎ | | | | | |
| zh | ✔︎ | | | | | |
| zh-Hans | ✔︎ | | | | | |
| zh-Hant | ✔︎ | | | | | |
| `de` | ✔︎ | ✔︎ | | | | |
| `de-CH` | ✔︎ | ✔︎ | ✔︎ | | | |
| `de-CH-1996` | ✔︎ | ✔︎ | ✔︎ | | | |
| `de-DE` | ✔︎ | ✔︎ | | ✔︎ | | |
| `de-DE-1996` | ✔︎ | ✔︎ | | ✔︎ | | |
| `de-DE-x-goethe` | ✔︎ | ✔︎ | | ✔︎ | | |
| `de-Deva` | ✔︎ | ✔︎ | | | | |
| `de-Deva-DE` | ✔︎ | ✔︎ | | | | |
| `de-Latf-DE` | ✔︎ | ✔︎ | | | | |
| `de-Latn-DE` | ✔︎ | ✔︎ | | | | |
| `de-Latn-DE-1996` | ✔︎ | ✔︎ | | | | |
| `de-x-DE` | ✔︎ | ✔︎ | | | | |
| `en` | ✔︎ | | | | | |
| `en-GB` | ✔︎ | | | | | |
| `zh` | ✔︎ | | | | | |
| `zh-Hans` | ✔︎ | | | | | |
| `zh-Hant` | ✔︎ | | | | | |

@@ -112,4 +139,6 @@ </details>

* `tags` (`string` or `Array.<string>`) — List of BCP-47 tags
* `ranges` (`string` or `Array.<string>`) — List of RFC 4647
* `tags` (`string` or `Array<string>`)
— list of BCP 47 tags
* `ranges` (`string` or `Array<string>`)
— list of RFC 4647
[basic ranges][basic-range]

@@ -120,8 +149,9 @@ (aka, matching `/^(\*|[a-z]{1,8}(-[a-z0-9]{1,8})*)$/i`)

`Array.<string>` — Possibly empty list of matching tags in the order they
matched.
Possibly empty list of matching tags in the order they matched
(`Array<string>`).
### `extendedFilter(tags[, ranges='*'])`
> [See Extended Filtering spec](https://tools.ietf.org/html/rfc4647#section-3.3.2)
> 👉 **Note**: See
> [Extended Filtering spec](https://tools.ietf.org/html/rfc4647#section-3.3.2)

@@ -134,21 +164,21 @@ Match language tags to a list of extended ranges.

| Extended Filter | * | de | de-CH | de-DE | de-*-DE | *-CH |
| Extended Filter | `*` | `de` | `de-CH` | `de-DE` | `de-*-DE` | `*-CH` |
| - | - | - | - | - | - | - |
| de | ✔︎ | ✔︎ | | | | |
| de-CH | ✔︎ | ✔︎ | ✔︎ | | | ✔︎ |
| de-CH-1996 | ✔︎ | ✔︎ | ✔︎ | | | ✔︎ |
| de-DE | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| de-DE-1996 | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| de-DE-x-goethe | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| de-Deva | ✔︎ | ✔︎ | | | | |
| de-Deva-DE | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| de-Latf-DE | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| de-Latn-DE | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| de-Latn-DE-1996 | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| de-x-DE | ✔︎ | ✔︎ | | | | |
| en | ✔︎ | | | | | |
| en-GB | ✔︎ | | | | | |
| zh | ✔︎ | | | | | |
| zh-Hans | ✔︎ | | | | | |
| zh-Hant | ✔︎ | | | | | |
| `de` | ✔︎ | ✔︎ | | | | |
| `de-CH` | ✔︎ | ✔︎ | ✔︎ | | | ✔︎ |
| `de-CH-1996` | ✔︎ | ✔︎ | ✔︎ | | | ✔︎ |
| `de-DE` | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| `de-DE-1996` | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| `de-DE-x-goethe` | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| `de-Deva` | ✔︎ | ✔︎ | | | | |
| `de-Deva-DE` | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| `de-Latf-DE` | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| `de-Latn-DE` | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| `de-Latn-DE-1996` | ✔︎ | ✔︎ | | ✔︎ | ✔︎ | |
| `de-x-DE` | ✔︎ | ✔︎ | | | | |
| `en` | ✔︎ | | | | | |
| `en-GB` | ✔︎ | | | | | |
| `zh` | ✔︎ | | | | | |
| `zh-Hans` | ✔︎ | | | | | |
| `zh-Hant` | ✔︎ | | | | | |

@@ -159,5 +189,6 @@ </details>

* `tags` (`string` or `Array.<string>`) — List of BCP-47 tags
* `ranges` (`string` or `Array.<string>`) — List of RFC 4647
[extended ranges][extended-range]
* `tags` (`string` or `Array<string>`)
— list of BCP 47 tags
* `ranges` (`string` or `Array<string>`)
— list of RFC 4647 [extended ranges][extended-range]
(aka, matching `/^(\*|[a-z]{1,8})(-(\*|[a-z0-9]{1,8}))*$/i`)

@@ -167,8 +198,9 @@

`Array.<string>` — Possibly empty list of matching tags in the order they
matched.
Possibly empty list of matching tags in the order they matched
(`Array<string>`).
### `lookup(tags, ranges)`
> [See Lookup spec](https://tools.ietf.org/html/rfc4647#section-3.4)
> 👉 **Note**: See
> [Lookup spec](https://tools.ietf.org/html/rfc4647#section-3.4)

@@ -182,21 +214,21 @@ Find the best language tag that matches a list of ranges.

| Lookup | * | de | de-CH | de-DE | de-*-DE | *-CH |
| Lookup | `*` | `de` | `de-CH` | `de-DE` | `de-*-DE` | `*-CH` |
| - | - | - | - | - | - | - |
| de | | ✔︎︎ | ✔︎︎ | ✔︎ | ✔︎ | ✔︎ |
| de-CH | | | ✔︎ | | | ✔︎ |
| de-CH-1996 | | | | | | ✔︎ |
| de-DE | | | | ✔︎ | | ✔︎ |
| de-DE-1996 | | | | | | ✔︎ |
| de-DE-x-goethe | | | | | | ✔︎ |
| de-Deva | | | | | | ✔︎ |
| de-Deva-DE | | | | | | ✔︎ |
| de-Latf-DE | | | | | | ✔︎ |
| de-Latn-DE | | | | | | ✔︎ |
| de-Latn-DE-1996 | | | | | | ✔︎ |
| de-x-DE | | | | | | ✔︎ |
| en | | | | | | ✔︎ |
| en-GB | | | | | | ✔︎ |
| zh | | | | | | ✔︎ |
| zh-Hans | | | | | | ✔︎ |
| zh-Hant | | | | | | ✔︎ |
| `de` | | ✔︎︎ | ✔︎︎ | ✔︎ | ✔︎ | ✔︎ |
| `de-CH` | | | ✔︎ | | | ✔︎ |
| `de-CH-1996` | | | | | | ✔︎ |
| `de-DE` | | | | ✔︎ | | ✔︎ |
| `de-DE-1996` | | | | | | ✔︎ |
| `de-DE-x-goethe` | | | | | | ✔︎ |
| `de-Deva` | | | | | | ✔︎ |
| `de-Deva-DE` | | | | | | ✔︎ |
| `de-Latf-DE` | | | | | | ✔︎ |
| `de-Latn-DE` | | | | | | ✔︎ |
| `de-Latn-DE-1996` | | | | | | ✔︎ |
| `de-x-DE` | | | | | | ✔︎ |
| `en` | | | | | | ✔︎ |
| `en-GB` | | | | | | ✔︎ |
| `zh` | | | | | | ✔︎ |
| `zh-Hans` | | | | | | ✔︎ |
| `zh-Hant` | | | | | | ✔︎ |

@@ -207,27 +239,47 @@ </details>

* `tags` (`string` or `Array.<string>`) — List of BCP-47 tags
* `ranges` (`string` or `Array.<string>`) — List of RFC 4647 basic ranges
(but `*` is ignored)
* `tags` (`string` or `Array<string>`)
— list of BCP 47 tags
* `ranges` (`string` or `Array<string>`)
— list of RFC 4647 basic ranges (but `*` is ignored)
###### Returns
`string?` — The first matching tag in `tags`, or `undefined` otherwise.
First matching tag in `tags`, `undefined` otherwise (`string?`).
## Types
This package is fully typed with [TypeScript][].
## Compatibility
This package is at least compatible with all maintained versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
It also works in Deno and modern browsers.
## Security
This package is safe.
## Related
* [`bcp-47`](https://github.com/wooorm/bcp-47)
— Parse and serialize BCP 47 language tags
* [`bcp-47-normalize`](https://github.com/wooorm/bcp-47-normalize)
— Normalize, canonicalize, and format BCP 47 tags
* [`iso-3166`](https://github.com/wooorm/iso-3166)
* [`wooorm/bcp-47`](https://github.com/wooorm/bcp-47)
— parse and serialize BCP 47 language tags
* [`wooorm/bcp-47-normalize`](https://github.com/wooorm/bcp-47-normalize)
— normalize, canonicalize, and format BCP 47 tags
* [`wooorm/iso-3166`](https://github.com/wooorm/iso-3166)
— ISO 3166 codes
* [`iso-639-2`](https://github.com/wooorm/iso-639-2)
* [`wooorm/iso-639-2`](https://github.com/wooorm/iso-639-2)
— ISO 639-2 codes
* [`iso-639-3`](https://github.com/wooorm/iso-639-3)
* [`wooorm/iso-639-3`](https://github.com/wooorm/iso-639-3)
— ISO 639-3 codes
* [`iso-15924`](https://github.com/wooorm/iso-15924)
* [`wooorm/iso-15924`](https://github.com/wooorm/iso-15924)
— ISO 15924 codes
* [`un-m49`](https://github.com/wooorm/un-m49)
* [`wooorm/un-m49`](https://github.com/wooorm/un-m49)
— UN M49 codes
## Contribute
Yes please!
See [How to Contribute to Open Source][contribute].
## License

@@ -257,2 +309,4 @@

[skypack]: https://www.skypack.dev
[license]: license

@@ -262,4 +316,8 @@

[bcp47]: https://github.com/wooorm/bcp-47
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[typescript]: https://www.typescriptlang.org
[contribute]: https://opensource.guide/how-to-contribute/
[spec]: https://tools.ietf.org/html/bcp47

@@ -266,0 +324,0 @@

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