{"name":"@bramus/specificity","version":"1.0.3","keywords":["css","specificity"],"author":{"url":"https://www.bram.us/","name":"Bramus Van Damme","email":"bramus@bram.us"},"license":"MIT","_id":"@bramus/specificity@1.0.3","maintainers":[{"name":"bramus","email":"bramus@bram.us"}],"homepage":"https://github.com/bramus/js-specificity#readme","bugs":{"url":"https://github.com/bramus/js-specificity/issues"},"dist":{"shasum":"01951ef9d9a967287ed9fabdc056ee2a6ae2fcc8","tarball":"http://registry-npm.ywwl.com/@bramus/specificity/download/@bramus/specificity-1.0.3.tgz","fileCount":8,"integrity":"sha512-Oyl20wtpz1nmzCbWiLYy2Yur2AKahxUZykSiCfgYjnOJkC0mfYxhsecHI+Biqo3BIBQt8Snj6l3XF7T1WNHT/w==","signatures":[{"sig":"MEUCIQDyNKh57wsaSeP9IbvDfQ8IgL/HfGNrVcYUQH/1zD6pBAIgbznK92v51IHxKseDQoIt5KGuzgJW4aWaQqbIa0/mY/M=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":1423446,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiFhWxACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpWdQ//RgjePrQWkKoPJeHN1AQw8y/ACAYQxlENQkP0nI1Ski71BZ3J\r\nSyCr3b4NltnqDaIIwmp0MRd11e2SpK0HtgxrrmXWAUVOG7uQoksovZdUOOif\r\nQ0nY/zkNERdV4gET7uJJAXdgs0o6BwGzR4URQX1I2dXAiz+5WpRAWTOo/ABM\r\n3tTpTtnsEYscUz4vDBbqr8qwmjtHwIqg4WCdBa3dYyf6SVh2i0nL+uO3xUzw\r\nsS/BQ622BsWBZ4+C8kiVeAwMXWammPmEuF6Ky420vCrdsncFVji8sh8UvVMO\r\n2e6FWTjHBe8wCV/O2pDxpUBeDV9XR5XmTSAlCIenXDwBI1UdLcZFOTJyBwnI\r\nUN/QIlzXGoQV2BS7loSK1rd3QwCgk5kbeA0WTb7qYBLv1RbU8QbRyszPk3Bz\r\nhZVEjabD0ywV1HBgmPy582a84OjFC4820luRPImlny7CHwh32p+RN9SyDmfW\r\n6swPL56h5qJOQzL76DegV8SL9OoUxaNkpAvqaUg0Yh63ciRTU9SkH7FURnAM\r\nkHG3fI+GF93pphakSKBEX4ca2r0/Xb/AYDVON6hsbqiSKMDDT9ELVqkcR+8V\r\nAm1druTIv1cf3Ng0EffEDqgYUW8yaPM8vQ3Ez3iFkTQ+Zu9x7x9154X3YfRV\r\nCZ+AGZDkuJuEf//9G1gflRNUVh65po6KRsg=\r\n=0HyI\r\n-----END PGP SIGNATURE-----\r\n","size":328301,"noattachment":false,"key":"/@bramus/specificity/-/@bramus/specificity-1.0.3.tgz"},"main":"dist/index.cjs.js","type":"module","export":{".":"./dist/index.esm.js","import":"./dist/index.esm.js","require":"./dist/index.cjs.js"},"module":"dist/index.esm.js","gitHead":"33df7fcc5107eb6e2d4a8d6a3f26171536794ea3","scripts":{"test":"mocha","build":"npm run build-esm && npm run build-cjs","pretest":"npm run build","build-cjs":"esbuild --bundle ./src/index.js --outfile=./dist/index.cjs.js --format=cjs --sourcemap --minify","build-esm":"esbuild --bundle ./src/index.js --outfile=./dist/index.esm.js --format=esm --sourcemap --minify","prepublish":"npm run build"},"_npmUser":{"name":"bramus","email":"bramus@bram.us"},"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.0.4"},"_hasShrinkwrap":false,"devDependencies":{"mocha":"^9.1.3","esbuild":"^0.14.5"},"_npmOperationalInternal":{"tmp":"tmp/specificity_1.0.3_1645614513583_0.9428702171942132","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":1645614513771,"_cnpm_publish_time":1645614513771,"dist-tags":{"latest":"2.4.3","next":"2.0.0-beta.11"}}