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
Purpose
I was working on another project where I parsed some HTML and CSS and wanted to extract all the classes and id's. However, most of the time, the classes were surrounded with other selectors and tags. I wanted to nail this library 100% — not just download some regex'es from StackOverflow and hope library won't have any classes "contaminated" with surrounding tags. That's why I thoroughly researched the CSS specs and not only prepared for all the possible characters that terminate class/id names, but coded AVA tests for each case.
So, I'm taking this seriously, what first means serious tests and serious documentation.
TLDR
Basically.
Input this:
sometagname a.class-name:hoverOutput:
[.class-name]OR, input this:
a.class-name[target=_blank]output this:
[.class-name]String input, string output. See API below.
Install
$ npm install --save string-extract-class-namesUse
var extract = require('string-extract-class-names')Test
$ npm testUses AVA for unit tests.
API
extract(
string, // String. Input.
chopUpToNotIncluding // String. `.` for classes or `#` for id's. Default is `.`.
)
// => Extracted string(s) in an arrayExamples
// second param is omitted, falls back to default "."
extract('div.class-name a[target=_blank]')
// => '.class-name'
// or with second parameter
extract('div.class-name a[target=_blank]', '.')
// => '.class-name'
// if there are multiple classes, you'll get an array:
extract('div.class-name-one.class-name-two a[target=_blank]')
// => ['.class-name-one', '.class-name-two']
// you can filter id and class combos:
extract('div#id.class a[target=_blank]', '#')
// => ['#id', '.class']We can extract id's by providing id selector (#) as a second input parameter:
extract('a#id-name[href^="https"]', '#')
// => #id-nameContributing & testing
All contributions welcome. This library uses Standard JavaScript notation. See test.js. It's very minimalistic testing setup using AVA.
npm testIf you see anything incorrect whatsoever, raise an issue. PR's are welcome — fork, hack and PR.
Licence
MIT © Roy Reveltas