Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 19, 2021
1 parent b2c5358 commit 4b4e243
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 189 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage/
*.json
*.md
24 changes: 8 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
'use strict'
import {any} from './lib/any.js'
import {parse} from './lib/parse.js'

exports.matches = matches
exports.selectAll = selectAll
exports.select = select

var any = require('./lib/any')
var parse = require('./lib/parse')

function matches(selector, node) {
return Boolean(
any(parse(selector), node, {one: true, shallow: true, any: any})[0]
)
export function matches(selector, node) {
return Boolean(any(parse(selector), node, {one: true, shallow: true, any})[0])
}

function select(selector, node) {
return any(parse(selector), node, {one: true, any: any})[0] || null
export function select(selector, node) {
return any(parse(selector), node, {one: true, any})[0] || null
}

function selectAll(selector, node) {
return any(parse(selector), node, {any: any})
export function selectAll(selector, node) {
return any(parse(selector), node, {any})
}
35 changes: 15 additions & 20 deletions lib/any.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
'use strict'

module.exports = match

var zwitch = require('zwitch')
var pseudo = require('./pseudo')
var test = require('./test')
var nest = require('./nest')
import {zwitch} from 'zwitch'
import {pseudo} from './pseudo.js'
import {test} from './test.js'
import {nest} from './nest.js'

var type = zwitch('type', {
unknown: unknownType,
invalid: invalidType,
handlers: {
selectors: selectors,
ruleSet: ruleSet,
rule: rule
}
handlers: {selectors, ruleSet, rule}
})

function match(query, node, state) {
export function any(query, node, state) {
return query && node ? type(query, node, state) : []
}

Expand Down Expand Up @@ -50,7 +42,7 @@ function rule(query, tree, state) {
null,
configure(query, {
scopeNodes: tree.type === 'root' ? tree.children : [tree],
iterator: iterator,
iterator,
one: state.one,
shallow: state.shallow,
any: state.any
Expand All @@ -76,7 +68,7 @@ function configure(query, state) {
var index = -1

while (++index < pseudos.length) {
if (pseudo.needsIndex.indexOf(pseudos[index].name) > -1) {
if (pseudo.needsIndex.includes(pseudos[index].name)) {
state.index = true
break
}
Expand All @@ -85,12 +77,14 @@ function configure(query, state) {
return state
}

/* istanbul ignore next - Shouldn’t be invoked, all data is handled. */
// Shouldn’t be invoked, all data is handled.
/* c8 ignore next 3 */
function unknownType(query) {
throw new Error('Unknown type `' + query.type + '`')
}

/* istanbul ignore next - Shouldn’t be invoked, parser gives correct data. */
// Shouldn’t be invoked, parser gives correct data.
/* c8 ignore next 3 */
function invalidType() {
throw new Error('Invalid type')
}
Expand Down Expand Up @@ -118,12 +112,13 @@ function collector(one) {

function collectOne(node) {
if (one) {
/* istanbul ignore if - shouldn’t happen, safeguards performance problems. */
/* Shouldn’t happen, safeguards performance problems. */
/* c8 ignore next */
if (found) throw new Error('Cannot collect multiple nodes')

found = true
}

if (result.indexOf(node) < 0) result.push(node)
if (!result.includes(node)) result.push(node)
}
}
21 changes: 9 additions & 12 deletions lib/attribute.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
'use strict'

module.exports = match

var zwitch = require('zwitch')
import {zwitch} from 'zwitch'

var handle = zwitch('operator', {
unknown: unknownOperator,
Expand All @@ -16,7 +12,7 @@ var handle = zwitch('operator', {
}
})

function match(query, node) {
export function attribute(query, node) {
var attrs = query.attrs
var index = -1

Expand All @@ -29,25 +25,25 @@ function match(query, node) {

// [attr]
function exists(query, node) {
return node[query.name] != null
return node[query.name] !== null && node[query.name] !== undefined
}

// [attr=value]
function exact(query, node) {
return node[query.name] != null && String(node[query.name]) === query.value
return exists(query, node) && String(node[query.name]) === query.value
}

// [attr~=value]
function containsArray(query, node) {
var value = node[query.name]

if (value == null) return false
if (value === null || value === undefined) return false

// If this is an array, and the query is contained in it, return true.
if (
typeof value === 'object' &&
'length' in value &&
value.indexOf(query.value) > -1
value.includes(query.value)
) {
return true
}
Expand Down Expand Up @@ -79,10 +75,11 @@ function ends(query, node) {
// [attr*=value]
function containsString(query, node) {
var value = node[query.name]
return typeof value === 'string' && value.indexOf(query.value) > -1
return typeof value === 'string' && value.includes(query.value)
}

/* istanbul ignore next - Shouldn’t be invoked, Parser throws an error instead. */
// Shouldn’t be invoked, Parser throws an error instead.
/* c8 ignore next 3 */
function unknownOperator(query) {
throw new Error('Unknown operator `' + query.operator + '`')
}
6 changes: 1 addition & 5 deletions lib/name.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
'use strict'

module.exports = match

function match(query, node) {
export function name(query, node) {
return query.tagName === '*' || query.tagName === node.type
}
37 changes: 19 additions & 18 deletions lib/nest.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
'use strict'

module.exports = match

var zwitch = require('zwitch')
import {zwitch} from 'zwitch'

var own = {}.hasOwnProperty

Expand All @@ -17,36 +13,38 @@ var handle = zwitch('nestingOperator', {
}
})

function match(query, node, index, parent, state) {
export function nest(query, node, index, parent, state) {
return handle(query, node, index, parent, state)
}

/* istanbul ignore next - Shouldn’t be invoked, parser gives correct data. */
// Shouldn’t be invoked, parser gives correct data.
/* c8 ignore next 3 */
function unknownNesting(query) {
throw new Error('Unexpected nesting `' + query.nestingOperator + '`')
}

function topScan(query, node, index, parent, state) {
/* istanbul ignore if - Shouldn’t happen. */
// Shouldn’t happen.
/* c8 ignore next 3 */
if (parent) {
throw new Error('topScan is supposed to be called from the root node')
}

state.iterator.apply(null, arguments)
state.iterator(...arguments)

if (!state.shallow) descendant.apply(this, arguments)
if (!state.shallow) descendant.call(this, ...arguments)
}

function descendant(query, node, index, parent, state) {
var previous = state.iterator

state.iterator = iterator

child.apply(this, arguments)
child.call(this, ...arguments)

function iterator(_, node, index, parent, state) {
state.iterator = previous
previous.apply(this, arguments)
previous.call(this, ...arguments)
state.iterator = iterator

if (state.one && state.found) return
Expand All @@ -56,13 +54,14 @@ function descendant(query, node, index, parent, state) {
}

function child(query, node, index, parent, state) {
if (!node.children || !node.children.length) return
if (!node.children || node.children.length === 0) return

walkIterator(query, node, state).each().done()
}

function adjacentSibling(query, node, index, parent, state) {
/* istanbul ignore if - Shouldn’t happen. */
// Shouldn’t happen.
/* c8 ignore next */
if (!parent) return

walkIterator(query, parent, state)
Expand All @@ -73,7 +72,8 @@ function adjacentSibling(query, node, index, parent, state) {
}

function generalSibling(query, node, index, parent, state) {
/* istanbul ignore if - Shouldn’t happen. */
// Shouldn’t happen.
/* c8 ignore next */
if (!parent) return

walkIterator(query, parent, state)
Expand All @@ -91,7 +91,7 @@ function walkIterator(query, parent, state) {
return {
prefillTypeIndex: rangeDefaults(prefillTypeIndex),
each: rangeDefaults(each),
done: done
done
}

function done() {
Expand Down Expand Up @@ -149,8 +149,9 @@ function walkIterator(query, parent, state) {
return rangeDefault

function rangeDefault(start, end) {
if (start == null || start < 0) start = 0
if (end == null || end > siblings.length) end = siblings.length
if (start === null || start === undefined || start < 0) start = 0
if (end === null || end === undefined || end > siblings.length)
end = siblings.length
return iterator.call(this, start, end)
}
}
Expand Down
31 changes: 14 additions & 17 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
'use strict'
import {CssSelectorParser} from 'css-selector-parser'
import nthCheck from 'nth-check'
import {zwitch} from 'zwitch'

module.exports = parse
var nth = new Set([
'nth-child',
'nth-last-child',
'nth-of-type',
'nth-last-of-type'
])

var Parser = require('css-selector-parser').CssSelectorParser
var zwitch = require('zwitch')
var nthCheck = require('nth-check').default

var nth = ['nth-child', 'nth-last-child', 'nth-of-type', 'nth-last-of-type']

var parser = new Parser()
var parser = new CssSelectorParser()

var compile = zwitch('type', {
handlers: {
selectors: selectors,
ruleSet: ruleSet,
rule: rule
}
handlers: {selectors, ruleSet, rule}
})

parser.registerAttrEqualityMods('~', '^', '$', '*')
parser.registerSelectorPseudos('any', 'matches', 'not', 'has')
parser.registerNestingOperators('>', '+', '~')

function parse(selector) {
export function parse(selector) {
if (typeof selector !== 'string') {
throw new TypeError('Expected `string` as selector, not `' + selector + '`')
}
Expand Down Expand Up @@ -53,8 +50,8 @@ function rule(query) {
while (++index < pseudos.length) {
pseudo = pseudos[index]

if (nth.indexOf(pseudo.name) > -1) {
pseudo.value = nthCheck(pseudo.value)
if (nth.has(pseudo.name)) {
pseudo.value = nthCheck.default(pseudo.value)
pseudo.valueType = 'function'
}
}
Expand Down
Loading

0 comments on commit 4b4e243

Please sign in to comment.