@superset-ui/core
Version:
70 lines • 2.87 kB
JavaScript
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { timeFormatLocale, timeFormat, utcFormat, } from 'd3-time-format';
import { utcUtils, localTimeUtils } from '../utils/d3Time';
import TimeFormatter from '../TimeFormatter';
export default function createMultiFormatter({ id, label, description, formats = {}, useLocalTime = false, locale, }) {
const { millisecond = '.%L', second = ':%S', minute = '%I:%M', hour = '%I %p', day = '%a %d', week = '%b %d', month = '%B', year = '%Y', } = formats;
let formatFunc;
if (typeof locale === 'undefined') {
formatFunc = useLocalTime ? timeFormat : utcFormat;
}
else {
const formatLocale = timeFormatLocale(locale);
formatFunc = useLocalTime ? formatLocale.format : formatLocale.utcFormat;
}
const formatMillisecond = formatFunc(millisecond);
const formatSecond = formatFunc(second);
const formatMinute = formatFunc(minute);
const formatHour = formatFunc(hour);
const formatDay = formatFunc(day);
const formatFirstDayOfWeek = formatFunc(week);
const formatMonth = formatFunc(month);
const formatYear = formatFunc(year);
const { hasMillisecond, hasSecond, hasMinute, hasHour, isNotFirstDayOfMonth, isNotFirstDayOfWeek, isNotFirstMonth, } = useLocalTime ? localTimeUtils : utcUtils;
function multiFormatFunc(date) {
if (hasMillisecond(date)) {
return formatMillisecond;
}
if (hasSecond(date)) {
return formatSecond;
}
if (hasMinute(date)) {
return formatMinute;
}
if (hasHour(date)) {
return formatHour;
}
if (isNotFirstDayOfMonth(date)) {
return isNotFirstDayOfWeek(date) ? formatDay : formatFirstDayOfWeek;
}
if (isNotFirstMonth(date)) {
return formatMonth;
}
return formatYear;
}
return new TimeFormatter({
description,
formatFunc: (date) => multiFormatFunc(date)(date),
id,
label,
useLocalTime,
});
}
//# sourceMappingURL=createMultiFormatter.js.map