Skip to content

Commit

Permalink
Use strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 4, 2021
1 parent 253e5d2 commit affa68b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
36 changes: 27 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
/**
* 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.
*/

/**
* @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}
*/

/**
Expand Down Expand Up @@ -39,7 +57,7 @@ const factory = (
/**
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges='*']
* @returns {Tag|Tags}
* @returns {Tag|Tags|undefined}
*/
function match(tags, ranges) {
let left = cast(tags, 'tag')
Expand Down Expand Up @@ -183,9 +201,9 @@ export const lookup = factory(
/**
* Validate tags or ranges, and cast them to arrays.
*
* @param {string|Array.<string>} values
* @param {string|Array<string>} values
* @param {string} name
* @returns {Array.<string>}
* @returns {Array<string>}
*/
function cast(values, name) {
const value = values && typeof values === 'string' ? [values] : values
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"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"
},
"prettier": {
Expand Down Expand Up @@ -84,6 +84,7 @@
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
"strict": true,
"ignoreCatch": true
}
}
29 changes: 19 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/**
* @typedef {import('tape').Test} Test
* @typedef {import('./index.js').Filter} Filter
* @typedef {import('./index.js').Lookup} Lookup
*/

import test from 'tape'
import chalk from 'chalk'
import {basicFilter, extendedFilter, lookup} from './index.js'

test('basicFilter(tags[, ranges="*"])', function (t) {
/** @type {Array<[string|string[], string|string[]|undefined, unknown]>} */
const basics = [
['de-de', null, ['de-de']],
['de-de', undefined, ['de-de']],
['de-de', '*', ['de-de']],
['de-DE', '*', ['de-DE']],
['de-DE-1996', '*', ['de-DE-1996']],
Expand Down Expand Up @@ -57,7 +64,7 @@ test('basicFilter(tags[, ranges="*"])', function (t) {

t.throws(
function () {
// @ts-ignore runtime
// @ts-expect-error runtime
basicFilter()
},
/^Error: Invalid tag `undefined`, expected non-empty string$/,
Expand All @@ -74,7 +81,7 @@ test('basicFilter(tags[, ranges="*"])', function (t) {

t.throws(
function () {
// @ts-ignore runtime
// @ts-expect-error runtime
basicFilter(1)
},
/^Error: Invalid tag `1`, expected non-empty string$/,
Expand All @@ -85,8 +92,9 @@ test('basicFilter(tags[, ranges="*"])', function (t) {
})

test('extendedFilter(tags[, ranges="*""])', function (t) {
/** @type {Array<[string|string[], string|string[]|undefined, unknown]>} */
const extendeds = [
['de-de', null, ['de-de']],
['de-de', undefined, ['de-de']],
['de-de', '*', ['de-de']],
['de-DE', '*', ['de-DE']],
['de-Latn-DE', '*', ['de-Latn-DE']],
Expand Down Expand Up @@ -159,7 +167,7 @@ test('extendedFilter(tags[, ranges="*""])', function (t) {

t.throws(
function () {
// @ts-ignore runtime
// @ts-expect-error runtime
extendedFilter()
},
/^Error: Invalid tag `undefined`, expected non-empty string$/,
Expand All @@ -176,7 +184,7 @@ test('extendedFilter(tags[, ranges="*""])', function (t) {

t.throws(
function () {
// @ts-ignore runtime
// @ts-expect-error runtime
extendedFilter(1)
},
/^Error: Invalid tag `1`, expected non-empty string$/,
Expand All @@ -187,9 +195,10 @@ test('extendedFilter(tags[, ranges="*""])', function (t) {
})

test('lookup(tags[, ranges="*"])', function (t) {
/** @type {Array<[string|string[], string|string[]|undefined, unknown]>} */
const lookups = [
// Wildcards have no effect in `lookup`
['de-de', null, undefined],
['de-de', undefined, undefined],
['de-de', '*', undefined],
['de-DE', '*', undefined],
['en-GB', 'de-ch', undefined],
Expand Down Expand Up @@ -229,9 +238,9 @@ test('lookup(tags[, ranges="*"])', function (t) {
})

/**
* @param {import('tape').Test} t
* @param {import('./index.js').Filter | import('./index.js').Lookup} fn
* @param {Array.<string|Array.<string>>} options
* @param {Test} t
* @param {Filter|Lookup} fn
* @param {[string|string[], string|string[]|undefined, unknown]} options
*/
function check(t, fn, options) {
t.deepEqual(
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit affa68b

Please sign in to comment.