JSPM

  • Created
  • Published
  • Downloads 22867
  • Score
    100M100P100Q147762F
  • 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 all class and id names from a string and return them in an array

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

Install

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

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-remove-unused-css 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.

Examples

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

// two classes and one id extracted:
extract('div.first.second#third a[target=_blank]')
// => ['.first', '.second', '#third']

// six id's extracted (works even despite the nonsensical question mark characters):
extract('?#id1#id2? #id3#id4> p > #id5#id6')
// => ['#id1', '#id2', '#id3', '#id4', '#id5', '#id6']

API

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

Testing

$ npm test

Unit tests use AVA and JS Standard notation.

Contributing

All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js with new unit tests covering your feature(s).

If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to review it promptly. If you have any comments on the code, including ideas how to improve things, don't hesitate to contact me by email.

Licence

MIT License (MIT)

Copyright (c) 2017 Codsen Ltd, Roy Revelt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.