@medv/finder
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -10,3 +10,4 @@ export declare type Options = { | ||
threshold: number; | ||
maxNumberOfTries: number; | ||
}; | ||
export default function (input: Element, options?: Partial<Options>): string; |
@@ -76,2 +76,3 @@ "use strict"; | ||
threshold: 1000, | ||
maxNumberOfTries: 10000, | ||
}; | ||
@@ -312,4 +313,5 @@ config = __assign({}, defaults, options); | ||
} | ||
function optimize(path, input) { | ||
var i, newPath; | ||
function optimize(path, input, scope) { | ||
var i, newPath, newPathKey; | ||
if (scope === void 0) { scope = { counter: 0, visited: new Map() }; } | ||
return __generator(this, function (_a) { | ||
@@ -323,4 +325,12 @@ switch (_a.label) { | ||
if (!(i < path.length - 1)) return [3 /*break*/, 5]; | ||
if (scope.counter > config.maxNumberOfTries) { | ||
return [2 /*return*/]; // Okay At least I tried! | ||
} | ||
scope.counter += 1; | ||
newPath = path.slice(); | ||
newPath.splice(i, 1); | ||
newPathKey = selector(newPath); | ||
if (scope.visited.has(newPathKey)) { | ||
return [2 /*return*/]; | ||
} | ||
if (!(unique(newPath) && same(newPath, input))) return [3 /*break*/, 4]; | ||
@@ -330,3 +340,4 @@ return [4 /*yield*/, newPath]; | ||
_a.sent(); | ||
return [5 /*yield**/, __values(optimize(newPath, input))]; | ||
scope.visited.set(newPathKey, true); | ||
return [5 /*yield**/, __values(optimize(newPath, input, scope))]; | ||
case 3: | ||
@@ -333,0 +344,0 @@ _a.sent(); |
{ | ||
"name": "@medv/finder", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "CSS Selector Generator", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -22,6 +22,2 @@ ![finder](https://user-images.githubusercontent.com/141232/36463709-381f8c36-16fe-11e8-8fdc-fcbdd4f2a36c.png) | ||
<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 | ||
@@ -55,5 +51,7 @@ | ||
tagName: (name) => true, | ||
attr: (name, value) => false, | ||
seedMinLength: 1, | ||
optimizedMinLength: 2, | ||
threshold: 1000 | ||
threshold: 1000, | ||
maxNumberOfTries: 10_000, | ||
}) | ||
@@ -90,2 +88,6 @@ ``` | ||
#### `attr: (name: string, value: string) => boolean` | ||
Check if attr name can be used. | ||
#### `seedMinLength: number` | ||
@@ -110,16 +112,6 @@ | ||
### Comparison with [optimal-select](https://github.com/Autarc/optimal-select) | ||
#### `maxNumberOfTries: number` | ||
Max number of tries when we do the optimization. It is a trade-off between optimization and efficiency. | ||
Default `10_000` is good enough in most cases. | ||
`optimal-select` fails to generate selectors some times, and some times generates not unique selectors. | ||
`finder` generates shorter selectors than optimal-select. Also optimal-select now does not have tests and we have :) | ||
For example, on github.com page: | ||
| | finder | optimal-select | | ||
|----------|--------|----------------| | ||
| fails | 0 | 42 | | ||
| shortest | 404 | 38 | | ||
| longest | `.story:nth-child(3) .d-lg-flex:nth-child(2) > .width-full:nth-child(1)` | `[class="circle d-flex flex-column flex-justify-center text-center p-4 mx-auto mt-6 mb-3 m-md-3 bg-orange box-shadow-extra-large"] [class="d-block f0-light text-white lh-condensed-ultra mb-2"]` | | ||
| size | 2.9 kB | 4.58 kB | | ||
### Google Chrome Extension | ||
@@ -126,0 +118,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26302
363
122