Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Feb 21, 2017
0 parents commit c8dcafc
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
coverage
node_modules
9 changes: 9 additions & 0 deletions .travis.yml
@@ -0,0 +1,9 @@
dist: trusty
branches:
except: /^v\d/
language: node_js
node_js: node
script: npm run-script pretest && npm run-script coverage
after_script:
- npm install istanbul-coveralls
- node node_modules/.bin/istanbul-coveralls
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 Shinnosuke Watanabe

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.
74 changes: 74 additions & 0 deletions README.md
@@ -0,0 +1,74 @@
# get-processor-ids-from-stylelint-options

[![NPM version](https://img.shields.io/npm/v/get-processor-ids-from-stylelint-options.svg)](https://www.npmjs.com/package/get-processor-ids-from-stylelint-options)
[![Build Status](https://travis-ci.org/shinnn/get-processor-ids-from-stylelint-options.svg?branch=master)](https://travis-ci.org/shinnn/get-processor-ids-from-stylelint-options)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/get-processor-ids-from-stylelint-options.svg)](https://coveralls.io/github/shinnn/get-processor-ids-from-stylelint-options?branch=master)

Get [stylelint](https://stylelint.io/) [processor](https://stylelint.io/user-guide/processors/) identifiers from a [stylelint option object](https://stylelint.io/user-guide/node-api/#options)

```javascript
const getProcessorIdsFromStylelintOptions = require('get-processor-ids-from-stylelint-options');

getProcessorIdsFromStylelintOptions({
config: {
processors: [
'@mapbox/stylelint-processor-markdown',
['./custom-proessor.js', {
optionOne: true,
optionTwo: false
}]
]
}
}); //=> Set {'@mapbox/stylelint-processor-markdown', './custom-processor.js'}
```

## Installation

[Use npm.](https://docs.npmjs.com/cli/install)

```
npm install get-processor-ids-from-stylelint-options
```

## API

```javascript
const getProcessorIdsFromStylelintOptions = require('get-processor-ids-from-stylelint-options');
```

### getProcessorIdsFromStylelintOptions([*options*])

*options*: `Object` ([stylelint API options](https://github.com/stylelint/stylelint/blob/master/docs/user-guide/node-api.md#options))
Return: `Set<string>`

```javascript
getProcessorIdsFromStylelintOptions({
config: {
processors: [
'./processor0.js',
'./processor1.js'
]
}
}); //=> Set {'./processor0.js', ./processor1.js'}

getProcessorIdsFromStylelintOptions({
configOverrides: {
processors: '/processor/can/be/a/string/instead/of/an/array.js'
}
}); //=> Set {'/processor/can/be/a/string/instead/of/an/array.js'}

getProcessorIdsFromStylelintOptions({
config: {},
configOverrides: {
processors: 'configOverrides/will/be/ignored/when/both/config/and/configOverrides/are/provided'
}
}); //=> Set {}

getProcessorIdsFromStylelintOptions(); //=> Set {}
```

## License

Copyright (c) 2017 [Shinnosuke Watanabe](https://github.com/shinnn)

Licensed under [the MIT License](./LICENSE).
67 changes: 67 additions & 0 deletions index.js
@@ -0,0 +1,67 @@
/*!
* get-processor-ids-from-stylelint-options | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/get-processor-ids-from-stylelint-options
*/
'use strict';

const {inspect} = require('util');

const isPlainObject = require('lodash/isPlainObject');

const props = new Set(['config', 'configOverrides']);

module.exports = function getProcessorIdsFromStylelintOptions(...args) {
const argLen = args.length;

if (argLen === 0) {
return new Set();
}

if (argLen !== 1) {
throw new TypeError(`Expected 0 or 1 argument ([object]), but got ${argLen} arguments instead.`);
}

const options = args[0];

if (!isPlainObject(options)) {
throw new TypeError(`Expected the argument to be an object of stylelint API options, but got ${
inspect(options)
}.`);
}

for (const prop of props) {
const val = options[prop];

if (val) {
if (!isPlainObject(val)) {
throw new TypeError(`Expected \`${prop}\` option in stylelint API to be an object, but it was ${
inspect(val)
}.`);
}

if (val.processors) {
if (typeof val.processors === 'string') {
return new Set([val.processors]);
}

if (Array.isArray(val.processors)) {
const ids = new Set();

for (const processor of val.processors) {
ids.add(Array.isArray(processor) ? processor[0] : processor);
}

return ids;
}

throw new TypeError(
`\`processors\` property in the sytylelint config must be an array or a string, but it was ${
inspect(val.processors)
}.`
);
}
}
}

return new Set();
};
40 changes: 40 additions & 0 deletions package.json
@@ -0,0 +1,40 @@
{
"name": "get-processor-ids-from-stylelint-options",
"version": "1.0.0",
"description": "Get stylelint processor identifiers from given stylelint API options",
"repository": "shinnn/get-processor-ids-from-stylelint-options",
"author": "Shinnosuke Watanabe (https://github.com/shinnn)",
"scripts": {
"pretest": "eslint --fix --format=codeframe index.js test.js",
"test": "node --throw-deprecation test.js",
"coverage": "istanbul cover --print=both test.js"
},
"license": "MIT",
"files": [
"index.js"
],
"keywords": [
"mode",
"file",
"stat",
"lstat",
"int",
"integer",
"number",
"promise",
"async",
"asynchronous"
],
"dependencies": {
"lodash": "^4.17.4"
},
"devDependencies": {
"@shinnn/eslint-config-node": "^3.0.0",
"eslint": "^3.16.0",
"istanbul": "^0.4.5",
"tape": "^4.6.3"
},
"eslintConfig": {
"extends": "@shinnn/node"
}
}
106 changes: 106 additions & 0 deletions test.js
@@ -0,0 +1,106 @@
'use strict';

const {inspect} = require('util');

const main = require('.');
const test = require('tape');
const cloneDeep = require('lodash/cloneDeep');

test('getProcessorIdsFromStylelintOptions()', t => {
const option = {
configOverrides: {
processors: '@mapbox/stylelint-processor-markdown'
}
};
const clonedOption = cloneDeep(option);

const result = main(option);

t.ok(result instanceof Set, 'should return a Set instance.');
t.deepEqual(
[...result],
['@mapbox/stylelint-processor-markdown'],
'should get stylelint processor identifiers.'
);

t.deepEqual(option, clonedOption, 'should not mutate arguments.');

t.strictEqual(
inspect(main({
config: {
processors: [
['stylelint-processor-arbitrary-tags', {startTag: '***'}],
'./custom-processor.js'
]
},
configOverrides: {
processors: 'stylelint-processor-html'
}
}), {breakLength: Infinity}),
'Set { \'stylelint-processor-arbitrary-tags\', \'./custom-processor.js\' }',
'should ignore `configOverrides` if `config` is provided.'
);

t.strictEqual(
main({
config: {
processors: []
}
}).size,
0,
'should return an empty set if the config has an empty `processor` option.'
);

t.strictEqual(
main({
config: {}
}).size,
0,
'should return an empty set if the config doesn\'t have `processor` option.'
);

t.strictEqual(
main({}).size,
0,
'should return an empty set if the object has neither `config` nor `configOverrides`.'
);

t.strictEqual(
main().size,
0,
'should return an empty set if it takes no arguments.'
);

t.throws(
() => main({}, {}),
/^TypeError.*Expected 0 or 1 argument \(\[object]\), but got 2 arguments instead\./,
'should throw an error when it takes too many arguments.'
);

t.throws(
() => main(['Hi']),
/^TypeError.*Expected the argument to be an object of stylelint API options, but got \[ 'Hi' ]\./,
'should throw an error when it takes a non-plain object.'
);

t.throws(
() => main({
config: Buffer.from('?'),
configOverrides: {}
}),
/^TypeError.*Expected `config` option in stylelint API to be an object, but it was <Buffer 3f>\./,
'should throw an error when the config object is not a plain object.'
);

t.throws(
() => main({
config: {
processors: new Map()
}
}),
/^TypeError.*`processors` property in the sytylelint config must be an array or a string, but it was Map \{\}\./,
'should throw an error when `processor` option is neither an array nor a string.'
);

t.end();
});

0 comments on commit c8dcafc

Please sign in to comment.