Skip to content

Commit f192655

Browse files
committed
convenient utilities
1 parent 722a81b commit f192655

File tree

5 files changed

+121
-60
lines changed

5 files changed

+121
-60
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
"engines": {
77
"node": ">=0.12"
88
},
9+
"dependencies": {
10+
"lodash.identity": "^3.0.0",
11+
"lodash.isarray": "^3.0.4",
12+
"lodash.isfunction": "^3.0.6",
13+
"lodash.isstring": "^3.0.1"
14+
},
915
"devDependencies": {
1016
"babel": "^5.8.20",
1117
"babel-eslint": "^4.0.5",

src/fn.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/index.js

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import hook from './hook';
22
import { readFileSync } from 'fs';
33
import { dirname, sep, relative, resolve } from 'path';
4-
import { identity, removeQuotes } from './fn';
4+
import { get, removeQuotes } from './utility';
5+
import identity from 'lodash.identity';
56
import postcss from 'postcss';
67

78
import ExtractImports from 'postcss-modules-extract-imports';
@@ -32,57 +33,30 @@ export default function setup(opts = {}) {
3233
importNr = 0;
3334
tokensByFile = {};
3435

35-
if (opts.processCss && typeof opts.processCss !== 'function') {
36-
throw new Error('should specify function for processCss');
37-
}
38-
39-
postProcess = opts.processCss || null;
40-
41-
if (opts.rootDir && typeof opts.rootDir !== 'string') {
42-
throw new Error('should specify string for rootDir');
43-
}
44-
45-
rootDir = opts.rootDir || process.cwd();
46-
47-
if (opts.use) {
48-
if (!Array.isArray(opts.use)) {
49-
throw new Error('should specify array for use');
50-
}
36+
postProcess = get('processCss', null, 'function', opts) || null;
37+
rootDir = get('rootDir', ['root', 'd'], 'string', opts) || process.cwd();
5138

52-
return void (plugins = opts.use);
39+
const customPlugins = get('use', ['u'], 'array', opts);
40+
if (customPlugins) {
41+
return void (plugins = customPlugins);
5342
}
5443

5544
plugins = [];
5645

57-
if (opts.mode) {
58-
if (typeof opts.mode !== 'string') {
59-
throw new Error('should specify string for mode');
60-
}
46+
const mode = get('mode', null, 'string', opts);
47+
plugins.push(mode
48+
? new LocalByDefault({mode: opts.mode})
49+
: LocalByDefault);
6150

62-
plugins.push(new LocalByDefault({mode: opts.mode}));
63-
} else {
64-
plugins.push(LocalByDefault);
65-
}
66-
67-
if (opts.createImportedName) {
68-
if (typeof opts.createImportedName !== 'function') {
69-
throw new Error('should specify function for createImportedName');
70-
}
71-
72-
plugins.push(new ExtractImports({createImportedName: opts.createImportedName}));
73-
} else {
74-
plugins.push(ExtractImports);
75-
}
76-
77-
if (opts.generateScopedName) {
78-
if (typeof opts.generateScopedName !== 'function') {
79-
throw new Error('should specify function for generateScopedName');
80-
}
51+
const createImportedName = get('createImportedName', null, 'function', opts);
52+
plugins.push(createImportedName
53+
? new ExtractImports({createImportedName: opts.createImportedName})
54+
: ExtractImports);
8155

82-
plugins.push(new Scope({generateScopedName: opts.generateScopedName}));
83-
} else {
84-
plugins.push(Scope);
85-
}
56+
const generateScopedName = get('generateScopedName', null, 'function', opts);
57+
plugins.push(generateScopedName
58+
? new Scope({generateScopedName: opts.generateScopedName})
59+
: Scope);
8660
}
8761

8862
/**

src/utility.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import isArray from 'lodash.isarray';
2+
import isFunction from 'lodash.isfunction';
3+
import isString from 'lodash.isstring';
4+
import { format } from 'util';
5+
6+
const check = {
7+
'array': isArray,
8+
'function': isFunction,
9+
'string': isString,
10+
};
11+
12+
/**
13+
* @param {string} prop
14+
* @param {string[]} aliases
15+
* @param {string} type
16+
* @param {object} source
17+
* @return {*}
18+
*/
19+
export function get(prop, aliases, type, source) {
20+
if (source[prop]) {
21+
if (!is(type, source[prop])) {
22+
throw new Error(format('should specify %s for %s', type, prop));
23+
}
24+
25+
return source[prop];
26+
}
27+
28+
if (!isArray(aliases)) {
29+
return null;
30+
}
31+
32+
let deprecatedProp;
33+
const match = aliases.some(alias => Boolean(source[(deprecatedProp = alias)]));
34+
35+
if (match) {
36+
if (!is(type, source[deprecatedProp])) {
37+
throw new Error(format('should specify %s for %s', type, deprecatedProp));
38+
}
39+
40+
// deprecated message
41+
return source[deprecatedProp];
42+
}
43+
44+
return null;
45+
}
46+
47+
/**
48+
* @param {string} type
49+
* @param {*} value
50+
* @return {boolean}
51+
*/
52+
export function is(type, value) {
53+
return check[type](value);
54+
}
55+
56+
/**
57+
* @param {string} str
58+
* @return {string}
59+
*/
60+
export function removeQuotes(str) {
61+
return str.replace(/^["']|["']$/g, '');
62+
}

test/utility.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ok, equal, throws } from 'assert';
2+
import { get, is, removeQuotes } from '../src/utility';
3+
4+
describe('utility', () => {
5+
describe('get()', () => {
6+
it('should return value for an existing property', () => equal(get('a', null, 'string', {a: 'val'}), 'val'));
7+
8+
it('should return value for an existing alias', () => equal(get('a', ['b'], 'string', {b: 'val'}), 'val'));
9+
10+
it('should return null for a non-existing property', () => equal(get('a', null, 'array', {}), null));
11+
12+
it('should throw an error for the specified key with wrong type of value', () => {
13+
throws(() => get('a', null, 'string', {a: 5}));
14+
});
15+
});
16+
17+
describe('is()', () => {
18+
it('should return true for an array', () => ok(is('array', [])));
19+
20+
it('should return false for not an array', () => ok(!is('array', null)));
21+
22+
it('should return true for a function', () => ok(is('function', () => {})));
23+
24+
it('should return false for not a function', () => ok(!is('function', null)));
25+
26+
it('should return true for a string', () => ok(is('string', '')));
27+
28+
it('should return false for not a string', () => ok(!is('string', null)));
29+
});
30+
31+
describe('removeQuotes()', () => {
32+
it('should remove quotes', () => equal(removeQuotes('"TEST"'), 'TEST'));
33+
});
34+
});

0 commit comments

Comments
 (0)