Skip to content

Commit e265f0d

Browse files
committed
replace multiple incexOf calls with a regex call
1 parent 7b9391f commit e265f0d

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/plots/cartesian/axes.js

+33-31
Original file line numberDiff line numberDiff line change
@@ -697,48 +697,50 @@ axes.calcTicks = function calcTicks(ax, opts) {
697697

698698
var definedDelta;
699699
if(isPeriod && ax.tickformat) {
700-
var _has = function(str) {
701-
return ax.tickformat.indexOf(str) !== -1;
702-
};
703-
704700
if(
705-
!_has('%f') && // microseconds as a decimal number [000000, 999999]
706-
!_has('%L') && // milliseconds as a decimal number [000, 999]
707-
!_has('%Q') && // milliseconds since UNIX epoch
708-
!_has('%s') && // seconds since UNIX epoch
709-
!_has('%S') && // second as a decimal number [00,61]
710-
!_has('%M') && // minute as a decimal number [00,59]
711-
!_has('%H') && // hour (24-hour clock) as a decimal number [00,23]
712-
!_has('%I') && // hour (12-hour clock) as a decimal number [01,12]
713-
!_has('%p') && // either AM or PM
714-
!_has('%X') // the locale’s time, such as %-I:%M:%S %p
701+
!(/%[fLQsSMHIpX]/.test(ax.tickformat))
702+
// %f: microseconds as a decimal number [000000, 999999]
703+
// %L: milliseconds as a decimal number [000, 999]
704+
// %Q: milliseconds since UNIX epoch
705+
// %s: seconds since UNIX epoch
706+
// %S: second as a decimal number [00,61]
707+
// %M: minute as a decimal number [00,59]
708+
// %H: hour (24-hour clock) as a decimal number [00,23]
709+
// %I: hour (12-hour clock) as a decimal number [01,12]
710+
// %p: either AM or PM
711+
// %X: the locale’s time, such as %-I:%M:%S %p
715712
) {
716713
if(
717-
_has('%A') || // full weekday name
718-
_has('%a') || // abbreviated weekday name
719-
_has('%d') || // zero-padded day of the month as a decimal number [01,31]
720-
_has('%e') || // space-padded day of the month as a decimal number [ 1,31]
721-
_has('%j') || // day of the year as a decimal number [001,366]
722-
_has('%u') || // Monday-based (ISO 8601) weekday as a decimal number [1,7]
723-
_has('%w') || // Sunday-based weekday as a decimal number [0,6]
724-
_has('%x') // the locale’s date, such as %-m/%-d/%Y
714+
/%[Aadejuwx]/.test(ax.tickformat)
715+
// %A: full weekday name
716+
// %a: abbreviated weekday name
717+
// %d: zero-padded day of the month as a decimal number [01,31]
718+
// %e: space-padded day of the month as a decimal number [ 1,31]
719+
// %j: day of the year as a decimal number [001,366]
720+
// %u: Monday-based (ISO 8601) weekday as a decimal number [1,7]
721+
// %w: Sunday-based weekday as a decimal number [0,6]
722+
// %x: the locale’s date, such as %-m/%-d/%Y
725723
) definedDelta = ONEDAY;
726724
else if(
727-
_has('%U') || // Sunday-based week of the year as a decimal number [00,53]
728-
_has('%V') || // ISO 8601 week of the year as a decimal number [01, 53]
729-
_has('%W') // Monday-based week of the year as a decimal number [00,53]
725+
/%[UVW]/.test(ax.tickformat)
726+
// %U: Sunday-based week of the year as a decimal number [00,53]
727+
// %V: ISO 8601 week of the year as a decimal number [01, 53]
728+
// %W: Monday-based week of the year as a decimal number [00,53]
730729
) definedDelta = ONEWEEK;
731730
else if(
732-
_has('%B') || // full month name
733-
_has('%b') || // abbreviated month name
734-
_has('%m') // month as a decimal number [01,12]
731+
/%[Bbm]/.test(ax.tickformat)
732+
// %B: full month name
733+
// %b: abbreviated month name
734+
// %m: month as a decimal number [01,12]
735735
) definedDelta = ONEAVGMONTH;
736736
else if(
737-
_has('%q') // quarter of the year as a decimal number [1,4]
737+
/%[q]/.test(ax.tickformat)
738+
// %q: quarter of the year as a decimal number [1,4]
738739
) definedDelta = ONEAVGQUARTER;
739740
else if(
740-
_has('%Y') || // year with century as a decimal number, such as 1999
741-
_has('%y') // year without century as a decimal number [00,99]
741+
/%[Yy]/.test(ax.tickformat)
742+
// %Y: year with century as a decimal number, such as 1999
743+
// %y: year without century as a decimal number [00,99]
742744
) definedDelta = ONEAVGYEAR;
743745
}
744746
}

0 commit comments

Comments
 (0)