{"name":"@bramus/specificity","version":"2.0.0-beta.10","keywords":["css","specificity"],"author":{"url":"https://www.bram.us/","name":"Bramus Van Damme","email":"bramus@bram.us"},"license":"MIT","_id":"@bramus/specificity@2.0.0-beta.10","maintainers":[{"name":"bramus","email":"bramus@bram.us"}],"homepage":"https://github.com/bramus/js-specificity#readme","bugs":{"url":"https://github.com/bramus/js-specificity/issues"},"bin":{"specificity":"bin/cli.js"},"dist":{"shasum":"c219c76c1295dd64cf53153a594ccb3464cdc121","tarball":"http://registry-npm.ywwl.com/@bramus/specificity/download/@bramus/specificity-2.0.0-beta.10.tgz","fileCount":18,"integrity":"sha512-T/KCD03GhXbpev+BCDOStcg4wFKoAnaKFbT+KXDVySCzBFkJ8ETw0rm84cchwpVSJ6NRpjlDksMjYk60lxn9iQ==","signatures":[{"sig":"MEUCIQDj2aaJwCXYuuhwQDXPHC5Z/IvBRVM9Z7YoQ1wmCCcXRQIgWD9MpRKtM2Wk5tL+d/7GpA97vGovviydEWNpQ25PSI8=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":649642,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiK5YwACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrpYQ/7BxQbdNuHdicoH7Ay6GKb+KxUFW/w8LB0T4kEZMjDWCpSU2d7\r\nnp4dte5A4wVrxo7OT0GYU9qsuGAnpWJcp9/wPThy6CjB5XZqjE2AEI5B5kAE\r\nWYaHEHW+5JcJurVd5gAhj3k2/7N9J9prz/x2iHl1ykzQucOYqi4jpgXLnzYP\r\nrXE4ydCbRPV5D/G+yM9Xdn/SGf3+Fk922qh+4rHt1vui9MxsI6EeLovTFngv\r\n2rgmQ/Ep1W/8OuD5eOTjPpkIwMBt5ijxBv8w5V4dTLsjc4gM+8ovGodmGK9Y\r\nSVceyQpy5MZ0+ICZ6eno9v6fj3Kl7ENDadkv3l97xGyzD5O1A7I68ES1OodG\r\ni1DBxr+CamrUegilggJcaMqAsYU4zxfSfhzXa76bfSWILRXQV8CRao1Ths0R\r\n1A+i11MbukEhqIlrY70QalaFCxlaYdVY0J4oVIOxkzsdO2BrG5WFAwMvJIA8\r\n192nGr5M1R/AqR6esro722exbUA00ma6s9FuKmElIxHI2GiOF5ygHULAFsXw\r\nHRSqm6gRka/He1LSLJaZylAZ8OO3svhr4SS/+zXdbK4kSlyhW69mL23mvWTq\r\nDIILrfrcOIfzjX+rdJwnztuyB53DS1uFpJllzqXyDID8Fz5vOcwlyF0istta\r\ntcznXMOR4k2GjCotb36sw1YVkuhl4SIWVuM=\r\n=KSs4\r\n-----END PGP SIGNATURE-----\r\n","size":150666,"noattachment":false,"key":"/@bramus/specificity/-/@bramus/specificity-2.0.0-beta.10.tgz"},"main":"./dist/index.cjs","type":"module","types":"./index.d.ts","unpkg":"./dist/index.js","module":"./dist/index.js","exports":{".":{"import":"./dist/index.js","browser":"./dist/index.js","require":"./dist/index.cjs"},"./sort":{"import":"./src/util/sort.js"},"./util":{"import":"./src/util/index.js"},"./types":{"import":"./src/type/index.js"},"./filter":{"import":"./src/util/filter.js"},"./compare":{"import":"./src/util/compare.js"}},"gitHead":"e23d95bdc589e8487aee258a23d83b288058a7c9","scripts":{"lint":"prettier --check '{src,test}/**/*.{ts,tsx,js,jsx}'","test":"mocha","build":"npm run build-esm && npm run build-cjs","prepack":"npm run prevent-dirty-tree","pretest":"npm run build","prettier":"prettier --write '{src,test}/**/*.{ts,tsx,js,jsx}'","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)","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)"},"_npmUser":{"name":"bramus","email":"bramus@bram.us"},"jsdelivr":"./dist/index.js","repository":{"url":"git+https://github.com/bramus/js-specificity.git","type":"git"},"_npmVersion":"8.1.4","description":"Calculate specificity of a CSS Selector","directories":{},"_nodeVersion":"17.2.0","dependencies":{"css-tree":"^2.1.0"},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"mocha":"^9.1.3","semver":"^7.3.5","esbuild":"^0.14.5","prettier":"2.5.1"},"_npmOperationalInternal":{"tmp":"tmp/specificity_2.0.0-beta.10_1647023664783_0.6358452513362887","host":"s3://npm-registry-packages"},"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":1647023664952,"_cnpm_publish_time":1647023664952,"dist-tags":{"latest":"2.4.3","next":"2.0.0-beta.11"}}