Skip to content

Commit 9dacfcb

Browse files
danielkaradachkigyoshev
authored andcommitted
refactor: add common methods for type check
1 parent 12fa9c9 commit 9dacfcb

File tree

8 files changed

+18
-7
lines changed

8 files changed

+18
-7
lines changed

src/cldr/info.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import defaultData from './default-data';
2+
import isString from '../common/is-string';
23
import { errors } from '../errors';
34

45
function availableLocaleInfo(fullName, suffixes) {
@@ -29,7 +30,7 @@ export const cldr = defaultData;
2930

3031
export function getLocaleInfo(locale) {
3132
let info;
32-
if (typeof locale === "string") {
33+
if (isString(locale)) {
3334
info = localeInfo(locale);
3435
} else {
3536
info = locale;

src/common/is-number.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function isNumber(value) {
2+
return typeof value === "number";
3+
}

src/common/is-string.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function isString(value) {
2+
return typeof value === "string";
3+
}

src/dates/date-pattern.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import formatString from '../common/format-string';
2+
import isString from '../common/is-string';
23
import { EMPTY } from '../common/constants';
34

45
const REMOVAL_PENALTY = 120;
@@ -192,7 +193,7 @@ function skeletonFromOptions(options) {
192193
export default function datePattern(format, info) {
193194
const calendar = info.calendar;
194195
let result;
195-
if (typeof format === "string") {
196+
if (isString(format)) {
196197
if (calendar.patterns[format]) {
197198
result = calendar.patterns[format];
198199
} else {

src/dates/split-date-format.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DEFAULT_LOCALE } from '../common/constants';
2+
import isNumber from '../common/is-number';
23
import datePattern from './date-pattern';
34
import dateNameType from './date-name-type';
45
import { dateFormatRegExp, DATE_FIELD_MAP } from './constants';
@@ -39,7 +40,6 @@ const NAME_TYPES = {
3940
};
4041

4142
const LITERAL = 'literal';
42-
const NUMBER = 'number';
4343

4444
function addLiteral(parts, value) {
4545
const lastPart = parts[parts.length - 1];
@@ -88,7 +88,7 @@ export default function splitDateFormat(format, locale = DEFAULT_LOCALE) {
8888
const names = NAME_TYPES[type];
8989

9090
if (names) {
91-
const minLength = typeof names.minLength === NUMBER ? names.minLength : names.minLength[specifier];
91+
const minLength = isNumber(names.minLength) ? names.minLength : names.minLength[specifier];
9292
const patternLength = value.length;
9393

9494
if (patternLength >= minLength) {

src/format.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { formatDate } from './dates';
22
import { formatNumber } from './numbers';
33
import { EMPTY } from './common/constants';
44
import isDate from './common/is-date';
5+
import isNumber from './common/is-number';
56

67
const formatRegExp = /\{(\d+)(:[^\}]+)?\}/g;
78

89
export function toString(value, format, locale) {
910
if (format) {
1011
if (isDate(value)) {
1112
return formatDate(value, format, locale);
12-
} else if (typeof value === "number") {
13+
} else if (isNumber(value)) {
1314
return formatNumber(value, format, locale);
1415
}
1516
}

src/numbers/format-number.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { localeInfo } from '../cldr';
22
import { CURRENCY, ACCOUNTING, DECIMAL, PERCENT, SCIENTIFIC, DEFAULT_LOCALE, NUMBER_PLACEHOLDER, EMPTY } from '../common/constants';
3+
import isString from '../common/is-string';
34
import standardNumberFormat from './standard-number-format';
45
import customNumberFormat from './custom-number-format';
56

@@ -35,7 +36,7 @@ function standardFormatOptions(format) {
3536

3637
function getFormatOptions(format) {
3738
let formatOptions;
38-
if (typeof format === "string") {
39+
if (isString(format)) {
3940
formatOptions = standardFormatOptions(format);
4041
} else {
4142
formatOptions = format;

src/numbers/parse-number.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { localeInfo, localeCurrency, currencyDisplays } from '../cldr';
22
import { PERCENT, NUMBER_PLACEHOLDER, CURRENCY_PLACEHOLDER, DEFAULT_LOCALE, EMPTY, POINT } from '../common/constants';
3+
import isNumber from '../common/is-number';
34
import isCurrencyStyle from './is-currency-style';
45

56
const exponentRegExp = /[eE][\-+]?[0-9]+/;
@@ -48,7 +49,7 @@ export default function parseNumber(value, locale = DEFAULT_LOCALE, format = {})
4849
return null;
4950
}
5051

51-
if (typeof value === "number") {
52+
if (isNumber(value)) {
5253
return value;
5354
}
5455

0 commit comments

Comments
 (0)