src/utils.js   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 1.83

Size

Lines of Code 124
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 0
c 4
b 0
f 0
nc 1
dl 0
loc 124
rs 10
wmc 11
mnd 2
bc 10
fnc 6
bpm 1.6666
cpm 1.8333
noi 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A utils.js ➔ createClassName 0 3 1
B utils.js ➔ generalClassNames 0 24 1
A utils.js ➔ removeProps 0 11 4
A utils.js ➔ objectValues 0 11 3
A utils.js ➔ isDefined 0 3 1
A utils.js ➔ objectKeys 0 3 1
1
import { PropTypes } from 'react';
2
import classNames from 'classnames';
3
import { Breakpoints, FloatTypes } from './enums';
4
5
/**
6
 * Property types for general properties.
7
 *
8
 * @returns {Object}
9
 */
10
export const GeneralPropTypes = {
11
  showFor: PropTypes.oneOf([Breakpoints.MEDIUM, Breakpoints.LARGE]),
12
  showOnlyFor: PropTypes.oneOf(objectValues(Breakpoints)),
13
  hideFor: PropTypes.oneOf([Breakpoints.MEDIUM, Breakpoints.LARGE]),
14
  hideOnlyFor: PropTypes.oneOf(objectValues(Breakpoints)),
15
  isHidden: PropTypes.bool,
16
  isInvisible: PropTypes.bool,
17
  showForLandscape: PropTypes.bool,
18
  showForPortrait: PropTypes.bool,
19
  showForSr: PropTypes.bool,
20
  showOnFocus: PropTypes.bool,
21
  isClearfix: PropTypes.bool,
22
  float: PropTypes.oneOf(objectValues(FloatTypes))
23
};
24
25
/**
26
 * Creates class names from the given arguments.
27
 *
28
 * @param {*} args
29
 * @returns {string}
30
 */
31
export function createClassName(...args) {
32
  return classNames(...args);
33
}
34
35
/**
36
 * Parses the general class names from the given properties.
37
 *
38
 * @param {Object} props
39
 * @returns {Object}
40
 */
41
export function generalClassNames(props) {
42
  return {
43
    'show-for-medium': props.showFor === Breakpoints.MEDIUM,
44
    'show-for-large': props.showFor === Breakpoints.LARGE,
45
    'show-for-small-only': props.showOnlyFor === Breakpoints.SMALL,
46
    'show-for-medium-only': props.showOnlyFor === Breakpoints.MEDIUM,
47
    'show-for-large-only': props.showOnlyFor === Breakpoints.LARGE,
48
    'hide-for-medium': props.hideFor === Breakpoints.MEDIUM,
49
    'hide-for-large': props.hideFor === Breakpoints.LARGE,
50
    'hide-for-small-only': props.hideOnlyFor === Breakpoints.SMALL,
51
    'hide-for-medium-only': props.hideOnlyFor === Breakpoints.MEDIUM,
52
    'hide-for-large-only': props.hideOnlyFor === Breakpoints.LARGE,
53
    'hide': props.isHidden,
54
    'invisible': props.isInvisible,
55
    'show-for-landscape': props.showForLandscape,
56
    'show-for-portrait': props.showForPortrait,
57
    'show-for-sr': props.showForSr,
58
    'show-on-focus': props.showOnFocus,
59
    'clearfix': props.isClearfix,
60
    'float-left': props.float === FloatTypes.LEFT,
61
    'float-center': props.float === FloatTypes.CENTER,
62
    'float-right': props.float === FloatTypes.RIGHT
63
  };
64
}
65
66
/**
67
 * Returns the keys for the given object.
68
 * This method is used for getting the keys for prop types.
69
 *
70
 * @param {Object} object
71
 * @returns {Array}
72
 */
73
export function objectKeys(object) {
74
  return Object.keys(object);
75
}
76
77
/**
78
 * Returns the values for the given object.
79
 * This method is used for getting the values for enumerables.
80
 *
81
 * @param {Object} object
82
 * @returns {Array}
83
 */
84
export function objectValues(object) {
85
  const values = [];
86
87
  for (const property in object) {
88
    if (object.hasOwnProperty(property)) {
89
      values.push(object[property]);
90
    }
91
  }
92
93
  return values;
94
}
95
96
/**
97
 * Removes properties from the given object.
98
 * This method is used for removing valid attributes from component props prior to rendering.
99
 *
100
 * @param {Object} object
101
 * @param {Array} remove
102
 * @returns {Object}
103
 */
104
export function removeProps(object, remove) {
105
  const result = {};
106
107
  for (const property in object) {
108
    if (object.hasOwnProperty(property) && remove.indexOf(property) === -1) {
109
      result[property] = object[property];
110
    }
111
  }
112
113
  return result;
114
}
115
116
/**
117
 * Returns whether or not the given value is defined.
118
 *
119
 * @param {*} value
120
 * @returns {boolean}
121
 */
122
export function isDefined(value) {
123
  return typeof value !== 'undefined';
124
}
125