Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 1, 2021
1 parent 491c2da commit 60948ee
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
strategy:
matrix:
node:
- lts/dubnium
- lts/erbium
- node
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
bcp-47-match.js
bcp-47-match.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
coverage/
bcp-47-match.js
bcp-47-match.min.js
*.json
*.md
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
'use strict'

// See https://tools.ietf.org/html/rfc4647#section-3.1
// for more information on the algorithms.

exports.basicFilter = factory(basic, true)
exports.extendedFilter = factory(extended, true)
exports.lookup = factory(lookup)
export var basicFilter = factory(basic, true)
export var extendedFilter = factory(extended, true)
export var lookup = factory(look)

// Basic Filtering (Section 3.3.1) matches a language priority list consisting
// of basic language ranges (Section 2.1) to sets of language tags.
function basic(tag, range) {
return range === '*' || tag === range || tag.indexOf(range + '-') > -1
return range === '*' || tag === range || tag.includes(range + '-')
}

// Extended Filtering (Section 3.3.2) matches a language priority list
Expand Down Expand Up @@ -63,7 +61,7 @@ function extended(tag, range) {
// Lookup (Section 3.4) matches a language priority list consisting of basic
// language ranges to sets of language tags to find the one exact language tag
// that best matches the range.
function lookup(tag, range) {
function look(tag, range) {
var right = range
var index

Expand Down Expand Up @@ -93,7 +91,10 @@ function factory(check, filter) {

function match(tags, ranges) {
var left = cast(tags, 'tag')
var right = cast(ranges == null ? '*' : ranges, 'range')
var right = cast(
ranges === null || ranges === undefined ? '*' : ranges,
'range'
)
var matches = []
var rightIndex = -1
var range
Expand Down
35 changes: 10 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,26 @@
"contributors": [
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"devDependencies": {
"browserify": "^17.0.0",
"c8": "^7.0.0",
"chalk": "^4.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"xo": "^0.38.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify index.js -s bcp47Match -o bcp-47-match.js",
"build-mangle": "browserify index.js -s bcp47Match -p tinyify -o bcp-47-match.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage"
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -59,25 +57,12 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"eqeqeq": [
"error",
"always",
{
"null": "ignore"
}
],
"no-eq-null": "off",
"unicorn/prefer-includes": "off"
"import/no-mutable-exports": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"remarkConfig": {
"plugins": [
"preset-wooorm",
Expand Down
43 changes: 23 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ Related to [`bcp-47`][bcp47].
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`match.basicFilter(tags[, ranges])`](#matchbasicfiltertags-ranges)
* [`match.extendedFilter(tags[, ranges])`](#matchextendedfiltertags-ranges)
* [`basicFilter(tags[, ranges])`](#basicfiltertags-ranges)
* [`extendedFilter(tags[, ranges])`](#extendedfiltertags-ranges)
* [`match.lookup(tags, ranges)`](#matchlookuptags-ranges)
* [Related](#related)
* [License](#license)

## Install

This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
instead of `require`d.

[npm][]:

```sh
Expand All @@ -35,26 +38,22 @@ npm install bcp-47-match
## Use

```js
var match = require('bcp-47-match')

var basic = match.basicFilter
var extended = match.extendedFilter
var lookup = match.lookup
import {basicFilter, extendedFilter, lookup} from 'bcp-47-match'

var tags = ['en-GB', 'de-CH', 'en', 'de']

console.log(basic(tags, '*')) // => [ 'en-GB', 'de-CH', 'en', 'de' ]
console.log(basic(tags, 'en')) // => [ 'en-GB', 'en' ]
console.log(basic(tags, 'en-GB')) // => [ 'en-GB' ]
console.log(basic(tags, ['en-GB', 'en'])) // => [ 'en-GB', 'en' ]
console.log(basic(tags, 'jp')) // => []
console.log(basicFilter(tags, '*')) // => [ 'en-GB', 'de-CH', 'en', 'de' ]
console.log(basicFilter(tags, 'en')) // => [ 'en-GB', 'en' ]
console.log(basicFilter(tags, 'en-GB')) // => [ 'en-GB' ]
console.log(basicFilter(tags, ['en-GB', 'en'])) // => [ 'en-GB', 'en' ]
console.log(basicFilter(tags, 'jp')) // => []

console.log(extended(tags, '*')) // => [ 'en-GB', 'de-CH', 'en', 'de' ]
console.log(extended(tags, 'en')) // => [ 'en-GB', 'en' ]
console.log(extended(tags, 'en-GB')) // => [ 'en-GB' ]
console.log(extended(tags, '*-GB')) // => [ 'en-GB' ]
console.log(extended(tags, ['en-GB', 'en'])) // => [ 'en-GB', 'en' ]
console.log(extended(tags, 'jp')) // => []
console.log(extendedFilter(tags, '*')) // => [ 'en-GB', 'de-CH', 'en', 'de' ]
console.log(extendedFilter(tags, 'en')) // => [ 'en-GB', 'en' ]
console.log(extendedFilter(tags, 'en-GB')) // => [ 'en-GB' ]
console.log(extendedFilter(tags, '*-GB')) // => [ 'en-GB' ]
console.log(extendedFilter(tags, ['en-GB', 'en'])) // => [ 'en-GB', 'en' ]
console.log(extendedFilter(tags, 'jp')) // => []

console.log(lookup(tags, 'en')) // => 'en'
console.log(lookup(tags, 'en-GB')) // => 'en-GB'
Expand All @@ -65,7 +64,11 @@ console.log(lookup(tags, 'jp')) // => undefined

## API

### `match.basicFilter(tags[, ranges])`
This package exports the following identifiers: `basicFilter`, `extendedFilter`,
`lookup`.
There is no default export.

### `basicFilter(tags[, ranges])`

> [See Basic Filtering spec](https://tools.ietf.org/html/rfc4647#section-3.3.1)
Expand Down Expand Up @@ -110,7 +113,7 @@ Returns a list of matching tags in the order they matched.
`Array.<string>` — Possibly empty list of matching tags in the order they
matched.

### `match.extendedFilter(tags[, ranges])`
### `extendedFilter(tags[, ranges])`

> [See Extended Filtering spec](https://tools.ietf.org/html/rfc4647#section-3.3.2)
Expand Down
32 changes: 13 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
'use strict'
import test from 'tape'
import chalk from 'chalk'
import {basicFilter, extendedFilter, lookup} from './index.js'

var test = require('tape')
var chalk = require('chalk')
var match = require('.')

var basic = match.basicFilter
var extended = match.extendedFilter
var lookup = match.lookup

test('basic(tags[, ranges="*"])', function (t) {
test('basicFilter(tags[, ranges="*"])', function (t) {
var basics = [
['de-de', null, ['de-de']],
['de-de', '*', ['de-de']],
Expand Down Expand Up @@ -58,28 +52,28 @@ test('basic(tags[, ranges="*"])', function (t) {
var index = -1

while (++index < basics.length) {
check(t, basic, basics[index])
check(t, basicFilter, basics[index])
}

t.throws(
function () {
basic()
basicFilter()
},
/^Error: Invalid tag `undefined`, expected non-empty string$/,
'should throw without tag'
)

t.throws(
function () {
basic('')
basicFilter('')
},
/^Error: Invalid tag ``, expected non-empty string$/,
'should throw with empty string tag'
)

t.throws(
function () {
basic(1)
basicFilter(1)
},
/^Error: Invalid tag `1`, expected non-empty string$/,
'should throw with invalid tag'
Expand All @@ -88,7 +82,7 @@ test('basic(tags[, ranges="*"])', function (t) {
t.end()
})

test('extended(tags[, ranges="*""])', function (t) {
test('extendedFilter(tags[, ranges="*""])', function (t) {
var extendeds = [
['de-de', null, ['de-de']],
['de-de', '*', ['de-de']],
Expand Down Expand Up @@ -158,28 +152,28 @@ test('extended(tags[, ranges="*""])', function (t) {
var index = -1

while (++index < extendeds.length) {
check(t, extended, extendeds[index])
check(t, extendedFilter, extendeds[index])
}

t.throws(
function () {
extended()
extendedFilter()
},
/^Error: Invalid tag `undefined`, expected non-empty string$/,
'should throw without tag'
)

t.throws(
function () {
extended('')
extendedFilter('')
},
/^Error: Invalid tag ``, expected non-empty string$/,
'should throw with empty string tag'
)

t.throws(
function () {
extended(1)
extendedFilter(1)
},
/^Error: Invalid tag `1`, expected non-empty string$/,
'should throw with invalid tag'
Expand Down

0 comments on commit 60948ee

Please sign in to comment.