Skip to content

Commit 404df95

Browse files
committed
Verify options in one place
1 parent e8d74d7 commit 404df95

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

index.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ var postcss = require('postcss');
22
var path = require('path');
33
var fs = require('fs');
44

5-
function getSortOrderFromOptions(options) {
6-
// If no options use default config
7-
if (options === null || typeof options !== 'object' || !options['sort-order']) {
8-
options = { 'sort-order': 'default' };
5+
function verifyOptions(options) {
6+
if (options === null || typeof options !== 'object') {
7+
options = {};
98
}
109

10+
options['sort-order'] = options['sort-order'] || 'default';
11+
options['empty-lines-between-children-rules'] = options['empty-lines-between-children-rules'] || 0;
12+
13+
return options;
14+
}
15+
16+
function getSortOrderFromOptions(options) {
1117
var sortOrder;
1218

1319
if (Array.isArray(options['sort-order'])) {
@@ -42,14 +48,9 @@ function getSortOrderFromOptions(options) {
4248
return order;
4349
}
4450

45-
function getLinesBetweenChildrenFromOptions(opts) {
46-
var options = opts || {};
51+
function getLinesBetweenChildrenFromOptions(options) {
4752
var lines = options['empty-lines-between-children-rules'];
4853

49-
if (lines === undefined || lines === null) {
50-
return 0;
51-
}
52-
5354
if (typeof lines !== 'number' || isNaN(lines) || !isFinite(lines) || lines < 0 || Math.floor(lines) !== lines) {
5455
throw new Error('Type of "empty-lines-between-children-rules" option must be integer with positive value.');
5556
}
@@ -158,12 +159,13 @@ function fetchAllCommentsAfterNode(comments, nextNode, node) {
158159
return fetchAllCommentsAfterNode(comments.concat(nextNode), nextNode.next(), node);
159160
}
160161

161-
162162
module.exports = postcss.plugin('postcss-sorting', function (opts) {
163-
var linesBetweenChildrenRules = getLinesBetweenChildrenFromOptions(opts);
163+
// Verify options and use defaults if not specified
164+
opts = verifyOptions(opts);
164165

165166
return function (css) {
166167
var order = getSortOrderFromOptions(opts);
168+
var linesBetweenChildrenRules = getLinesBetweenChildrenFromOptions(opts);
167169

168170
// Index to place the nodes that shouldn't be sorted
169171
var lastGroupIndex = order['...'] ? order['...'].group : Infinity;

0 commit comments

Comments
 (0)