Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed May 2, 2020
1 parent f9acd2e commit f38ede7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Anton Medvedev
Copyright (c) 2018–2020 Anton Medvedev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 3 additions & 6 deletions README.md
Expand Up @@ -19,10 +19,6 @@
npm install @medv/finder
```

<a href="https://www.patreon.com/antonmedv">
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
</a>

## Usage

```js
Expand Down Expand Up @@ -54,7 +50,8 @@ const selector = finder(event.target, {
attr: (name, value) => false,
seedMinLength: 1,
optimizedMinLength: 2,
threshold: 1000
threshold: 1000,
maxNumberOfTries: 10_000,
})
```

Expand Down Expand Up @@ -111,7 +108,7 @@ Default `1000` is good enough in most cases.

#### `maxNumberOfTries: number`
Max number of tries when we do the optimization. It is a trade-off between optimization and efficiency.
Default `10000` is good enough in most cases.
Default `10_000` is good enough in most cases.

### Google Chrome Extension

Expand Down
24 changes: 13 additions & 11 deletions src/index.ts
Expand Up @@ -26,14 +26,9 @@ export type Options = {
maxNumberOfTries: number
}

type OptimizeScopeType = {
counter: number
visited: Map<string, boolean>
}

let config: Options
let rootDocument: Document | Element

let rootDocument: Document | Element
export default function (input: Element, options?: Partial<Options>) {
if (input.nodeType !== Node.ELEMENT_NODE) {
throw new Error(`Can't generate CSS selector for non-element node type.`)
Expand Down Expand Up @@ -300,17 +295,24 @@ function sort(paths: Iterable<Path>): Path[] {
return Array.from(paths).sort((a, b) => penalty(a) - penalty(b))
}

// TODO: there's some overlap cases in this algorithm
function* optimize(path: Path, input: Element, scope: OptimizeScopeType = { counter: 0, visited: new Map<string, boolean>() }) {
type Scope = {
counter: number
visited: Map<string, boolean>
}

function* optimize(path: Path, input: Element, scope: Scope = { counter: 0, visited: new Map<string, boolean>() }) {
if (path.length > 2 && path.length > config.optimizedMinLength) {
for (let i = 1; i < path.length - 1; i++) {
// Okay At least I tried!
if(scope.counter > config.maxNumberOfTries) return
if(scope.counter > config.maxNumberOfTries) {
return // Okay At least I tried!
}
scope.counter += 1
const newPath = [...path]
newPath.splice(i, 1)
const newPathKey = selector(newPath)
if(scope.visited.has(newPathKey)) return
if(scope.visited.has(newPathKey)) {
return
}
if (unique(newPath) && same(newPath, input)) {
yield newPath
scope.visited.set(newPathKey, true)
Expand Down

0 comments on commit f38ede7

Please sign in to comment.