{"_id":"@bramus/specificity@2.4.3","bin":{"specificity":"bin/cli.js"},"bugs":{"url":"https://github.com/bramus/specificity/issues"},"dist":{"shasum":"66586208e89e37327e509a94eef6545d76915db8","tarball":"http://registry-npm.ywwl.com/@bramus/specificity/download/@bramus/specificity-2.4.3.tgz","fileCount":16,"integrity":"sha512-0GEz8qJWokkUVYQYRukmkSHIFNoBH8QTreeHrAFbvz1XFYc3Y+puhMs8PQ5x8GNugiC2l5jvDBXKLbBIByeSBw==","signatures":[{"sig":"MEQCIAn134FCaYPJoSo9jvM2zFqZH5b8FcZNzvj4AMDsPaCgAiBPM1274k9CxuaiFJgCN8QkimbCHqV8aKfA9o6gJBu3EQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDVD6AsVayNFqyh+1/InmJtCXbXytfrTY8N6OXHd9ydqAIgH/HyTwOMy0GrySQjZX84DxpsCIy7ghO4Ybj8obN4TtA="}],"unpackedSize":802282,"size":184262,"noattachment":false,"key":"/@bramus/specificity/-/@bramus/specificity-2.4.3.tgz"},"main":"./dist/index.cjs","name":"@bramus/specificity","type":"module","types":"./index.d.ts","unpkg":"./dist/index.js","author":{"url":"https://www.bram.us/","name":"Bramus Van Damme","email":"bramus@bram.us"},"module":"./dist/index.js","exports":{".":{"import":"./dist/index.js","browser":"./dist/index.js","require":"./dist/index.cjs"},"./core":{"import":"./src/core/index.js"},"./sort":{"import":"./src/util/sort.js"},"./util":{"import":"./src/util/index.js"},"./filter":{"import":"./src/util/filter.js"},"./compare":{"import":"./src/util/compare.js"}},"gitHead":"fb7646605b67558539b243f9e311d414911a4e18","license":"MIT","scripts":{"lint":"prettier --check '{src,test}/**/*.{ts,tsx,js,jsx}'","test":"mocha","build":"npm run build-esm && npm run build-cjs","format":"prettier --write '{src,test}/**/*.{ts,tsx,js,jsx}'","prepack":"npm run prevent-dirty-tree && npm run test","pretest":"npm run build","benchmark":"node ./benchmark/bench.cjs","build-cjs":"esbuild --bundle ./src/index.js --outfile=./dist/index.cjs --format=cjs --sourcemap --minify","build-esm":"esbuild --bundle ./src/index.js --outfile=./dist/index.js --format=esm --sourcemap --minify","prepublish":"npm run build","preversion":"npm run prevent-dirty-tree && npm run test","rc-version":"npm version $(semver $npm_package_version -i prerelease --preid rc)","prebenchmark":"npm run build","final-release":"npm version $(semver $npm_package_version -i)","beta-version-major":"npm version $(semver $npm_package_version -i premajor --preid beta)","beta-version-minor":"npm version $(semver $npm_package_version -i preminor --preid beta)","beta-version-patch":"npm version $(semver $npm_package_version -i prerelease --preid beta)","prevent-dirty-tree":"exit $(git status --porcelain | wc -l)"},"version":"2.4.3","_npmUser":{"name":"bramus","email":"bramus@bram.us"},"homepage":"https://github.com/bramus/specificity#readme","jsdelivr":"./dist/index.js","keywords":["css","specificity"],"repository":{"url":"git+https://github.com/bramus/specificity.git","type":"git"},"_npmVersion":"11.8.0","description":"Calculate specificity of a CSS Selector","directories":{},"maintainers":[{"name":"bramus","email":"bramus@bram.us"}],"_nodeVersion":"25.2.1","dependencies":{"css-tree":"^3.2.1"},"_hasShrinkwrap":false,"devDependencies":{"mocha":"^12.0.3","semver":"^7.8.5","esbuild":"^0.28.2","prettier":"^3.9.9","benchmark":"^2.1.4","microtime":"^3.1.1"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/specificity_2.4.3_1791229108170_0.5648413108373942"},"readme":"[![Calculate CSS Specificity](./screenshots/calculate-specificity.png)](https://codepen.io/bramus/pen/WNXyoYm)\n\n# Specificity\n\n`@bramus/specificity` is a package to calculate the specificity of CSS Selectors. It also includes some convenience functions to compare, sort, and filter an array of specificity values.\n\nSupports [Selectors Level 4](https://www.w3.org/TR/selectors-4/), including those special cases `:is()`, `:where()`, `:not()`, etc.\n\nDemo: [https://codepen.io/bramus/pen/WNXyoYm](https://codepen.io/bramus/pen/WNXyoYm)\n\n## Installation\n\n```bash\nnpm i @bramus/specificity\n```\n\n## Usage / Example\n\nAt its core, `@bramus/specificity` exposes a `Specificity` class. Its static `calculate` method can be used to calculate the specificity of a given CSS [Selector List](https://www.w3.org/TR/selectors-4/#grouping) string.\n\n```js\nimport Specificity from '@bramus/specificity';\n\nconst specificities = Specificity.calculate('header:where(#top) nav li:nth-child(2n), #doormat');\n```\n\nBecause `calculate` accepts a [Selector List](https://www.w3.org/TR/selectors-4/#grouping) — which can contain more than 1 [Selector](https://www.w3.org/TR/selectors-4/#selector) — it will always return an array, with each entry being a `Specificity` instance — one per found selector.\n\n```js\nconst specificities = Specificity.calculate('header:where(#top) nav li:nth-child(2n), #doormat');\nspecificities.map((s) => s.toString()); // ~> [\"(0,1,3)\",\"(1,0,0)\"]\n```\n\n💡 If you know you’re passing only a single Selector into `calculate()`, you can use JavaScript’s built-in destructuring to keep your variable names clean.\n\n```js\nconst [s] = Specificity.calculate('header:where(#top) nav li:nth-child(2n)');\ns.toString(); // ~> \"(0,1,3)\"\n```\n\n💡 Under the hood, `@bramus/specificity` uses [CSSTree](https://github.com/csstree/csstree) to do the parsing of strings to Selectors. As a result, the `calculate` method also accepts a [CSSTree AST](https://github.com/csstree/csstree/blob/master/docs/ast.md) of the types `Selector` and `SelectorList`.\n\nIf you have a pre-parsed CSSTree AST of the type `Selector` you can pass it into `Specificity.calculateForAST()`. It [performs slightly better](#benchmark) than `Specificity.calculate()` as it needs to check fewer things. It differs from `Specificity.calculate()` in that it does not return an array of `Specificity` instances but only a single value.\n\n## The Return Format\n\nA calculated specificity is represented as an instance of the `Specificity` class. The `Specificity` class includes methods to get the specificity value in a certain format, along with some convenience methods to compare it against other instances.\n\n```js\n// 🚀 Thunderbirds are go!\nimport Specificity from '@bramus/specificity';\n\n// ✨ Calculate specificity for each Selector in the given Selector List\nconst specificities = Specificity.calculate('header:where(#top) nav li:nth-child(2n), #doormat');\n\n// 🚚 The values in the array are instances of the Specificity class\nconst s = specificities[0]; // Instance of Specificity\n\n// 👀 Read the specificity value using one of its accessors\ns.value; // { a: 0, b: 1, c: 3 }\ns.a; // 0\ns.b; // 1\ns.c; // 3\n\n// 🛠 Convert the calculated value to various formats using one of the toXXX() instance methods\ns.toString(); // \"(0,1,3)\"\ns.toArray(); // [0, 1, 3]\ns.toObject(); // { a: 0, b: 1, c: 3 }\n\n// 💡 Extract the matched selector string\ns.selectorString(); // \"header:where(#top) nav li:nth-child(2n)\"\n\n// 🔀 Use one of its instance comparison methods to compare it to another Specificity instance\ns.isEqualTo(specificities[1]); // false\ns.isGreaterThan(specificities[1]); // false\ns.isLessThan(specificities[1]); // true\n\n// 💻 Don’t worry about JSON.stringify()\nJSON.stringify(s);\n// {\n//    \"selector\": 'header:where(#top) nav li:nth-child(2n)',\n//    \"asObject\": { \"a\": 0, \"b\": 1, \"c\": 3 },\n//    \"asArray\": [0, 1, 3],\n//    \"asString\": \"(0,1,3)\",\n// }\n```\n\n## Utility Functions (Static Methods)\n\nThis package also exposes some utility functions to work with specificities. These utility functions are all exposed as static methods on the `Specificity` class.\n\n-   Comparing:\n\n    -   `Specificity.compare(s1, s2)`: Compares s1 to s2. Returns a value that can be:\n        -   `> 0` = Sort s2 before s1 _(i.e. s1 is more specific than s2)_\n        -   `0` = Keep original order of s1 and s2 _(i.e. s1 and s2 are equally specific)_\n        -   `< 0` = Sort s1 before s2 _(i.e. s1 is less specific than s2)_\n    -   `Specificity.equals(s1, s2)`: Returns `true` if s1 and s2 have the same specificity. If not, `false` is returned.\n    -   `Specificity.greaterThan(s1, s2)`: Returns `true` if s1 has a higher specificity than s2. If not, `false` is returned.\n    -   `Specificity.lessThan(s1, s2)`: Returns `true` if s1 has a lower specificity than s2. If not, `false` is returned.\n\n-   Sorting:\n\n    -   `Specificity.sortAsc(s1, s2, …, sN)`: Sorts the given specificities in ascending order _(low specificity to high specificity)_\n    -   `Specificity.sortDesc(s1, s2, …, sN)`: Sorts the given specificities in descending order _(high specificity to low specificity)_\n\n-   Filtering:\n    -   `Specificity.min(s1, s2, …, sN)`: Filters out the value with the lowest specificity\n    -   `Specificity.max(s1, s2, …, sN)`: Filters out the value with the highest specificity\n\nA specificity passed into any of these utility functions can be any of:\n\n-   An instance of the included `Specificity` class\n-   A simple Object such as `{'a': 1, 'b': 0, 'c': 2}`\n\n## Utility Functions (Standalone)\n\nAll static methods the `Specificity` class exposes are also exported as standalone functions using [Subpath Exports](https://nodejs.org/api/packages.html#subpath-exports).\n\nIf you're only interested in including some of these functions into your project you can import them from their Subpath. As a result, your bundle size will be reduced greatly _(except for including the standalone `calculate`, as it returns an array of `Specificity` instances that relies on the whole lot)_\n\n```js\nimport { calculate, calculateForAST } from '@bramus/specificity/core';\nimport { compare, equals, greaterThan, lessThan } from '@bramus/specificity/compare';\nimport { min, max } from '@bramus/specificity/filter';\nimport { sortAsc, sortDesc } from '@bramus/specificity/sort';\n```\n\n## Type Definitions\n\nAlthough `@bramus/specificity` is written in Vanilla JavaScript, it does include [Type Definitions](https://www.typescriptlang.org/docs/handbook/2/type-declarations.html) which are exposed via its `package.json`.\n\n## Binary/CLI\n\n`@bramus/specificity` exposes a binary named `specificity` to calculate the specificity of a given selector list on the CLI. For each selector that it finds, it'll print out the calculated specificity as a string on a new line.\n\n```bash\n$ specificity \"header:where(#top) nav li:nth-child(2n), #doormat\"\n(0,1,3)\n(1,0,0)\n```\n\n## Benchmark\n\nA benchmark is included, which you can invoke using `npm run benchmark`.\n\nSample results (tested on a MacBook Air M3):\n\n```\nSpecificity.calculate(string) x 420,682 ops/sec ±0.34% (98 runs sampled)\nSpecificity.calculate(ast) - using SelectorList x 8,994,080 ops/sec ±0.25% (98 runs sampled)\nSpecificity.calculate(ast) - using Selector x 11,054,856 ops/sec ±0.39% (91 runs sampled)\nSpecificity.calculateForAST(ast) x 12,652,322 ops/sec ±0.35% (96 runs sampled)\n```\n\n## License\n\n`@bramus/specificity` is released under the MIT public license. See the enclosed `LICENSE` for details.\n\n## Acknowledgements\n\nThe idea to create this package was sparked by [the wonderful Specificity Calculator created by Kilian Valkhof / Polypane](https://polypane.app/css-specificity-calculator/), a highly educational tool that not only calculates the specificity, but also explains which parts are responsible for it.\n\nThe heavy lifting of doing the actual parsing of Selectors is done by [CSSTree](https://github.com/csstree/csstree).\n","publish_time":1791229108330,"_cnpm_publish_time":1791229108330,"dist-tags":{"latest":"2.4.3","next":"2.0.0-beta.11"}}