Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return type of filter #4

Merged
merged 3 commits into from
Mar 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 49 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@
*/

/**
* @callback Filter
* @param {Tag|Tags} tag
* @param {Range|Ranges} [ranges]
* @returns {Tag}
* @typedef {Tags} FilterResponseType
* @typedef {Tag|undefined} LookupResponseType
*
*/

/**
* @callback Lookup
* @param {Tag|Tags} tag
* @param {Range|Ranges} [ranges]
* @returns {Tag}
*
* @template {true|false} IsFilter
* @typedef {IsFilter extends true ? FilterResponseType : LookupResponseType} FilterOrLookupResponse
*
*/

/**
*
* @template {true|false} IsFilter
* @callback FilterOrLookup
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges='*']
* @returns {FilterOrLookupResponse<IsFilter>}
*
*/

/**
* @typedef {FilterOrLookup<true>} Filter
* @typedef {FilterOrLookup<false>} Lookup
*/

/**
Expand All @@ -39,27 +53,23 @@
* This match function iterates over ranges, and for each range,
* iterates over tags. That way, earlier ranges matching any tag have
* precedence over later ranges.
*
* @type {{
* (check: Check, filter: true): Filter
* (check: Check, filter?: false): Lookup
* }}
*/
// prettier-ignore
const factory = (
/**
* @param {Check} check
* @param {boolean} [filter=false]
*/
function (check, filter) {
return match

/**
* @template {true|false} IsFilter
* @param {Check} check
* @param {IsFilter} filter
* @returns {FilterOrLookup<IsFilter>}
*/
function factory(check, filter) {

/**
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges='*']
* @returns {Tag|Tags|undefined}
* @returns {FilterOrLookupResponse<IsFilter>}
*/
function match(tags, ranges) {
function match (tags, ranges) {
let left = cast(tags, 'tag')
const right = cast(
ranges === null || ranges === undefined ? '*' : ranges,
Expand All @@ -82,7 +92,10 @@ const factory = (
while (++leftIndex < left.length) {
if (check(left[leftIndex].toLowerCase(), range)) {
// Exit if this is a lookup and we have a match.
if (!filter) return left[leftIndex]
if (!filter) {
return /** @type {FilterOrLookupResponse<IsFilter>} */ (left[leftIndex]);
}

matches.push(left[leftIndex])
} else {
next.push(left[leftIndex])
Expand All @@ -94,17 +107,19 @@ const factory = (

// If this is a filter, return the list. If it’s a lookup, we didn’t find
// a match, so return `undefined`.
return filter ? matches : undefined

return /** @type {FilterOrLookupResponse<IsFilter>} */ (filter ? matches : undefined);
}
}
)

/** @type {FilterOrLookup<IsFilter>} */
return match;
}

/**
* Basic Filtering (Section 3.3.1) matches a language priority list consisting
* of basic language ranges (Section 2.1) to sets of language tags.
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges]
* @returns {Tags}
*
* @type Filter
*/
export const basicFilter = factory(
/** @type {Check} */
Expand All @@ -118,9 +133,8 @@ export const basicFilter = factory(
* Extended Filtering (Section 3.3.2) matches a language priority list
* consisting of extended language ranges (Section 2.2) to sets of language
* tags.
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges]
* @returns {Tags}
*
* @type Filter
*/
export const extendedFilter = factory(
/** @type {Check} */
Expand Down Expand Up @@ -174,9 +188,7 @@ export const extendedFilter = factory(
* 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.
* @param {Tag|Tags} tags
* @param {Range|Ranges} [ranges]
* @returns {Tag}
* @type Lookup
*/
export const lookup = factory(
/** @type {Check} */
Expand All @@ -195,7 +207,8 @@ export const lookup = factory(

right = right.slice(0, index)
}
}
},
false
)

/**
Expand Down