Package Exports
- string-extract-class-names
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (string-extract-class-names) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
string-extract-class-names
Extract class (or id) name from a string
Install
npm i string-extract-class-names
Here's what you'll get:
Type | Key in package.json |
Path | Size |
---|---|---|---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports |
main |
dist/string-extract-class-names.cjs.js |
2 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . |
module |
dist/string-extract-class-names.esm.js |
2 KB |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in |
browser |
dist/string-extract-class-names.umd.js |
1 KB |
Table of Contents
Purpose
This library extracts the class and id names from the string and returns them all put into an array.
I use string-extract-class-names
to identify all the CSS class names from the parsed HTML/CSS in the library email-comb which detects and deletes the unused CSS styles.
Since deleting of people's code is a risky task, a huge responsibility falls onto parts which identify what should be deleted, and more importantly, parts which identify class names and id's. That's why I extracted the string-extract-class-names
from the email-remove-unused-css
and set up a proper test suite.
Currently there 196 checks in test.js
running on AVA. I'm checking all the possible (and impossible) strings in and around the class and id names to be 100% sure only correct class and id names are put into the results array and nothing else.
API
stringExtractClassNames(inputString, [returnRangesInstead]);
API - Input
Input argument | Type | Obligatory? | Description |
---|---|---|---|
inputString | String | yes | String to process |
returnRangesInstead | Boolean | no | Default - false - return arrays of strings - selectors; optionally - true - return array of ranges |
By ranges we mean string slice ranges, arrays of two elements where both arguments match the String.slice
first two arguments, beginIndex
and endIndex
.
For example,
const extract = require("string-extract-class-names");
const str = "div.first-class.second-class";
// default settings: each selector as string will be put in an array and returned:
const res1 = extract(str);
console.log("res1 = " + res1);
// => res1 = [".first-class", ".second-class"]
// optionally, you can request ranges:
const res2 = extract(str, true);
console.log("res2 = " + res2);
// => res2 = [[3, 15], [15, 28]]
More examples
const extract = require("string-extract-class-names");
// two classes and one id extracted:
const res3 = extract("div.first.second#third a[target=_blank]");
console.log("res3 = " + res3);
// => res3 = ['.first', '.second', '#third']
// six id's extracted (works even despite the nonsensical question mark characters):
const res4 = extract("?#id1#id2? #id3#id4> p > #id5#id6");
console.log("res4 = " + res4);
// => res4 = ['#id1', '#id2', '#id3', '#id4', '#id5', '#id6']
Contributing
- If you see an error, raise an issue.
- If you want a new feature but can't code it up yourself, also raise an issue. Let's discuss it.
- If you tried to use this package, but something didn't work out, also raise an issue. We'll try to help.
- If you want to contribute some code, fork the monorepo via BitBucket, then write code, then file a pull request via BitBucket. We'll merge it in and release.
In monorepo, npm libraries are located in packages/
folder. Inside, the source code is located either in src/
folder (normal npm library) or in the root, cli.js
(if it's a command line application).
The npm script "dev
", the "dev": "rollup -c --dev --silent"
builds the development version retaining all console.log
s with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all console.log
s.
Licence
MIT License
Copyright (c) 2015-2019 Roy Revelt and other contributors