Skip to content

Commit 024cfd6

Browse files
committed
Less clutter when setting the configuration.
1 parent dc8d90f commit 024cfd6

12 files changed

+158
-165
lines changed

dist/index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Object.defineProperty(exports, '__esModule', {
66

77
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
88

9-
var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
9+
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
1010

1111
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
1212

@@ -18,10 +18,6 @@ var _linkClass = require('./linkClass');
1818

1919
var _linkClass2 = _interopRequireDefault(_linkClass);
2020

21-
var _makeConfig = require('./makeConfig');
22-
23-
var _makeConfig2 = _interopRequireDefault(_makeConfig);
24-
2521
var decoratorConstructor = undefined,
2622
functionConstructor = undefined;
2723

@@ -33,9 +29,7 @@ var decoratorConstructor = undefined,
3329
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
3430
* @return {Function}
3531
*/
36-
functionConstructor = function (Component, styles) {
37-
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
38-
32+
functionConstructor = function (Component, styles, options) {
3933
return (function (_Component) {
4034
_inherits(_class, _Component);
4135

@@ -48,7 +42,7 @@ functionConstructor = function (Component, styles) {
4842
_createClass(_class, [{
4943
key: 'render',
5044
value: function render() {
51-
return (0, _linkClass2['default'])(_get(Object.getPrototypeOf(_class.prototype), 'render', this).call(this), styles, (0, _makeConfig2['default'])(options));
45+
return (0, _linkClass2['default'])(_get(Object.getPrototypeOf(_class.prototype), 'render', this).call(this), styles, options);
5246
}
5347
}]);
5448

dist/linkClass.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,45 @@ var _react = require('react');
1010

1111
var _react2 = _interopRequireDefault(_react);
1212

13+
var _makeConfiguration = require('./makeConfiguration');
14+
15+
var _makeConfiguration2 = _interopRequireDefault(_makeConfiguration);
16+
1317
var linkClass = undefined;
1418

1519
/**
1620
* @param {ReactElement} element
1721
* @param {Object} styles CSS modules class map.
18-
* @param {CSSModules~Options} options
22+
* @param {CSSModules~Options} userConfiguration
1923
* @return {ReactElement}
2024
*/
21-
linkClass = function (element) {
22-
var styles = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
23-
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
25+
linkClass = function (element, styles, userConfiguration) {
26+
if (styles === undefined) styles = {};
2427

2528
var appendClassName = undefined,
2629
childrenCount = undefined,
2730
clonedElement = undefined,
31+
configuration = undefined,
2832
newChildren = undefined,
2933
newProps = undefined,
3034
styleNames = undefined;
3135

36+
configuration = (0, _makeConfiguration2['default'])(userConfiguration);
37+
3238
styleNames = element.props.styleName;
3339

3440
if (styleNames) {
3541
styleNames = styleNames.split(' ');
3642

37-
if (options.allowMultiple === false && styleNames.length > 1) {
43+
if (configuration.allowMultiple === false && styleNames.length > 1) {
3844
throw new Error('ReactElement styleName property defines multiple module names ("' + element.props.styleName + '").');
3945
}
4046

4147
appendClassName = styleNames.map(function (styleName) {
4248
if (styles[styleName]) {
4349
return styles[styleName];
4450
} else {
45-
if (options.errorWhenNotFound === true) {
51+
if (configuration.errorWhenNotFound === true) {
4652
throw new Error('"' + styleName + '" CSS module is undefined.');
4753
}
4854

@@ -65,13 +71,13 @@ linkClass = function (element) {
6571
if (childrenCount > 1) {
6672
newChildren = _react2['default'].Children.map(element.props.children, function (node) {
6773
if (_react2['default'].isValidElement(node)) {
68-
return linkClass(node, styles, options);
74+
return linkClass(node, styles, configuration);
6975
} else {
7076
return node;
7177
}
7278
});
7379
} else if (childrenCount === 1) {
74-
newChildren = linkClass(_react2['default'].Children.only(element.props.children), styles, options);
80+
newChildren = linkClass(_react2['default'].Children.only(element.props.children), styles, configuration);
7581
}
7682
}
7783

dist/makeConfig.js

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

dist/makeConfiguration.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, '__esModule', {
4+
value: true
5+
});
6+
7+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
8+
9+
var _utils = require('./utils');
10+
11+
var _utils2 = _interopRequireDefault(_utils);
12+
13+
/**
14+
* @typedef CSSModules~Options
15+
* @see {@link https://github.com/gajus/react-css-modules#options}
16+
* @property {Boolean} allowMultiple
17+
* @property {Boolean} errorWhenNotFound
18+
*/
19+
20+
/**
21+
* @param {CSSModules~Options} userConfiguration
22+
* @return {CSSModules~Options}
23+
*/
24+
25+
exports['default'] = function () {
26+
var userConfiguration = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
27+
28+
var configuration = undefined;
29+
30+
configuration = {
31+
allowMultiple: false,
32+
errorWhenNotFound: true
33+
};
34+
35+
_utils2['default'].forEach(userConfiguration, function (value, name) {
36+
if (typeof configuration[name] === 'undefined') {
37+
throw new Error('Unknown configuration property "' + name + '".');
38+
}
39+
40+
if (typeof value !== 'boolean') {
41+
throw new Error('"' + name + '" property value must be a boolean.');
42+
}
43+
44+
configuration[name] = value;
45+
});
46+
47+
return configuration;
48+
};
49+
50+
module.exports = exports['default'];

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import linkClass from './linkClass';
2-
import makeConfig from './makeConfig';
32

43
let decoratorConstructor,
54
functionConstructor;
@@ -12,10 +11,10 @@ let decoratorConstructor,
1211
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
1312
* @return {Function}
1413
*/
15-
functionConstructor = (Component, styles, options = {}) => {
14+
functionConstructor = (Component, styles, options) => {
1615
return class extends Component {
1716
render () {
18-
return linkClass(super.render(), styles, makeConfig(options));
17+
return linkClass(super.render(), styles, options);
1918
}
2019
};
2120
};

src/linkClass.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
import React from 'react';
2+
import makeConfiguration from './makeConfiguration';
23

34
let linkClass;
45

56
/**
67
* @param {ReactElement} element
78
* @param {Object} styles CSS modules class map.
8-
* @param {CSSModules~Options} options
9+
* @param {CSSModules~Options} userConfiguration
910
* @return {ReactElement}
1011
*/
11-
linkClass = (element, styles = {}, options = {}) => {
12+
linkClass = (element, styles = {}, userConfiguration) => {
1213
let appendClassName,
1314
childrenCount,
1415
clonedElement,
16+
configuration,
1517
newChildren,
1618
newProps,
1719
styleNames;
1820

21+
configuration = makeConfiguration(userConfiguration);
22+
1923
styleNames = element.props.styleName;
2024

2125
if (styleNames) {
2226
styleNames = styleNames.split(' ');
2327

24-
if (options.allowMultiple === false && styleNames.length > 1) {
28+
if (configuration.allowMultiple === false && styleNames.length > 1) {
2529
throw new Error(`ReactElement styleName property defines multiple module names ("${element.props.styleName}").`);
2630
}
2731

2832
appendClassName = styleNames.map((styleName) => {
2933
if (styles[styleName]) {
3034
return styles[styleName];
3135
} else {
32-
if (options.errorWhenNotFound === true) {
36+
if (configuration.errorWhenNotFound === true) {
3337
throw new Error(`"${styleName}" CSS module is undefined.`);
3438
}
3539

@@ -52,13 +56,13 @@ linkClass = (element, styles = {}, options = {}) => {
5256
if (childrenCount > 1) {
5357
newChildren = React.Children.map(element.props.children, (node) => {
5458
if (React.isValidElement(node)) {
55-
return linkClass(node, styles, options);
59+
return linkClass(node, styles, configuration);
5660
} else {
5761
return node;
5862
}
5963
});
6064
} else if (childrenCount === 1) {
61-
newChildren = linkClass(React.Children.only(element.props.children), styles, options);
65+
newChildren = linkClass(React.Children.only(element.props.children), styles, configuration);
6266
}
6367
}
6468

src/makeConfig.js

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

src/makeConfiguration.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import _ from './utils';
2+
3+
/**
4+
* @typedef CSSModules~Options
5+
* @see {@link https://github.com/gajus/react-css-modules#options}
6+
* @property {Boolean} allowMultiple
7+
* @property {Boolean} errorWhenNotFound
8+
*/
9+
10+
/**
11+
* @param {CSSModules~Options} userConfiguration
12+
* @return {CSSModules~Options}
13+
*/
14+
export default (userConfiguration = {}) => {
15+
let configuration;
16+
17+
configuration = {
18+
allowMultiple: false,
19+
errorWhenNotFound: true
20+
};
21+
22+
_.forEach(userConfiguration, (value, name) => {
23+
if (typeof configuration[name] === 'undefined') {
24+
throw new Error(`Unknown configuration property "${name}".`);
25+
}
26+
27+
if (typeof value !== 'boolean') {
28+
throw new Error(`"${name}" property value must be a boolean.`);
29+
}
30+
31+
configuration[name] = value;
32+
});
33+
34+
return configuration;
35+
};

src/utils.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import keys from 'lodash/object/keys';
2-
import difference from 'lodash/array/difference';
31
import forEach from 'lodash/collection/forEach';
42

53
export default {
6-
keys,
7-
difference,
84
forEach
95
};

test/linkClass.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import React from 'react';
66
import TestUtils from 'react-addons-test-utils';
77
import jsdom from 'jsdom';
8-
import linkClass from './../dist/linkClass';
8+
import linkClass from './../src/linkClass';
99

1010
describe('linkClass', () => {
1111
context('when ReactElement does not define styleName', () => {

0 commit comments

Comments
 (0)