Skip to content

Commit

Permalink
Refactor code to improve bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 26, 2020
1 parent c1207d5 commit e71140a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 91 deletions.
133 changes: 44 additions & 89 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,57 @@
// See https://tools.ietf.org/html/rfc4647#section-3.1
// for more information on the algorithms.

var dash = '-'
var asterisk = '*'

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

// 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) {
tag = lower(tag)
range = lower(range)
return range === asterisk || tag === range || tag.indexOf(range + dash) !== -1
return range === '*' || tag === range || tag.indexOf(range + '-') > -1
}

// Extended Filtering (Section 3.3.2) matches a language priority list
// consisting of extended language ranges (Section 2.2) to sets of language
// tags.
function extended(tag, range) {
// 3.3.2.1
var tags = lower(tag).split(dash)
var ranges = lower(range).split(dash)
var length = ranges.length
var rangeIndex = -1
var tagIndex = -1

tag = tags[++tagIndex]
range = ranges[++rangeIndex]
var left = tag.split('-')
var right = range.split('-')
var leftIndex = 0
var rightIndex = 0

// 3.3.2.2
if (range !== asterisk && range !== tag) {
if (right[rightIndex] !== '*' && left[leftIndex] !== right[rightIndex]) {
return false
}

tag = tags[++tagIndex]
range = ranges[++rangeIndex]
leftIndex++
rightIndex++

// 3.3.2.3
while (rangeIndex < length) {
while (rightIndex < right.length) {
// 3.3.2.3.A
if (range === asterisk) {
range = ranges[++rangeIndex]
if (right[rightIndex] === '*') {
rightIndex++
continue
}

// 3.3.2.3.B
if (!tag) {
return false
}
if (!left[leftIndex]) return false

// 3.3.2.3.C
if (tag === range) {
tag = tags[++tagIndex]
range = ranges[++rangeIndex]
if (left[leftIndex] === right[rightIndex]) {
leftIndex++
rightIndex++
continue
}

// 3.3.2.3.D
if (tag.length === 1) {
return false
}
if (left[leftIndex].length === 1) return false

// 3.3.2.3.E
tag = tags[++tagIndex]
leftIndex++
}

// 3.3.2.4
Expand All @@ -77,29 +64,20 @@ function extended(tag, range) {
// language ranges to sets of language tags to find the one exact language tag
// that best matches the range.
function lookup(tag, range) {
var pos

tag = lower(tag)
range = lower(range)
var right = range
var index

/* eslint-disable no-constant-condition */
/* eslint-disable-next-line no-constant-condition */
while (true) {
/* eslint-enable no-constant-condition */
if (range === asterisk || tag === range) {
return true
}
if (right === '*' || tag === right) return true

pos = range.lastIndexOf(dash)
index = right.lastIndexOf('-')

if (pos === -1) {
return false
}
if (index < 0) return false

if (pos > 3 && range.charAt(pos - 2) === dash) {
pos -= 2
}
if (right.charAt(index - 2) === '-') index -= 2

range = range.slice(0, pos)
right = right.slice(0, index)
}
}

Expand All @@ -114,61 +92,42 @@ function factory(check, filter) {
return match

function match(tags, ranges) {
var values = normalize(tags, ranges)
var result = []
var next
var tagIndex
var tagLength
var tag
var rangeIndex
var rangeLength
var left = cast(tags, 'tag')
var right = cast(ranges == null ? '*' : ranges, 'range')
var matches = []
var rightIndex = -1
var range
var matches

tags = values.tags
ranges = values.ranges
rangeLength = ranges.length
rangeIndex = -1
var leftIndex
var next

while (++rangeIndex < rangeLength) {
range = ranges[rangeIndex]
while (++rightIndex < right.length) {
range = right[rightIndex].toLowerCase()

// Ignore wildcards in lookup mode.
if (!filter && range === asterisk) {
continue
}
if (!filter && range === '*') continue

tagLength = tags.length
tagIndex = -1
leftIndex = -1
next = []

while (++tagIndex < tagLength) {
tag = tags[tagIndex]
matches = check(tag, range)
;(matches ? result : next).push(tag)

// Exit if this is a lookup and we have a match.
if (!filter && matches) {
return tag
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]
matches.push(left[leftIndex])
} else {
next.push(left[leftIndex])
}
}

tags = next
left = next
}

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

// Normalize options.
function normalize(tags, ranges) {
ranges = ranges === undefined || ranges === null ? asterisk : ranges

return {tags: cast(tags, 'tag'), ranges: cast(ranges, 'range')}
}

// Validate tags or ranges, and cast them to arrays.
function cast(values, name) {
var value = values && typeof values === 'string' ? [values] : values
Expand All @@ -181,7 +140,3 @@ function cast(values, name) {

return value
}

function lower(value) {
return value.toLowerCase()
}
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify index.js -s bcp47Match > bcp-47-match.js",
"build-mangle": "browserify index.js -s bcp47Match -p tinyify > bcp-47-match.min.js",
"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",
Expand All @@ -62,6 +62,14 @@
"prettier": true,
"esnext": false,
"rules": {
"eqeqeq": [
"error",
"always",
{
"null": "ignore"
}
],
"no-eq-null": "off",
"unicorn/prefer-includes": "off"
}
},
Expand Down

0 comments on commit e71140a

Please sign in to comment.