Skip to content
This repository was archived by the owner on Jun 17, 2023. It is now read-only.

Commit b8a33f8

Browse files
theruzievTheSharpieOne
authored andcommitted
refactor: Remove lodash dependencies (reactstrap#1658)
* Remove lodash dependencies * Add tests and remove lodash from locks
1 parent a5f4882 commit b8a33f8

File tree

7 files changed

+130
-27
lines changed

7 files changed

+130
-27
lines changed

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@
101101
"dependencies": {
102102
"@babel/runtime": "^7.2.0",
103103
"classnames": "^2.2.3",
104-
"lodash.isfunction": "^3.0.9",
105-
"lodash.isobject": "^3.0.2",
106-
"lodash.tonumber": "^4.0.3",
107104
"prop-types": "^15.5.8",
108105
"react-lifecycles-compat": "^3.0.4",
109106
"react-popper": "^1.3.3",

src/Col.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import isobject from 'lodash.isobject';
21
import React from 'react';
32
import PropTypes from 'prop-types';
43
import classNames from 'classnames';
5-
import { mapToCssModules, tagPropType } from './utils';
4+
import { mapToCssModules, tagPropType, isObject } from './utils';
65

76
const colWidths = ['xs', 'sm', 'md', 'lg', 'xl'];
87
const stringOrNumberProp = PropTypes.oneOfType([PropTypes.number, PropTypes.string]);
@@ -66,7 +65,7 @@ const Col = (props) => {
6665

6766
const isXs = !i;
6867

69-
if (isobject(columnProp)) {
68+
if (isObject(columnProp)) {
7069
const colSizeInterfix = isXs ? '-' : `-${colWidth}-`;
7170
const colClass = getColumnSizeClass(isXs, colWidth, columnProp.size);
7271

src/Label.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import classNames from 'classnames';
4-
import isobject from 'lodash.isobject';
5-
import { mapToCssModules, tagPropType } from './utils';
4+
import { mapToCssModules, tagPropType, isObject } from './utils';
65

76
const colWidths = ['xs', 'sm', 'md', 'lg', 'xl'];
87

@@ -77,7 +76,7 @@ const Label = (props) => {
7776
const isXs = !i;
7877
let colClass;
7978

80-
if (isobject(columnProp)) {
79+
if (isObject(columnProp)) {
8180
const colSizeInterfix = isXs ? '-' : `-${colWidth}-`;
8281
colClass = getColumnSizeClass(isXs, colWidth, columnProp.size);
8382

src/Progress.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import classNames from 'classnames';
4-
import toNumber from 'lodash.tonumber';
5-
import { mapToCssModules, tagPropType } from './utils';
4+
import { mapToCssModules, tagPropType, toNumber } from './utils';
65

76
const propTypes = {
87
children: PropTypes.node,

src/__tests__/utils.spec.js

+80
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,86 @@ describe('Utils', () => {
181181
});
182182
});
183183

184+
describe('isFunction', function() {
185+
it('should return `true` for functions', function() {
186+
function test(){}
187+
expect(Utils.isFunction(test)).toBe(true);
188+
expect(Utils.isFunction(Array.prototype.slice)).toBe(true);
189+
});
190+
191+
it('should return `true` for async functions', function() {
192+
async function asyncFunc() {}
193+
expect(Utils.isFunction(asyncFunc)).toEqual(typeof asyncFunc === 'function');
194+
});
195+
196+
it('should return `true` for generator functions', function() {
197+
function* genFunc() {}
198+
expect(Utils.isFunction(genFunc)).toEqual(typeof genFunc === 'function');
199+
});
200+
201+
202+
it('should return `false` for non-functions', function() {
203+
function toArgs(array) {
204+
return (function() { return arguments; }.apply(undefined, array));
205+
}
206+
expect(Utils.isFunction(toArgs([1, 2, 3]))).toBe(false);
207+
expect(Utils.isFunction([1, 2, 3])).toBe(false);
208+
expect(Utils.isFunction(true)).toBe(false);
209+
expect(Utils.isFunction(new Date)).toBe(false);
210+
expect(Utils.isFunction(new Error)).toBe(false);
211+
expect(Utils.isFunction({ 'a': 1 })).toBe(false);
212+
expect(Utils.isFunction(1)).toBe(false);
213+
expect(Utils.isFunction(/x/)).toBe(false);
214+
expect(Utils.isFunction('a')).toBe(false);
215+
expect(Utils.isFunction(Symbol("a"))).toBe(false);
216+
//
217+
if (document) {
218+
expect(Utils.isFunction(document.getElementsByTagName('body'))).toBe(false);
219+
}
220+
});
221+
222+
});
223+
224+
describe('isObject', function() {
225+
it('should return `true` for objects', function() {
226+
function toArgs(array) {
227+
return (function() { return arguments; }.apply(undefined, array));
228+
}
229+
expect(Utils.isObject([1, 2, 3])).toBe(true);
230+
expect(Utils.isObject(Object(false))).toBe(true);
231+
expect(Utils.isObject(new Date)).toBe(true);
232+
expect(Utils.isObject(new Error)).toBe(true);
233+
expect(Utils.isObject({ 'a': 1 })).toBe(true);
234+
expect(Utils.isObject({ 'a': 1 })).toBe(true);
235+
expect(Utils.isObject(Object(0))).toBe(true);
236+
expect(Utils.isObject(/x/)).toBe(true);
237+
expect(Utils.isObject(Object("a"))).toBe(true);
238+
if (document) {
239+
expect(Utils.isObject(document.body)).toBe(true);
240+
}
241+
});
242+
243+
it('should return `false` for non-objects', function() {
244+
245+
expect(Utils.isObject(0)).toBe(false);
246+
expect(Utils.isObject(false)).toBe(false);
247+
expect(Utils.isObject(1)).toBe(false);
248+
});
249+
250+
});
251+
252+
describe('toNumber', function() {
253+
it('should return number', function() {
254+
expect(Utils.toNumber("5")).toEqual(5);
255+
expect(Utils.toNumber("5.0")).toEqual(5);
256+
expect(Utils.toNumber("1.1")).toEqual(1.1);
257+
expect(Utils.toNumber("-1.1")).toEqual(-1.1);
258+
expect(Utils.toNumber(0/0)).toEqual(NaN);
259+
expect(Utils.toNumber(0)).toEqual(0);
260+
261+
});
262+
});
263+
184264
// TODO
185265
// describe('getScrollbarWidth', () => {
186266
// // jsdom workaround https://github.com/tmpvar/jsdom/issues/135#issuecomment-68191941

src/utils.js

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import isFunction from 'lodash.isfunction';
21
import PropTypes from 'prop-types';
32

43
// https://github.com/twbs/bootstrap/blob/v4.0.0-alpha.4/js/src/modal.js#L436-L443
@@ -226,6 +225,51 @@ export function isReactRefObj(target) {
226225
return false;
227226
}
228227

228+
function getTag(value) {
229+
if (value == null) {
230+
return value === undefined ? '[object Undefined]' : '[object Null]'
231+
}
232+
return Object.prototype.toString.call(value)
233+
}
234+
235+
export function toNumber(value) {
236+
const type = typeof value;
237+
const NAN = 0 / 0;
238+
if (type === 'number') {
239+
return value
240+
}
241+
if (type === 'symbol' || (type === 'object' && getTag(value) === '[object Symbol]')) {
242+
return NAN
243+
}
244+
if (isObject(value)) {
245+
const other = typeof value.valueOf === 'function' ? value.valueOf() : value;
246+
value = isObject(other) ? `${other}` : other
247+
}
248+
if (type !== 'string') {
249+
return value === 0 ? value : +value
250+
}
251+
value = value.replace(/^\s+|\s+$/g, '');
252+
const isBinary = /^0b[01]+$/i.test(value);
253+
return (isBinary || /^0o[0-7]+$/i.test(value))
254+
? parseInt(value.slice(2), isBinary ? 2 : 8)
255+
: (/^[-+]0x[0-9a-f]+$/i.test(value) ? NAN : +value)
256+
}
257+
258+
export function isObject(value) {
259+
const type = typeof value;
260+
return value != null && (type === 'object' || type === 'function')
261+
}
262+
263+
export function isFunction(value) {
264+
if (!isObject(value)) {
265+
return false
266+
}
267+
268+
const tag = getTag(value);
269+
return tag === '[object Function]' || tag === '[object AsyncFunction]' ||
270+
tag === '[object GeneratorFunction]' || tag === '[object Proxy]'
271+
}
272+
229273
export function findDOMElements(target) {
230274
if (isReactRefObj(target)) {
231275
return target.current;

yarn.lock

-15
Original file line numberDiff line numberDiff line change
@@ -7821,21 +7821,11 @@ lodash.isequal@^4.5.0:
78217821
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
78227822
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=
78237823

7824-
lodash.isfunction@^3.0.9:
7825-
version "3.0.9"
7826-
resolved "https://registry.yarnpkg.com/lodash.isfunction/-/lodash.isfunction-3.0.9.tgz#06de25df4db327ac931981d1bdb067e5af68d051"
7827-
integrity sha512-AirXNj15uRIMMPihnkInB4i3NHeb4iBtNg9WRWuK2o31S+ePwwNmDPaTL3o7dTJ+VXNZim7rFs4rxN4YU1oUJw==
7828-
78297824
lodash.ismatch@^4.4.0:
78307825
version "4.4.0"
78317826
resolved "https://registry.yarnpkg.com/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"
78327827
integrity sha1-dWy1FQyjum8RCFp4hJZF8Yj4Xzc=
78337828

7834-
lodash.isobject@^3.0.2:
7835-
version "3.0.2"
7836-
resolved "https://registry.yarnpkg.com/lodash.isobject/-/lodash.isobject-3.0.2.tgz#3c8fb8d5b5bf4bf90ae06e14f2a530a4ed935e1d"
7837-
integrity sha1-PI+41bW/S/kK4G4U8qUwpO2TXh0=
7838-
78397829
lodash.isplainobject@^4.0.6:
78407830
version "4.0.6"
78417831
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
@@ -7915,11 +7905,6 @@ lodash.templatesettings@^4.0.0:
79157905
dependencies:
79167906
lodash._reinterpolate "^3.0.0"
79177907

7918-
lodash.tonumber@^4.0.3:
7919-
version "4.0.3"
7920-
resolved "https://registry.yarnpkg.com/lodash.tonumber/-/lodash.tonumber-4.0.3.tgz#0b96b31b35672793eb7f5a63ee791f1b9e9025d9"
7921-
integrity sha1-C5azGzVnJ5Prf1pj7nkfG56QJdk=
7922-
79237908
lodash.uniq@^4.5.0:
79247909
version "4.5.0"
79257910
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

0 commit comments

Comments
 (0)