Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(configOrFormatString = isRequired('configOrFormatString')) {
const config = isString(configOrFormatString)
? { id: configOrFormatString }
: configOrFormatString;
let formatFunc;
let isInvalid = false;
const { id, label, description } = config;
try {
formatFunc = d3Format(id);
} catch (e) {
formatFunc = () => `Invalid format: ${id}`;
isInvalid = true;
}
register() {
const { key = isRequired('config.key') } = this.config;
getChartMetadataRegistry().registerValue(key, this.metadata);
getChartComponentRegistry().registerLoader(key, this.loadChart);
getChartControlPanelRegistry().registerValue(key, this.controlPanel);
getChartTransformPropsRegistry().registerLoader(key, this.loadTransformProps);
if (this.loadBuildQuery) {
getChartBuildQueryRegistry().registerLoader(key, this.loadBuildQuery);
}
return this;
}
constructor(config: {
id: string;
label?: string;
description?: string;
formatFunc: TimeFormatFunction;
useLocalTime?: boolean;
}) {
super((value: Date | null | undefined) => this.format(value));
const {
id = isRequired('config.id'),
label,
description = '',
formatFunc = isRequired('config.formatFunc'),
useLocalTime = false,
} = config;
this.id = id;
this.label = label || id;
this.description = description;
this.formatFunc = formatFunc;
this.useLocalTime = useLocalTime;
}
constructor(config: NumberFormatterConfig) {
super((value: number) => this.format(value));
const {
id = isRequired('config.id'),
label,
description = '',
formatFunc = isRequired('config.formatFunc'),
isInvalid = false,
} = config;
this.id = id;
this.label = label || id;
this.description = description;
this.formatFunc = formatFunc;
this.isInvalid = isInvalid;
}
constructor(config: {
id: string;
label?: string;
description?: string;
formatFunc: TimeFormatFunction;
useLocalTime?: boolean;
}) {
super((value: Date | null | undefined) => this.format(value));
const {
id = isRequired('config.id'),
label,
description = '',
formatFunc = isRequired('config.formatFunc'),
useLocalTime = false,
} = config;
this.id = id;
this.label = label || id;
this.description = description;
this.formatFunc = formatFunc;
this.useLocalTime = useLocalTime;
}
constructor(config: NumberFormatterConfig) {
super((value: number) => this.format(value));
const {
id = isRequired('config.id'),
label,
description = '',
formatFunc = isRequired('config.formatFunc'),
isInvalid = false,
} = config;
this.id = id;
this.label = label || id;
this.description = description;
this.formatFunc = formatFunc;
this.isInvalid = isInvalid;
}
unregister() {
const { key = isRequired('config.key') } = this.config;
getChartMetadataRegistry().remove(key);
getChartComponentRegistry().remove(key);
getChartControlPanelRegistry().remove(key);
getChartTransformPropsRegistry().remove(key);
getChartBuildQueryRegistry().remove(key);
return this;
}
export default function createD3NumberFormatter(config: {
description?: string;
formatString: string;
label?: string;
locale?: FormatLocaleDefinition;
}) {
const { description, formatString = isRequired('config.formatString'), label, locale } = config;
let formatFunc: NumberFormatFunction;
let isInvalid = false;
try {
formatFunc =
typeof locale === 'undefined'
? d3Format(formatString)
: formatLocale(locale).format(formatString);
} catch (e) {
formatFunc = value => `${value} (Invalid format: ${formatString})`;
isInvalid = true;
}
return new NumberFormatter({
description,
export default function createD3TimeFormatter(config: {
description?: string;
formatString: string;
label?: string;
locale?: TimeLocaleDefinition;
useLocalTime?: boolean;
}) {
const {
description,
formatString = isRequired('formatString'),
label,
locale,
useLocalTime = false,
} = config;
const id = useLocalTime ? `${LOCAL_PREFIX}${formatString}` : formatString;
let formatFunc;
if (typeof locale === 'undefined') {
const format = useLocalTime ? timeFormat : utcFormat;
formatFunc = format(formatString);
} else {
const localeObject = timeFormatLocale(locale);
formatFunc = useLocalTime
? localeObject.format(formatString)
: localeObject.utcFormat(formatString);
register() {
const { key = isRequired('config.key') } = this.config;
getChartMetadataRegistry().registerValue(key, this.metadata);
getChartBuildQueryRegistry().registerLoader(key, this.loadBuildQuery);
getChartComponentRegistry().registerLoader(key, this.loadChart);
getChartTransformPropsRegistry().registerLoader(key, this.loadTransformProps);
return this;
}