Skip to content

Commit

Permalink
Refactor to improve bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 30, 2020
1 parent 2da9400 commit e9855f0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 46 deletions.
50 changes: 19 additions & 31 deletions convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
module.exports = convert

function convert(test) {
if (typeof test === 'string') {
return typeFactory(test)
if (test == null) {
return ok
}

if (test === null || test === undefined) {
return ok
if (typeof test === 'string') {
return typeFactory(test)
}

if (typeof test === 'object') {
return ('length' in test ? anyFactory : matchesFactory)(test)
return 'length' in test ? anyFactory(test) : allFactory(test)
}

if (typeof test === 'function') {
Expand All @@ -22,52 +22,40 @@ function convert(test) {
throw new Error('Expected function, string, or object as test')
}

function convertAll(tests) {
var results = []
var length = tests.length
var index = -1

while (++index < length) {
results[index] = convert(tests[index])
}

return results
}

// Utility assert each property in `test` is represented in `node`, and each
// values are strictly equal.
function matchesFactory(test) {
return matches
function allFactory(test) {
return all

function matches(node) {
function all(node) {
var key

for (key in test) {
if (node[key] !== test[key]) {
return false
}
if (node[key] !== test[key]) return
}

return true
}
}

function anyFactory(tests) {
var checks = convertAll(tests)
var length = checks.length
var checks = []
var index = -1

while (++index < tests.length) {
checks[index] = convert(tests[index])
}

return matches
return any

function matches() {
function any() {
var index = -1

while (++index < length) {
while (++index < checks.length) {
if (checks[index].apply(this, arguments)) {
return true
}
}

return false
}
}

Expand All @@ -77,7 +65,7 @@ function typeFactory(test) {
return type

function type(node) {
return Boolean(node && node.type === test)
return node && node.type === test
}
}

Expand Down
19 changes: 7 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,25 @@ is.convert = convert

// Assert if `test` passes for `node`.
// When a `parent` node is known the `index` of node should also be given.
// eslint-disable-next-line max-params
function is(node, test, index, parent, context) {
var hasParent = parent !== null && parent !== undefined
var hasIndex = index !== null && index !== undefined
var check = convert(test)

if (
hasIndex &&
index != null &&
(typeof index !== 'number' || index < 0 || index === Infinity)
) {
throw new Error('Expected positive finite index or child node')
throw new Error('Expected positive finite index')
}

if (hasParent && (!is(parent) || !parent.children)) {
if (parent != null && (!is(parent) || !parent.children)) {
throw new Error('Expected parent node')
}

if (!node || !node.type || typeof node.type !== 'string') {
return false
}

if (hasParent !== hasIndex) {
if ((parent == null) !== (index == null)) {
throw new Error('Expected both parent and index')
}

return Boolean(check.call(context, node, index, parent))
return node && node.type && typeof node.type === 'string'
? Boolean(check.call(context, node, index, parent))
: false
}
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
"prettier": true,
"esnext": false,
"rules": {
"eqeqeq": [
"error",
"always",
{
"null": "ignore"
}
],
"max-params": "off",
"no-eq-null": "off",
"unicorn/prefer-type-error": "off",
"unicorn/prefer-reflect-apply": "off"
},
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ test('unist-util-is', function (t) {
function () {
is(node, null, -1, parent)
},
/Expected positive finite index or child node/,
/Expected positive finite index/,
'should throw when `index` is invalid (#1)'
)

t.throws(
function () {
is(node, null, Infinity, parent)
},
/Expected positive finite index or child node/,
/Expected positive finite index/,
'should throw when `index` is invalid (#2)'
)

t.throws(
function () {
is(node, null, false, parent)
},
/Expected positive finite index or child node/,
/Expected positive finite index/,
'should throw when `index` is invalid (#3)'
)

Expand Down

0 comments on commit e9855f0

Please sign in to comment.