Skip to content

Commit 8e8ef2d

Browse files
author
Marco Cesarato
committed
refactor: move some methods to utils
1 parent e2257a4 commit 8e8ef2d

File tree

2 files changed

+73
-30
lines changed

2 files changed

+73
-30
lines changed

index.js

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {Component} from "react";
22
import {Text, TextInput, TouchableHighlight, View} from "react-native";
33
import PropTypes from "prop-types";
44
import {Style} from "./style";
5+
import {isEmpty, isStringEmpty, debounce} from "./utils";
56

67
/**
78
* Default Color
@@ -95,7 +96,7 @@ class InputSpinner extends Component {
9596
onChange(num) {
9697
if (this.props.disabled) return;
9798
const current_value = this.state.value;
98-
const separator = !this.isStringEmpty(this.props.decimalSeparator)
99+
const separator = !isEmpty(this.props.decimalSeparator)
99100
? this.props.decimalSeparator
100101
: ".";
101102
if (
@@ -159,7 +160,6 @@ class InputSpinner extends Component {
159160
return num;
160161
}
161162

162-
163163
/**
164164
* Limit value to be within max and min range
165165
* @param value
@@ -190,7 +190,7 @@ class InputSpinner extends Component {
190190
*/
191191
parseNum(num) {
192192
num = String(num).replace(
193-
!this.isStringEmpty(this.props.decimalSeparator)
193+
!isEmpty(this.props.decimalSeparator)
194194
? this.props.decimalSeparator
195195
: ".",
196196
"."
@@ -225,13 +225,15 @@ class InputSpinner extends Component {
225225
} else {
226226
value = String(this.parseNum(value));
227227
}
228-
let hasPlaceholder = value === "0" && !this.isStringEmpty(this.props.placeholder);
229-
return hasPlaceholder ? "" : value.replace(
230-
".",
231-
!this.isStringEmpty(this.props.decimalSeparator)
232-
? this.props.decimalSeparator
233-
: "."
234-
);
228+
let hasPlaceholder = value === "0" && !isEmpty(this.props.placeholder);
229+
return hasPlaceholder
230+
? ""
231+
: value.replace(
232+
".",
233+
!isEmpty(this.props.decimalSeparator)
234+
? this.props.decimalSeparator
235+
: "."
236+
);
235237
}
236238

237239
/**
@@ -391,24 +393,6 @@ class InputSpinner extends Component {
391393
this.textInput.clear();
392394
}
393395

394-
/**
395-
* Is object empty
396-
* @param obj
397-
* @returns {boolean}
398-
*/
399-
isObjectEmpty(obj) {
400-
return Object.entries(obj).length === 0 && obj.constructor === Object;
401-
}
402-
403-
/**
404-
* Is string empty
405-
* @param str
406-
* @returns {boolean|boolean}
407-
*/
408-
isStringEmpty(str) {
409-
return !str || String(str) === "";
410-
}
411-
412396
/**
413397
* Is text input editable
414398
* @returns {boolean|Boolean}
@@ -625,7 +609,7 @@ class InputSpinner extends Component {
625609
* @private
626610
*/
627611
_getStyleButtonPress() {
628-
return this.isObjectEmpty(this.props.buttonPressStyle)
612+
return isEmpty(this.props.buttonPressStyle)
629613
? this.props.buttonStyle
630614
: this.props.buttonPressStyle;
631615
}
@@ -803,7 +787,9 @@ class InputSpinner extends Component {
803787
*/
804788
render() {
805789
return (
806-
<View style={this._getContainerStyle()} {...this.props.containerProps}>
790+
<View
791+
style={this._getContainerStyle()}
792+
{...this.props.containerProps}>
807793
{this._renderLeftButton()}
808794

809795
{this.props.prepend}

utils.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Is string empty
3+
* @param str
4+
* @returns {boolean|boolean}
5+
*/
6+
export const isStringEmpty = (str) => {
7+
return String(str) === "";
8+
};
9+
10+
/**
11+
* Is empty
12+
* @param x
13+
* @returns {boolean}
14+
*/
15+
export const isEmpty = (x) => {
16+
if (typeof x === typeof "") x = x.replace(/\s/g, "");
17+
18+
if (!x) return true;
19+
if (x === "") return true;
20+
if (x === {}) return true;
21+
if (x === []) return true;
22+
if (x == null) return true;
23+
if (typeof x === "undefined") return true;
24+
if (x === "") return true;
25+
if (x === function () {}) return true;
26+
27+
if (typeof x === typeof {}) {
28+
if (Object.entries(x).length === 0 && x.constructor === Object) {
29+
return true;
30+
}
31+
for (let key in x) {
32+
if (x.hasOwnProperty(key) && !isEmpty(x[key])) {
33+
return false;
34+
}
35+
}
36+
return true;
37+
}
38+
return false;
39+
};
40+
41+
/**
42+
* Debounce
43+
* @param callback
44+
* @param wait
45+
* @param context
46+
* @returns {function(...[*]=): void}
47+
*/
48+
export const debounce = (callback, wait, context = null) => {
49+
let timeout;
50+
return (...args) => {
51+
if (context === null) {
52+
context = this;
53+
}
54+
clearTimeout(timeout);
55+
timeout = setTimeout(() => callback.apply(context, args), wait);
56+
};
57+
};

0 commit comments

Comments
 (0)