JSPM

  • Created
  • Published
  • Downloads 22867
  • Score
    100M100P100Q147835F
  • License MIT

Extract class (or id) name from a string

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

Standard JavaScript

Extract classes (or id's) from a string into an array

Build Status bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies Downloads/Month

Purpose

This library extracts the classes and id's from the strings that HTML parsers return. Basically, it will chop off any tags in front (actually anything until it encounters first class or id). Then, it will put any chained classes or id's into an array and will stop if it encounters a space or any character that's not allowed to be part of class or id name.

I use this library to identify and delete unused CSS selectors in the library email-remove-unused-css.

Since deleting of people's code is a risky task, a huge responsibility falls onto all auxiliary libraries used, especially the ones that identify what should be deleted. That's why I extracted the string-extract-class-names from the email-remove-unused-css and set up proper test suite.

Currently there 190 deep comparisons 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.

Examples

// chop off tag, then the rest after the space character:
extract('div.first.second#third a[target=_blank]')
// => ['.first', '.second', '#third']

extract('div#id.class a[target=_blank]')
// => ['#id', '.class']

Install

$ npm install --save string-extract-class-names

Use

var extract = require('string-extract-class-names')

API

extract(
  string               // String. Input.
)
// => Extracted classes/id's in an array

Contributing & testing

All contributions welcome. This library uses Standard JavaScript notation. See test.js. It's very minimalistic testing setup using AVA.

npm test

If you see anything incorrect whatsoever, raise an issue. PR's are welcome — fork, hack and PR.

Licence

MIT © Roy Reveltas