Skip to content

Commit

Permalink
Refactor code-style 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 4388f03 commit a3220c3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 81 deletions.
125 changes: 44 additions & 81 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ var many = require('./many.json')
module.exports = normalize

var own = {}.hasOwnProperty
var assign = Object.assign

var parse = bcp47.parse
var stringify = bcp47.stringify

var collator = new Intl.Collator()

Expand All @@ -27,75 +23,63 @@ var emptyExtraFields = {

function normalize(value, options) {
var settings = options || {}
var forgiving = settings.forgiving
var warning = settings.warning
// 1. normalize and lowercase the tag (`sgn-be-fr` -> `sfb`).
var schema = parse(String(value || '').toLowerCase(), {
forgiving: forgiving,
warning: warning
})
var tag = stringify(schema)
var coreTag
// 2. Do fancy, expensive replaces (`ha-latn-gh` -> `ha-gh`).
var length = matches.length
var schema = bcp47.parse(String(value || '').toLowerCase(), settings)
var tag = bcp47.stringify(schema)
var index = -1
var d
var key

if (tag === '') {
if (!tag) {
return tag
}

while (++index < length) {
d = matches[index]
if (match.extendedFilter(tag, d.from).length > 0) {
replace(schema, d.from, d.to)
tag = stringify(schema)
// 2. Do fancy, expensive replaces (`ha-latn-gh` -> `ha-gh`).
while (++index < matches.length) {
if (match.extendedFilter(tag, matches[index].from).length) {
replace(schema, matches[index].from, matches[index].to)
tag = bcp47.stringify(schema)
}
}

// 3. Do basic field replaces (`en-840` -> `en-us`).
length = fields.length
index = -1

while (++index < length) {
d = fields[index]

if (remove(schema, d.from.field, d.from.value)) {
add(schema, d.to.field, d.to.value)
while (++index < fields.length) {
if (remove(schema, fields[index].from.field, fields[index].from.value)) {
add(schema, fields[index].to.field, fields[index].to.value)
}
}

// 4. Remove defaults (`nl-nl` -> `nl`).
coreTag = stringify(assign({}, schema, emptyExtraFields))
length = defaults.length
tag = bcp47.stringify(Object.assign({}, schema, emptyExtraFields))
index = -1

while (++index < length) {
d = defaults[index]

if (coreTag === d) {
replace(schema, d, d.split('-').slice(0, -1).join('-'))
coreTag = stringify(assign({}, schema, emptyExtraFields))
while (++index < defaults.length) {
if (tag === defaults[index]) {
replace(
schema,
defaults[index],
defaults[index].split('-').slice(0, -1).join('-')
)
tag = bcp47.stringify(Object.assign({}, schema, emptyExtraFields))
}
}

tag = stringify(schema)

// 5. Sort extensions on singleton.
schema.extensions.sort(compareSingleton)

// 6. Warn if fields (currently only regions) should be updated but have
// multiple choices.
if (warning) {
for (d in many) {
if (own.call(many[d], schema[d])) {
warning(
if (settings.warning) {
for (key in many) {
if (own.call(many[key], schema[key])) {
settings.warning(
'Deprecated ' +
d +
key +
' `' +
schema[d] +
schema[key] +
'`, expected one of `' +
many[d][schema[d]].join('`, `') +
many[key][schema[key]].join('`, `') +
'`',
null,
7
Expand All @@ -106,29 +90,29 @@ function normalize(value, options) {

// 7. Add proper casing back.
// Format script (ISO 15924) as titlecase (example: `Latn`):
if (schema.script !== null) {
if (schema.script) {
schema.script =
schema.script.charAt(0).toUpperCase() + schema.script.slice(1)
}

// Format region (ISO 3166) as uppercase (note: this doesn’t affect numeric
// codes, which is fine):
if (schema.region !== null) {
if (schema.region) {
schema.region = schema.region.toUpperCase()
}

return stringify(schema)
return bcp47.stringify(schema)
}

function replace(schema, from, to) {
var left = parse(from)
var right = parse(to)
var left = bcp47.parse(from)
var right = bcp47.parse(to)
var removed = []
var key

// Remove values from `from`:
for (key in left) {
if (defined(left[key]) && remove(schema, key, left[key])) {
if (left[key] && left[key].length && remove(schema, key, left[key])) {
removed.push(key)
}
}
Expand All @@ -138,8 +122,9 @@ function replace(schema, from, to) {
// Only add values that are defined on `to`, and that were either removed by
// `from` or are currently empty.
if (
defined(right[key]) &&
(removed.indexOf(key) !== -1 || !defined(schema[key]))
right[key] &&
right[key].length &&
(removed.indexOf(key) > -1 || !schema[key] || !schema[key].length)
) {
add(schema, key, right[key])
}
Expand All @@ -150,7 +135,6 @@ function remove(object, key, value) {
var removed = false
var current
var result
var length
var index
var item

Expand All @@ -161,15 +145,14 @@ function remove(object, key, value) {
current = object[key]
result = current

if (array(current)) {
if (current && typeof current === 'object') {
result = []
length = current.length
index = -1

while (++index < length) {
while (++index < current.length) {
item = current[index]

if (value.indexOf(item) === -1) {
if (value.indexOf(item) < 0) {
result.push(item)
} else {
removed = true
Expand All @@ -189,21 +172,19 @@ function remove(object, key, value) {
function add(object, key, value) {
var current = object[key]
var list
var length
var index
var item

if (array(current)) {
if (current && typeof current === 'object') {
list = [].concat(value)
length = list.length
index = -1

while (++index < length) {
while (++index < list.length) {
item = list[index]

/* istanbul ignore else - this currently can’t happen, but guard for the
* future. */
if (current.indexOf(item) === -1) {
if (current.indexOf(item) < 0) {
current.push(item)
}
}
Expand All @@ -212,24 +193,6 @@ function add(object, key, value) {
}
}

function defined(value) {
return value !== null && value.length !== 0
}

function array(value) {
return value !== null && typeof value === 'object'
}

function compareSingleton(left, right) {
return compare(singleton(left), singleton(right))
}

function compare(left, right) {
/* istanbul ignore next - equality can’t happen in BCP 47 tags, but let’s keep
* it in as a guard. */
return left === right ? 0 : collator.compare(left, right)
}

function singleton(value) {
return value.singleton
return collator.compare(left.singleton, right.singleton)
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"prettier": true,
"esnext": false,
"rules": {
"unicorn/explicit-length-check": "off",
"unicorn/prefer-includes": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/prefer-set-has": "off"
Expand Down

0 comments on commit a3220c3

Please sign in to comment.