filter-css
Advanced tools
Comparing version 0.1.2 to 1.0.0
113
cli.js
@@ -0,83 +1,88 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
var meow = require('meow'); | ||
var _ = require('lodash'); | ||
var stdin = require('get-stdin'); | ||
var updateNotifier = require('update-notifier'); | ||
var filterCss = require('./'); | ||
var pkg = require('./package.json'); | ||
var ok; | ||
var help = [ | ||
'Usage: filtercss <input> [<option>]', | ||
'', | ||
'Options:', | ||
' -i, --ignore RegExp, selector @type to ignore', | ||
' -S, --skipSelectors Don\'t match selectors', | ||
' -T, --skipTypes Don\'t match types', | ||
' -P, --skipDeclarationProperties Don\'t match declaration properties', | ||
' -V, --skiphDeclarationValues Don\'t match declaration vakues', | ||
' -M, --skipMedia Don\'t match media' | ||
].join('\n'); | ||
const meow = require('meow'); | ||
const isString = require('lodash.isstring'); | ||
const isRegExp = require('lodash.isregexp'); | ||
const stdin = require('get-stdin'); | ||
const filterCss = require('.'); | ||
const help = ` | ||
Usage: filtercss <input> [<option>] | ||
Options: | ||
-i, --ignore RegExp, selector @type to ignore | ||
-S, --skipSelectors Don't match selectors | ||
-T, --skipTypes Don't match types | ||
-P, --skipDeclarationProperties Don't match declaration properties | ||
-V, --skipDeclarationValues Don't match declaration values | ||
-M, --skipMedia Don't match media | ||
`; | ||
var cli = meow({help: help}, {alias: { | ||
i: 'ignore', | ||
S: 'skipSelectors', | ||
T: 'skipTypes', | ||
P: 'skipDeclarationProperties', | ||
V: 'skiphDeclarationValues', | ||
M: 'skipMedia' | ||
}}); | ||
const cli = meow( | ||
help, { | ||
flags: { | ||
ignore: { | ||
alias: 'i' | ||
}, | ||
skipSelectors: { | ||
type: 'boolean', | ||
alias: 'S' | ||
}, | ||
skipTypes: { | ||
type: 'boolean', | ||
alias: 'T' | ||
}, | ||
skipDeclarationProperties: { | ||
type: 'boolean', | ||
alias: 'P' | ||
}, | ||
skipDeclarationValues: { | ||
type: 'boolean', | ||
alias: 'V' | ||
}, | ||
skipMedia: { | ||
type: 'boolean', | ||
alias: 'M' | ||
} | ||
} | ||
} | ||
); | ||
if (cli.flags['update-notifier'] !== false) { | ||
updateNotifier({pkg: pkg}).notify(); | ||
} | ||
function go(data) { | ||
if (!data) { | ||
cli.showHelp(); | ||
} | ||
function go(data) { | ||
ok = true; | ||
if (_.isString(cli.flags.ignore) || _.isRegExp(cli.flags.ignore)) { | ||
if (isString(cli.flags.ignore) || isRegExp(cli.flags.ignore)) { | ||
cli.flags.ignore = [cli.flags.ignore]; | ||
} | ||
var ignores = _.map(cli.flags.ignore || [], function(ignore) { | ||
const ignores = (cli.flags.ignore || []).map(ignore => { | ||
// check regex | ||
var match = ignore.match(/^\/(.*)\/([igmy]+)?$/); | ||
const match = ignore.match(/^\/(.*)\/([igmy]+)?$/); | ||
if (match) { | ||
return new RegExp(match[1],match[2]); | ||
return new RegExp(match[1], match[2]); | ||
} | ||
return ignore; | ||
}); | ||
if (!data) { | ||
cli.showHelp(); | ||
return; | ||
} | ||
var diff = filterCss(data,ignores, { | ||
const diff = filterCss(data, ignores, { | ||
matchSelectors: !cli.flags.skipSelectors, | ||
matchTypes: !cli.flags.skipTypes, | ||
matchDeclarationProperties: !cli.flags.skipDeclarationProperties, | ||
matchDeclarationValues: !cli.flags.skiphDeclarationValues, | ||
matchDeclarationValues: !cli.flags.skipDeclarationValues, | ||
matchMedia: !cli.flags.skipMedia | ||
}); | ||
console.log(diff); | ||
process.exit(); | ||
} | ||
function die() { | ||
if (ok) { | ||
return; | ||
} | ||
cli.showHelp(); | ||
} | ||
if (cli.input[0]) { | ||
go(cli.input[0]); | ||
} else { | ||
// get stdin | ||
stdin().then(go); | ||
setTimeout(die, 200); | ||
} |
73
index.js
'use strict'; | ||
var _ = require('lodash'); | ||
var fs = require('fs'); | ||
var css = require('css'); | ||
var _default = { | ||
const fs = require('fs'); | ||
const css = require('css'); | ||
const defaults = require('lodash.defaults'); | ||
const isFunction = require('lodash.isfunction'); | ||
const isRegExp = require('lodash.isregexp'); | ||
const reject = require('lodash.reject'); | ||
const result = require('lodash.result'); | ||
const _default = { | ||
matchSelectors: true, | ||
@@ -18,3 +23,10 @@ matchTypes: true, | ||
function getValue(element, pluck) { | ||
if (pluck) { | ||
return result(element, pluck); | ||
} | ||
return element; | ||
} | ||
/** | ||
@@ -29,12 +41,5 @@ * Identify ignored selectors | ||
function _matcher(ignores, identifier, node, pluck) { | ||
function getValue(element) { | ||
if (pluck) { | ||
return _.result(element, pluck); | ||
} | ||
return element; | ||
} | ||
return function (element) { | ||
for (var i = 0; i < ignores.length; ++i) { | ||
if (_.isFunction(ignores[i]) && ignores[i](identifier, getValue(element), node || element)) { | ||
return element => { | ||
for (let i = 0; i < ignores.length; ++i) { | ||
if (isFunction(ignores[i]) && ignores[i](identifier, getValue(element, pluck), node || element)) { | ||
return true; | ||
@@ -44,9 +49,11 @@ } | ||
/* If ignore is RegExp and matches selector ... */ | ||
if (_.isRegExp(ignores[i]) && ignores[i].test(getValue(element))) { | ||
if (isRegExp(ignores[i]) && ignores[i].test(getValue(element, pluck))) { | ||
return true; | ||
} | ||
if (ignores[i] === getValue(element)) { | ||
if (ignores[i] === getValue(element, pluck)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
@@ -56,3 +63,2 @@ }; | ||
/** | ||
@@ -65,9 +71,7 @@ * | ||
function reduceRules(ignore, opts) { | ||
const matcher = (...args) => _matcher(ignore, ...args); | ||
var matcher = _.partial(_matcher, ignore); | ||
return function reducer(rules, rule) { | ||
// check if whole type is ignored | ||
if (opts.matchTypes && matcher('type', rule)('@' + rule.type)) { | ||
if (opts.matchTypes && matcher('type', rule)(`@${rule.type}`)) { | ||
return rules; | ||
@@ -82,6 +86,5 @@ } | ||
rule.rules = (rule.rules || []).reduce(reducer, []); | ||
rule.rules = _.reduce(rule.rules || [], reducer, []); | ||
if (_.size(rule.rules)) { | ||
if (rule.rules.length > 0) { | ||
rules.push(rule); | ||
@@ -92,9 +95,9 @@ } | ||
if (opts.matchSelectors) { | ||
rule.selectors = _.reject(rule.selectors || [], matcher('selector', rule)); | ||
rule.selectors = reject(rule.selectors || [], matcher('selector', rule)); | ||
} | ||
if (_.size(rule.selectors)) { | ||
if (rule.selectors.length > 0) { | ||
// check declaration property | ||
if (opts.matchDeclarationProperties) { | ||
rule.declarations = _.reject(rule.declarations || [], matcher('declarationProperty', undefined, 'property')); | ||
rule.declarations = reject(rule.declarations || [], matcher('declarationProperty', undefined, 'property')); | ||
} | ||
@@ -104,7 +107,7 @@ | ||
if (opts.matchDeclarationValues) { | ||
rule.declarations = _.reject(rule.declarations || [], matcher('declarationValue', undefined, 'value')); | ||
rule.declarations = reject(rule.declarations || [], matcher('declarationValue', undefined, 'value')); | ||
} | ||
// add rule if something's left | ||
if (_.size(rule.declarations)) { | ||
if (rule.declarations.length > 0) { | ||
rules.push(rule); | ||
@@ -121,19 +124,17 @@ } | ||
function api(stylesheet, ignore, opts) { | ||
opts = defaults(opts || {}, _default); | ||
opts = _.defaults(opts || {}, _default); | ||
if (!_.isArray(ignore)) { | ||
if (!Array.isArray(ignore)) { | ||
ignore = [ignore]; | ||
} | ||
var sheet; | ||
let sheet; | ||
try { | ||
sheet = css.parse(read(stylesheet)); | ||
} catch (err) { | ||
} catch (error) { | ||
sheet = css.parse(stylesheet); | ||
} | ||
sheet.stylesheet.rules = _.reduce(sheet.stylesheet.rules, reduceRules(ignore, opts), []); | ||
sheet.stylesheet.rules = sheet.stylesheet.rules.reduce(reduceRules(ignore, opts), []); | ||
return css.stringify(sheet); | ||
@@ -140,0 +141,0 @@ } |
{ | ||
"name": "filter-css", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "Filter CSS rules", | ||
@@ -13,6 +13,9 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "jshint . && mocha test/*.js" | ||
"lint": "npm run xo", | ||
"xo": "xo", | ||
"mocha": "mocha", | ||
"test": "npm run xo && npm run mocha" | ||
}, | ||
@@ -32,13 +35,22 @@ "files": [ | ||
"dependencies": { | ||
"css": "^2.2.0", | ||
"get-stdin": "^5.0.1", | ||
"lodash": "^4.13.1", | ||
"meow": "^3.1.0", | ||
"update-notifier": "^0.7.0" | ||
"css": "^2.2.4", | ||
"get-stdin": "^6.0.0", | ||
"lodash.defaults": "^4.2.0", | ||
"lodash.isfunction": "^3.0.9", | ||
"lodash.isregexp": "^4.0.1", | ||
"lodash.isstring": "^4.0.1", | ||
"lodash.reject": "^4.6.0", | ||
"lodash.result": "^4.5.2", | ||
"meow": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"jshint": "^2.7.0", | ||
"mocha": "^2.2.4" | ||
"chai": "^4.2.0", | ||
"mocha": "^6.2.2", | ||
"xo": "^0.24.0" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"capitalized-comments": "off" | ||
} | ||
} | ||
} |
114
readme.md
@@ -1,46 +0,43 @@ | ||
# filter-css [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Build status][appveyor-image]][appveyor-url] [![Dependency Status][depstat-image]][depstat-url] | ||
# filter-css [![npm version][npm-image]][npm-url] [![Build Status][ci-image]][ci-url] [![dependencies Status][depstat-image]][depstat-url] [![devDependencies Status Status][devdepstat-image]][devdepstat-url] | ||
Filter CSS rules | ||
## Install | ||
```shell | ||
npm install --save filter-css | ||
``` | ||
$ npm install --save filter-css | ||
``` | ||
## Usage | ||
```js | ||
var filterCss = require('filter-css'); | ||
var filtered = filterCss(<input>, <pattern>, <options>); | ||
const filterCss = require('filter-css'); | ||
const filtered = filterCss(<input>, <pattern>, <options>); | ||
``` | ||
#### Input | ||
### Input | ||
*Required* | ||
Type: `String` | ||
* *Required* | ||
* Type: `String` | ||
Can be a path to the CSS file or a raw CSS string | ||
Can be a path to the CSS file or a raw CSS string. | ||
#### Pattern | ||
### Pattern | ||
*Required* | ||
Type `String`,`RegExp`, `Function` or an `Array` containing it. | ||
Patterns used to discard specific parts of the CSS. | ||
The function is invoked with three arguments (context, value, node). | ||
* `context` Current matching context. Could be one of `['type','media','selector','declarationProperty','declarationValue']`. | ||
* `value` Current value. | ||
* `node` The currently processed AST node generated by [`css`](https://github.com/reworkcss/css). | ||
* *Required* | ||
* Type `String`,`RegExp`, `Function` or an `Array` containing it. | ||
Return true if the element should be discarded. | ||
Patterns used to discard specific parts of the CSS. | ||
The function is invoked with three arguments (`context`, `value`, `node`). | ||
#### Options | ||
* `context`: Current matching context. Could be one of `['type', 'media', 'selector', 'declarationProperty', 'declarationValue']`. | ||
* `value`: Current value. | ||
* `node`: The currently processed AST node generated by [`css`](https://github.com/reworkcss/css). | ||
Per default `filter-css` will be applied to all parts of the CSS. This behavior can be customized by disabling specific matchers | ||
Return true if the element should be discarded. | ||
### Options | ||
Per default `filter-css` will be applied to all parts of the CSS. This behavior can be customized by disabling specific matchers. | ||
| Name | Type | Description | | ||
@@ -54,3 +51,2 @@ | -------------------------- | --------- |-------------- | | ||
## Examples | ||
@@ -60,9 +56,9 @@ | ||
.bigBackground { | ||
width: 100%; | ||
height: 100%; | ||
background-image: url('some/big/image.png'); | ||
width: 100%; | ||
height: 100%; | ||
background-image: url('some/big/image.png'); | ||
} | ||
@font-face { | ||
font-family: 'My awesome font'; | ||
font-family: 'My awesome font'; | ||
} | ||
@@ -76,5 +72,5 @@ | ||
```js | ||
var filterCss = require('filter-css'); | ||
const filterCss = require('filter-css'); | ||
filterCss('test/fixtures/test.css',[/url\(/,'@font-face',/print/]); | ||
filterCss('test/fixtures/test.css', [/url\(/,'@font-face',/print/]); | ||
``` | ||
@@ -84,29 +80,28 @@ | ||
.bigBackground { | ||
width: 100%; | ||
height: 100%; | ||
width: 100%; | ||
height: 100%; | ||
} | ||
``` | ||
#### Remove all media queries | ||
### Remove all media queries | ||
```js | ||
var filterCss = require('filter-css'); | ||
const filterCss = require('filter-css'); | ||
filterCss('test/fixtures/test.css',/.*/,{ | ||
matchSelectors: false, | ||
matchTypes: false, | ||
matchDeclarationProperties: false, | ||
matchDeclarationValues: false, | ||
matchMedia: true | ||
filterCss('test/fixtures/test.css', /.*/, { | ||
matchSelectors: false, | ||
matchTypes: false, | ||
matchDeclarationProperties: false, | ||
matchDeclarationValues: false, | ||
matchMedia: true | ||
}); | ||
``` | ||
### Using a function matcher | ||
#### Using a function matcher | ||
```js | ||
var filterCss = require('filter-css'); | ||
const filterCss = require('filter-css'); | ||
filterCss('test/fixtures/test.css',function(context, value, node) { | ||
return context === 'declarationValue' && value === "url('some/big/image.png')" | ||
filterCss('test/fixtures/test.css', (context, value, node) => { | ||
return context === 'declarationValue' && value === "url('some/big/image.png')" | ||
}); | ||
@@ -116,3 +111,4 @@ | ||
#### Complete Example | ||
### Complete Example | ||
```js | ||
@@ -131,10 +127,12 @@ filterCss('test/fixtures/test.css', { | ||
```shell | ||
$ cat test/fixture/test.css | filtercss --ignore @font-face | ||
cat test/fixture/test.css | filtercss --ignore @font-face | ||
``` | ||
You can also pass in the file as an option. | ||
```shell | ||
$ filtercss test/fixture/test.css --ignore @font-face | ||
filtercss test/fixture/test.css --ignore @font-face | ||
``` | ||
#### CLI options | ||
### CLI options | ||
@@ -148,11 +146,11 @@ See `filtercss --help` for a full list of options. | ||
[npm-url]: https://npmjs.org/package/filter-css | ||
[npm-image]: https://badge.fury.io/js/filter-css.svg | ||
[npm-image]: https://img.shields.io/npm/v/filter-css.svg | ||
[travis-url]: https://travis-ci.org/bezoerb/filter-css | ||
[travis-image]: https://travis-ci.org/bezoerb/filter-css.svg?branch=master | ||
[ci-url]: https://github.com/bezoerb/filter-css/actions?workflow=Tests | ||
[ci-image]: https://github.com/bezoerb/filter-css/workflows/Tests/badge.svg | ||
[appveyor-url]: https://ci.appveyor.com/project/bezoerb/filter-css/branch/master | ||
[appveyor-image]: https://ci.appveyor.com/api/projects/status/xypps7lx2usx3akp/branch/master?svg=true | ||
[depstat-url]: https://david-dm.org/bezoerb/filter-css | ||
[depstat-image]: https://img.shields.io/david/bezoerb/filter-css.svg | ||
[depstat-url]: https://david-dm.org/bezoerb/filter-css | ||
[depstat-image]: https://david-dm.org/bezoerb/filter-css.svg | ||
[devdepstat-url]: https://david-dm.org/bezoerb/filter-css?type=dev | ||
[devdepstat-image]: https://img.shields.io/david/dev/bezoerb/filter-css.svg |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10892
186
1
9
150
+ Addedlodash.defaults@^4.2.0
+ Addedlodash.isfunction@^3.0.9
+ Addedlodash.isregexp@^4.0.1
+ Addedlodash.isstring@^4.0.1
+ Addedlodash.reject@^4.6.0
+ Addedlodash.result@^4.5.2
+ Addedarrify@1.0.1(transitive)
+ Addedcamelcase@4.1.0(transitive)
+ Addedcamelcase-keys@4.2.0(transitive)
+ Addeddecamelize-keys@1.1.1(transitive)
+ Addedfind-up@2.1.0(transitive)
+ Addedget-stdin@6.0.0(transitive)
+ Addedindent-string@3.2.0(transitive)
+ Addedis-plain-obj@1.1.0(transitive)
+ Addedjson-parse-better-errors@1.0.2(transitive)
+ Addedload-json-file@4.0.0(transitive)
+ Addedlocate-path@2.0.0(transitive)
+ Addedlodash.defaults@4.2.0(transitive)
+ Addedlodash.isfunction@3.0.9(transitive)
+ Addedlodash.isregexp@4.0.1(transitive)
+ Addedlodash.isstring@4.0.1(transitive)
+ Addedlodash.reject@4.6.0(transitive)
+ Addedlodash.result@4.5.2(transitive)
+ Addedmap-obj@2.0.0(transitive)
+ Addedmeow@5.0.0(transitive)
+ Addedminimist-options@3.0.2(transitive)
+ Addedp-limit@1.3.0(transitive)
+ Addedp-locate@2.0.0(transitive)
+ Addedp-try@1.0.0(transitive)
+ Addedparse-json@4.0.0(transitive)
+ Addedpath-exists@3.0.0(transitive)
+ Addedpath-type@3.0.0(transitive)
+ Addedpify@3.0.0(transitive)
+ Addedquick-lru@1.1.0(transitive)
+ Addedread-pkg@3.0.0(transitive)
+ Addedread-pkg-up@3.0.0(transitive)
+ Addedredent@2.0.0(transitive)
+ Addedstrip-bom@3.0.0(transitive)
+ Addedstrip-indent@2.0.0(transitive)
+ Addedtrim-newlines@2.0.0(transitive)
+ Addedyargs-parser@10.1.0(transitive)
- Removedlodash@^4.13.1
- Removedupdate-notifier@^0.7.0
- Removedansi-align@1.1.0(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedboxen@0.5.1(transitive)
- Removedcamelcase@2.1.1(transitive)
- Removedcamelcase-keys@2.1.0(transitive)
- Removedcapture-stack-trace@1.0.2(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcli-boxes@1.0.0(transitive)
- Removedcode-point-at@1.1.0(transitive)
- Removedconfigstore@2.1.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedcreate-error-class@3.0.2(transitive)
- Removeddeep-extend@0.6.0(transitive)
- Removeddot-prop@3.0.0(transitive)
- Removedduplexer2@0.1.4(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedfilled-array@1.1.0(transitive)
- Removedfind-up@1.1.2(transitive)
- Removedget-stdin@4.0.15.0.1(transitive)
- Removedgot@5.7.1(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedimurmurhash@0.1.4(transitive)
- Removedindent-string@2.1.0(transitive)
- Removedini@1.3.8(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedis-fullwidth-code-point@1.0.0(transitive)
- Removedis-npm@1.0.0(transitive)
- Removedis-obj@1.0.1(transitive)
- Removedis-redirect@1.0.0(transitive)
- Removedis-retry-allowed@1.2.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedis-utf8@0.2.1(transitive)
- Removedisarray@1.0.0(transitive)
- Removedlatest-version@2.0.0(transitive)
- Removedload-json-file@1.1.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedlowercase-keys@1.0.1(transitive)
- Removedmeow@3.7.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removednode-status-codes@1.0.0(transitive)
- Removednumber-is-nan@1.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedos-homedir@1.0.2(transitive)
- Removedos-tmpdir@1.0.2(transitive)
- Removedosenv@0.1.5(transitive)
- Removedpackage-json@2.4.0(transitive)
- Removedparse-json@2.2.0(transitive)
- Removedpath-exists@2.1.0(transitive)
- Removedpath-type@1.1.0(transitive)
- Removedpify@2.3.0(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedprepend-http@1.0.4(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedrc@1.2.8(transitive)
- Removedread-all-stream@3.1.0(transitive)
- Removedread-pkg@1.1.0(transitive)
- Removedread-pkg-up@1.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedredent@1.0.0(transitive)
- Removedregistry-auth-token@3.4.0(transitive)
- Removedregistry-url@3.1.0(transitive)
- Removedrepeating@2.0.1(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsemver-diff@2.1.0(transitive)
- Removedslide@1.1.6(transitive)
- Removedstring-width@1.0.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedstrip-bom@2.0.0(transitive)
- Removedstrip-indent@1.0.1(transitive)
- Removedstrip-json-comments@2.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedtimed-out@3.1.3(transitive)
- Removedtrim-newlines@1.0.0(transitive)
- Removedunzip-response@1.0.2(transitive)
- Removedupdate-notifier@0.7.0(transitive)
- Removedurl-parse-lax@1.0.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removeduuid@2.0.3(transitive)
- Removedwidest-line@1.0.0(transitive)
- Removedwrite-file-atomic@1.3.4(transitive)
- Removedxdg-basedir@2.0.0(transitive)
Updatedcss@^2.2.4
Updatedget-stdin@^6.0.0
Updatedmeow@^5.0.0