Skip to content

Commit 12fa9c9

Browse files
danielkaradachkigyoshev
authored andcommitted
refactor: replace constants
1 parent b7fd05a commit 12fa9c9

File tree

8 files changed

+31
-29
lines changed

8 files changed

+31
-29
lines changed

src/cldr/date-field-name.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { localeInfo } from './info';
22
import { errors } from '../errors';
3+
import { DEFAULT_LOCALE } from '../common/constants';
34

4-
export default function dateFieldName(options, locale = "en") {
5+
export default function dateFieldName(options, locale = DEFAULT_LOCALE) {
56
const info = localeInfo(locale);
67
const dateFields = info.calendar.dateFields;
78
if (!dateFields) {

src/cldr/date-format-names.js

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

34
function lowerArray(arr) {
45
const result = [];
@@ -25,7 +26,7 @@ export default function dateFormatNames(locale, options) {
2526
const { type, nameType, standAlone, lower } = options;
2627
const info = getLocaleInfo(locale);
2728
const formatType = standAlone ? "stand-alone" : "format";
28-
const lowerNameType = (lower ? "lower-" : "") + nameType;
29+
const lowerNameType = (lower ? "lower-" : EMPTY) + nameType;
2930
const formatNames = info.calendar[type][formatType];
3031
let result = formatNames[lowerNameType];
3132
if (!result && lower) {

src/dates/date-pattern.js

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

34
const REMOVAL_PENALTY = 120;
45
const ADDITION_PENALTY = 20;
@@ -103,7 +104,7 @@ function findBestMatch(specifiers, availableFormats) {
103104
if (!match) {
104105
score -= REMOVAL_PENALTY;
105106
} else {
106-
currentFormat = currentFormat.replace(match, "");
107+
currentFormat = currentFormat.replace(match, EMPTY);
107108
if (match.length !== specifier.length) {
108109
let delta = Math.max(Math.min(LENGHT_DELTA[match.length] - LENGHT_DELTA[specifier.length], 2), -2);
109110
score -= PENALTIES[delta];
@@ -185,7 +186,7 @@ function skeletonFromOptions(options) {
185186
}
186187
}
187188

188-
return result.join("");
189+
return result.join(EMPTY);
189190
}
190191

191192
export default function datePattern(format, info) {

src/dates/format-date.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { localeInfo, firstDay } from '../cldr';
2+
import { DEFAULT_LOCALE, EMPTY } from '../common/constants';
23
import formatString from '../common/format-string';
34
import datePattern from './date-pattern';
45
import formatNames from './format-names';
@@ -46,7 +47,7 @@ function formatTimeZone(date, info, options) {
4647
const minutes = hoursMinutes[1] || 0;
4748
let result = sign + (shortHours ? hoursMinutes[0] : pad(hoursMinutes[0], 2));
4849
if (minutes || !optionalMinutes) {
49-
result += (separator ? ":" : "") + pad(minutes, 2);
50+
result += (separator ? ":" : EMPTY) + pad(minutes, 2);
5051
}
5152

5253
if (localizedName) {
@@ -116,7 +117,7 @@ formatters.S = function(date, formatLength) {
116117
if (milliseconds !== 0) {
117118
result = String(date.getMilliseconds() / 1000).split(".")[1].substr(0, formatLength);
118119
} else {
119-
result = pad("", formatLength);
120+
result = pad(EMPTY, formatLength);
120121
}
121122
return result;
122123
};
@@ -174,10 +175,10 @@ formatters.q = function(date, formatLength, info) {
174175

175176
formatters.Q = formatQuarter;
176177

177-
export default function formatDate(date, format, locale = "en") {
178+
export default function formatDate(date, format, locale = DEFAULT_LOCALE) {
178179
if (!isDate(date)) {
179180
if (date === undefined || date === null) {
180-
return '';
181+
return EMPTY;
181182
}
182183
return date;
183184
}

src/dates/parse-date.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { adjustDST, convertTimeZone } from './time-utils';
22
import { localeInfo } from '../cldr';
3+
import { DEFAULT_LOCALE, EMPTY } from '../common/constants';
34
import { errors } from '../errors';
45
import formatNames from './format-names';
56
import datePattern from './date-pattern';
@@ -116,7 +117,7 @@ function calendarGmtFormats(calendar) {
116117
throw errors.NoGMTInfo.error();
117118
}
118119

119-
return [ gmtFormat.replace(PLACEHOLDER, "").toLowerCase(), gmtZeroFormat.replace(PLACEHOLDER, "").toLowerCase() ];
120+
return [ gmtFormat.replace(PLACEHOLDER, EMPTY).toLowerCase(), gmtZeroFormat.replace(PLACEHOLDER, EMPTY).toLowerCase() ];
120121
}
121122

122123
function parseTimeZoneOffset(state, info, options) {
@@ -431,7 +432,7 @@ function createDate(state) {
431432
}
432433

433434
function parseExact(value, format, info) {
434-
let pattern = datePattern(format, info).split("");
435+
let pattern = datePattern(format, info).split(EMPTY);
435436

436437
const state = {
437438
format: pattern,
@@ -521,7 +522,7 @@ function defaultFormats(calendar) {
521522
return formats.concat(standardDateFormats);
522523
}
523524

524-
export default function parseDate(value, formats, locale = "en") {
525+
export default function parseDate(value, formats, locale = DEFAULT_LOCALE) {
525526
if (!value) {
526527
return null;
527528
}

src/dates/split-date-format.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DEFAULT_LOCALE } from '../common/constants';
12
import datePattern from './date-pattern';
23
import dateNameType from './date-name-type';
34
import { dateFormatRegExp, DATE_FIELD_MAP } from './constants';
@@ -56,7 +57,7 @@ function isHour12(pattern) {
5657
return pattern === 'h';
5758
}
5859

59-
export default function splitDateFormat(format, locale = 'en') {
60+
export default function splitDateFormat(format, locale = DEFAULT_LOCALE) {
6061
const info = localeInfo(locale);
6162
const pattern = datePattern(format, info);
6263
const parts = [];

src/format.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { formatDate } from './dates';
22
import { formatNumber } from './numbers';
3+
import { EMPTY } from './common/constants';
34
import isDate from './common/is-date';
45

56
const formatRegExp = /\{(\d+)(:[^\}]+)?\}/g;
@@ -13,13 +14,13 @@ export function toString(value, format, locale) {
1314
}
1415
}
1516

16-
return value !== undefined && value !== null ? value : "";
17+
return value !== undefined && value !== null ? value : EMPTY;
1718
}
1819

1920
export function format(format, values, locale) {
2021
return format.replace(formatRegExp, function(match, index, placeholderFormat) {
2122
let value = values[parseInt(index, 10)];
2223

23-
return toString(value, placeholderFormat ? placeholderFormat.substring(1) : "", locale);
24+
return toString(value, placeholderFormat ? placeholderFormat.substring(1) : EMPTY, locale);
2425
});
2526
}

src/numbers/custom-number-format.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1+
import { CURRENCY, PERCENT, LIST_SEPARATOR, GROUP_SEPARATOR, CURRENCY_PLACEHOLDER, PERCENT_PLACEHOLDER, POINT, EMPTY } from '../common/constants';
12
import formatCurrencySymbol from './format-currency-symbol';
23
import groupInteger from './group-integer';
34
import round from '../common/round';
45

5-
const CURRENCY_SYMBOL = "$";
6-
const PERCENT_SYMBOL = "%";
76
const PLACEHOLDER = "__??__";
8-
const CURRENCY = "currency";
9-
const PERCENT = "percent";
10-
const POINT = ".";
11-
const COMMA = ",";
7+
128
const SHARP = "#";
139
const ZERO = "0";
14-
const EMPTY = "";
1510

1611
const literalRegExp = /(\\.)|(['][^']*[']?)|(["][^"]*["]?)/g;
1712
const trailingZerosRegExp = /(\.(?:[0-9]*[1-9])?)0+$/g;
@@ -23,8 +18,8 @@ function setFormatLiterals(formatOptions) {
2318
if (format.indexOf("'") > -1 || format.indexOf("\"") > -1 || format.indexOf("\\") > -1) {
2419
const literals = formatOptions.literals = [];
2520
formatOptions.format = format.replace(literalRegExp, function(match) {
26-
const quoteChar = match.charAt(0).replace("\\", "");
27-
const literal = match.slice(1).replace(quoteChar, "");
21+
const quoteChar = match.charAt(0).replace("\\", EMPTY);
22+
const literal = match.slice(1).replace(quoteChar, EMPTY);
2823

2924
literals.push(literal);
3025

@@ -42,7 +37,7 @@ function trimTrailingZeros(value, lastZero) {
4237
trimRegex = new RegExp(`(\\.[0-9]{${ lastZero }}[1-9]*)0+$`, 'g');
4338
}
4439

45-
return value.replace(trimRegex, '$1').replace(trailingPointRegExp, '');
40+
return value.replace(trimRegex, '$1').replace(trailingPointRegExp, EMPTY);
4641
}
4742

4843
function roundNumber(formatOptions) {
@@ -106,7 +101,7 @@ function isConstantFormat(format) {
106101

107102
function setValueSpecificFormat(formatOptions) {
108103
let { number, format } = formatOptions;
109-
format = format.split(";");
104+
format = format.split(LIST_SEPARATOR);
110105
if (formatOptions.negative && format[1]) {
111106
format = format[1];
112107
formatOptions.hasNegativeFormat = true;
@@ -127,20 +122,20 @@ function setStyleOptions(formatOptions, info) {
127122
const format = formatOptions.format;
128123

129124
//multiply number if the format has percent
130-
if (format.indexOf(PERCENT_SYMBOL) !== -1) {
125+
if (format.indexOf(PERCENT_PLACEHOLDER) !== -1) {
131126
formatOptions.style = PERCENT;
132127
formatOptions.symbol = info.numbers.symbols.percentSign;
133128
formatOptions.number *= 100;
134129
}
135130

136-
if (format.indexOf(CURRENCY_SYMBOL) !== -1) {
131+
if (format.indexOf(CURRENCY_PLACEHOLDER) !== -1) {
137132
formatOptions.style = CURRENCY;
138133
formatOptions.symbol = formatCurrencySymbol(info);
139134
}
140135
}
141136

142137
function setGroupOptions(formatOptions) {
143-
formatOptions.hasGroup = formatOptions.format.indexOf(COMMA) > -1;
138+
formatOptions.hasGroup = formatOptions.format.indexOf(GROUP_SEPARATOR) > -1;
144139
if (formatOptions.hasGroup) {
145140
formatOptions.format = formatOptions.format.replace(commaRegExp, EMPTY);
146141
}
@@ -185,7 +180,7 @@ function replaceStyleSymbols(number, style, symbol) {
185180
result = EMPTY;
186181
for (let idx = 0, length = number.length; idx < length; idx++) {
187182
let ch = number.charAt(idx);
188-
result += (ch === CURRENCY_SYMBOL || ch === PERCENT_SYMBOL) ? symbol : ch;
183+
result += (ch === CURRENCY_PLACEHOLDER || ch === PERCENT_PLACEHOLDER) ? symbol : ch;
189184
}
190185
}
191186
return result;

0 commit comments

Comments
 (0)