Chore: fix some typings in @grafana/data (#60009)

fix some typings in @grafana/data
This commit is contained in:
Ashley Harrison
2022-12-16 09:59:00 +00:00
committed by GitHub
parent 2e3c4a7626
commit 9bbc2c4cbb
20 changed files with 139 additions and 192 deletions
+30 -94
View File
@@ -51,21 +51,18 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"], [0, 0, 0, "Unexpected any. Specify a different type.", "3"],
[0, 0, 0, "Unexpected any. Specify a different type.", "4"], [0, 0, 0, "Unexpected any. Specify a different type.", "4"],
[0, 0, 0, "Unexpected any. Specify a different type.", "5"], [0, 0, 0, "Do not use any type assertions.", "5"],
[0, 0, 0, "Unexpected any. Specify a different type.", "6"], [0, 0, 0, "Unexpected any. Specify a different type.", "6"]
[0, 0, 0, "Do not use any type assertions.", "7"],
[0, 0, 0, "Unexpected any. Specify a different type.", "8"]
], ],
"packages/grafana-data/src/dataframe/CircularDataFrame.ts:5381": [ "packages/grafana-data/src/dataframe/CircularDataFrame.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"] [0, 0, 0, "Unexpected any. Specify a different type.", "0"]
], ],
"packages/grafana-data/src/dataframe/DataFrameJSON.ts:5381": [ "packages/grafana-data/src/dataframe/DataFrameJSON.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Do not use any type assertions.", "1"], [0, 0, 0, "Do not use any type assertions.", "1"],
[0, 0, 0, "Do not use any type assertions.", "2"], [0, 0, 0, "Do not use any type assertions.", "2"],
[0, 0, 0, "Do not use any type assertions.", "3"], [0, 0, 0, "Do not use any type assertions.", "3"],
[0, 0, 0, "Do not use any type assertions.", "4"], [0, 0, 0, "Unexpected any. Specify a different type.", "4"]
[0, 0, 0, "Unexpected any. Specify a different type.", "5"]
], ],
"packages/grafana-data/src/dataframe/DataFrameView.test.ts:5381": [ "packages/grafana-data/src/dataframe/DataFrameView.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
@@ -91,23 +88,18 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"], [0, 0, 0, "Unexpected any. Specify a different type.", "3"],
[0, 0, 0, "Unexpected any. Specify a different type.", "4"], [0, 0, 0, "Unexpected any. Specify a different type.", "4"],
[0, 0, 0, "Unexpected any. Specify a different type.", "5"], [0, 0, 0, "Do not use any type assertions.", "5"],
[0, 0, 0, "Unexpected any. Specify a different type.", "6"], [0, 0, 0, "Unexpected any. Specify a different type.", "6"],
[0, 0, 0, "Do not use any type assertions.", "7"], [0, 0, 0, "Unexpected any. Specify a different type.", "7"],
[0, 0, 0, "Unexpected any. Specify a different type.", "8"], [0, 0, 0, "Unexpected any. Specify a different type.", "8"],
[0, 0, 0, "Do not use any type assertions.", "9"], [0, 0, 0, "Unexpected any. Specify a different type.", "9"],
[0, 0, 0, "Unexpected any. Specify a different type.", "10"], [0, 0, 0, "Unexpected any. Specify a different type.", "10"],
[0, 0, 0, "Unexpected any. Specify a different type.", "11"], [0, 0, 0, "Do not use any type assertions.", "11"],
[0, 0, 0, "Unexpected any. Specify a different type.", "12"], [0, 0, 0, "Unexpected any. Specify a different type.", "12"],
[0, 0, 0, "Unexpected any. Specify a different type.", "13"], [0, 0, 0, "Do not use any type assertions.", "13"],
[0, 0, 0, "Unexpected any. Specify a different type.", "14"], [0, 0, 0, "Unexpected any. Specify a different type.", "14"],
[0, 0, 0, "Unexpected any. Specify a different type.", "15"], [0, 0, 0, "Unexpected any. Specify a different type.", "15"],
[0, 0, 0, "Do not use any type assertions.", "16"], [0, 0, 0, "Do not use any type assertions.", "16"]
[0, 0, 0, "Unexpected any. Specify a different type.", "17"],
[0, 0, 0, "Do not use any type assertions.", "18"],
[0, 0, 0, "Unexpected any. Specify a different type.", "19"],
[0, 0, 0, "Unexpected any. Specify a different type.", "20"],
[0, 0, 0, "Do not use any type assertions.", "21"]
], ],
"packages/grafana-data/src/dataframe/dimensions.ts:5381": [ "packages/grafana-data/src/dataframe/dimensions.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"] [0, 0, 0, "Unexpected any. Specify a different type.", "0"]
@@ -119,10 +111,7 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "3"] [0, 0, 0, "Unexpected any. Specify a different type.", "3"]
], ],
"packages/grafana-data/src/dataframe/frameComparisons.ts:5381": [ "packages/grafana-data/src/dataframe/frameComparisons.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "0"]
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Do not use any type assertions.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"]
], ],
"packages/grafana-data/src/dataframe/processDataFrame.test.ts:5381": [ "packages/grafana-data/src/dataframe/processDataFrame.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"] [0, 0, 0, "Unexpected any. Specify a different type.", "0"]
@@ -142,21 +131,16 @@ exports[`better eslint`] = {
[0, 0, 0, "Do not use any type assertions.", "11"], [0, 0, 0, "Do not use any type assertions.", "11"],
[0, 0, 0, "Unexpected any. Specify a different type.", "12"], [0, 0, 0, "Unexpected any. Specify a different type.", "12"],
[0, 0, 0, "Unexpected any. Specify a different type.", "13"], [0, 0, 0, "Unexpected any. Specify a different type.", "13"],
[0, 0, 0, "Unexpected any. Specify a different type.", "14"], [0, 0, 0, "Do not use any type assertions.", "14"],
[0, 0, 0, "Unexpected any. Specify a different type.", "15"], [0, 0, 0, "Do not use any type assertions.", "15"],
[0, 0, 0, "Unexpected any. Specify a different type.", "16"], [0, 0, 0, "Unexpected any. Specify a different type.", "16"],
[0, 0, 0, "Unexpected any. Specify a different type.", "17"], [0, 0, 0, "Do not use any type assertions.", "17"],
[0, 0, 0, "Unexpected any. Specify a different type.", "18"], [0, 0, 0, "Unexpected any. Specify a different type.", "18"],
[0, 0, 0, "Do not use any type assertions.", "19"], [0, 0, 0, "Do not use any type assertions.", "19"],
[0, 0, 0, "Do not use any type assertions.", "20"], [0, 0, 0, "Do not use any type assertions.", "20"],
[0, 0, 0, "Unexpected any. Specify a different type.", "21"], [0, 0, 0, "Do not use any type assertions.", "21"],
[0, 0, 0, "Do not use any type assertions.", "22"], [0, 0, 0, "Unexpected any. Specify a different type.", "22"],
[0, 0, 0, "Unexpected any. Specify a different type.", "23"], [0, 0, 0, "Unexpected any. Specify a different type.", "23"]
[0, 0, 0, "Do not use any type assertions.", "24"],
[0, 0, 0, "Do not use any type assertions.", "25"],
[0, 0, 0, "Do not use any type assertions.", "26"],
[0, 0, 0, "Unexpected any. Specify a different type.", "27"],
[0, 0, 0, "Unexpected any. Specify a different type.", "28"]
], ],
"packages/grafana-data/src/datetime/datemath.ts:5381": [ "packages/grafana-data/src/datetime/datemath.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
@@ -196,13 +180,9 @@ exports[`better eslint`] = {
], ],
"packages/grafana-data/src/datetime/rangeutil.ts:5381": [ "packages/grafana-data/src/datetime/rangeutil.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Do not use any type assertions.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Do not use any type assertions.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"], [0, 0, 0, "Unexpected any. Specify a different type.", "3"]
[0, 0, 0, "Do not use any type assertions.", "4"],
[0, 0, 0, "Do not use any type assertions.", "5"],
[0, 0, 0, "Do not use any type assertions.", "6"],
[0, 0, 0, "Unexpected any. Specify a different type.", "7"]
], ],
"packages/grafana-data/src/datetime/timezones.ts:5381": [ "packages/grafana-data/src/datetime/timezones.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "0"],
@@ -233,37 +213,17 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "4"] [0, 0, 0, "Unexpected any. Specify a different type.", "4"]
], ],
"packages/grafana-data/src/field/displayProcessor.test.ts:5381": [ "packages/grafana-data/src/field/displayProcessor.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"]
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"]
], ],
"packages/grafana-data/src/field/displayProcessor.ts:5381": [ "packages/grafana-data/src/field/displayProcessor.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Do not use any type assertions.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Do not use any type assertions.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"], [0, 0, 0, "Unexpected any. Specify a different type.", "3"]
[0, 0, 0, "Do not use any type assertions.", "4"],
[0, 0, 0, "Do not use any type assertions.", "5"],
[0, 0, 0, "Unexpected any. Specify a different type.", "6"]
],
"packages/grafana-data/src/field/fieldColor.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"packages/grafana-data/src/field/fieldComparers.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"],
[0, 0, 0, "Unexpected any. Specify a different type.", "4"],
[0, 0, 0, "Unexpected any. Specify a different type.", "5"],
[0, 0, 0, "Unexpected any. Specify a different type.", "6"],
[0, 0, 0, "Unexpected any. Specify a different type.", "7"]
], ],
"packages/grafana-data/src/field/fieldDisplay.test.ts:5381": [ "packages/grafana-data/src/field/fieldDisplay.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"]
[0, 0, 0, "Unexpected any. Specify a different type.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"]
], ],
"packages/grafana-data/src/field/fieldOverrides.test.ts:5381": [ "packages/grafana-data/src/field/fieldOverrides.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
@@ -309,10 +269,6 @@ exports[`better eslint`] = {
[0, 0, 0, "Do not use any type assertions.", "4"], [0, 0, 0, "Do not use any type assertions.", "4"],
[0, 0, 0, "Unexpected any. Specify a different type.", "5"] [0, 0, 0, "Unexpected any. Specify a different type.", "5"]
], ],
"packages/grafana-data/src/field/getFieldDisplayValuesProxy.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]
],
"packages/grafana-data/src/field/overrides/processors.ts:5381": [ "packages/grafana-data/src/field/overrides/processors.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"],
@@ -348,10 +304,7 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "7"] [0, 0, 0, "Unexpected any. Specify a different type.", "7"]
], ],
"packages/grafana-data/src/field/templateProxies.ts:5381": [ "packages/grafana-data/src/field/templateProxies.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"]
[0, 0, 0, "Do not use any type assertions.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"]
], ],
"packages/grafana-data/src/geo/layer.ts:5381": [ "packages/grafana-data/src/geo/layer.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
@@ -374,16 +327,8 @@ exports[`better eslint`] = {
], ],
"packages/grafana-data/src/panel/registryFactories.ts:5381": [ "packages/grafana-data/src/panel/registryFactories.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Do not use any type assertions.", "1"],
[0, 0, 0, "Do not use any type assertions.", "2"], [0, 0, 0, "Do not use any type assertions.", "2"]
[0, 0, 0, "Unexpected any. Specify a different type.", "3"],
[0, 0, 0, "Do not use any type assertions.", "4"]
],
"packages/grafana-data/src/text/sanitize.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"]
],
"packages/grafana-data/src/text/string.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"]
], ],
"packages/grafana-data/src/text/text.ts:5381": [ "packages/grafana-data/src/text/text.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"] [0, 0, 0, "Do not use any type assertions.", "0"]
@@ -391,22 +336,13 @@ exports[`better eslint`] = {
"packages/grafana-data/src/themes/colorManipulator.ts:5381": [ "packages/grafana-data/src/themes/colorManipulator.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"], [0, 0, 0, "Unexpected any. Specify a different type.", "2"]
[0, 0, 0, "Unexpected any. Specify a different type.", "3"]
], ],
"packages/grafana-data/src/themes/createColors.ts:5381": [ "packages/grafana-data/src/themes/createColors.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"] [0, 0, 0, "Do not use any type assertions.", "0"]
], ],
"packages/grafana-data/src/transformations/fieldReducer.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"packages/grafana-data/src/transformations/fieldReducer.ts:5381": [ "packages/grafana-data/src/transformations/fieldReducer.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"]
[0, 0, 0, "Do not use any type assertions.", "1"],
[0, 0, 0, "Do not use any type assertions.", "2"],
[0, 0, 0, "Do not use any type assertions.", "3"],
[0, 0, 0, "Unexpected any. Specify a different type.", "4"],
[0, 0, 0, "Unexpected any. Specify a different type.", "5"]
], ],
"packages/grafana-data/src/transformations/matchers/predicates.ts:5381": [ "packages/grafana-data/src/transformations/matchers/predicates.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "0"],
@@ -6,7 +6,7 @@ import { vectorToArray } from '../vector/vectorToArray';
import { guessFieldTypeFromNameAndValue, toDataFrameDTO } from './processDataFrame'; import { guessFieldTypeFromNameAndValue, toDataFrameDTO } from './processDataFrame';
/** @public */ /** @public */
export type ValueConverter<T = any> = (val: any) => T; export type ValueConverter<T = any> = (val: unknown) => T;
const NOOP: ValueConverter = (v) => v; const NOOP: ValueConverter = (v) => v;
@@ -67,7 +67,7 @@ export class ArrayDataFrame<T = any> extends FunctionalVector<T> implements Data
/** /**
* Add a field for each property in the object. This will guess the type * Add a field for each property in the object. This will guess the type
*/ */
setFieldsFromObject(obj: any) { setFieldsFromObject(obj: Record<string, unknown>) {
this.fields = Object.keys(obj).map((name) => { this.fields = Object.keys(obj).map((name) => {
return { return {
name, name,
@@ -111,7 +111,7 @@ export interface FieldValueEntityLookup {
NegInf?: number[]; NegInf?: number[];
} }
const ENTITY_MAP: Record<keyof FieldValueEntityLookup, any> = { const ENTITY_MAP: Record<keyof FieldValueEntityLookup, number | undefined> = {
Inf: Infinity, Inf: Infinity,
NegInf: -Infinity, NegInf: -Infinity,
Undef: undefined, Undef: undefined,
@@ -13,7 +13,7 @@ export type MutableField<T = any> = Field<T, MutableVector<T>>;
type MutableVectorCreator = (buffer?: any[]) => MutableVector; type MutableVectorCreator = (buffer?: any[]) => MutableVector;
export const MISSING_VALUE: any = undefined; // Treated as connected in new graph panel export const MISSING_VALUE = undefined; // Treated as connected in new graph panel
export class MutableDataFrame<T = any> extends FunctionalVector<T> implements DataFrame, MutableVector<T> { export class MutableDataFrame<T = any> extends FunctionalVector<T> implements DataFrame, MutableVector<T> {
name?: string; name?: string;
@@ -67,7 +67,7 @@ export class MutableDataFrame<T = any> extends FunctionalVector<T> implements Da
return this.first.length; return this.first.length;
} }
addFieldFor(value: any, name?: string): MutableField { addFieldFor(value: unknown, name?: string): MutableField {
return this.addField({ return this.addField({
name: name || '', // Will be filled in name: name || '', // Will be filled in
type: guessFieldTypeFromValue(value), type: guessFieldTypeFromValue(value),
@@ -79,7 +79,7 @@ export class MutableDataFrame<T = any> extends FunctionalVector<T> implements Da
if (f.values) { if (f.values) {
if (Array.isArray(f.values)) { if (Array.isArray(f.values)) {
buffer = f.values as any[]; buffer = f.values;
} else { } else {
buffer = (f.values as Vector).toArray(); buffer = (f.values as Vector).toArray();
} }
@@ -181,7 +181,7 @@ export class MutableDataFrame<T = any> extends FunctionalVector<T> implements Da
/** /**
* This will add each value to the corresponding column * This will add each value to the corresponding column
*/ */
appendRow(row: any[]) { appendRow(row: unknown[]) {
// Add any extra columns // Add any extra columns
for (let i = this.fields.length; i < row.length; i++) { for (let i = this.fields.length; i < row.length; i++) {
this.addField({ this.addField({
@@ -233,7 +233,7 @@ export class MutableDataFrame<T = any> extends FunctionalVector<T> implements Da
set(index: number, value: T) { set(index: number, value: T) {
if (index > this.length) { if (index > this.length) {
throw new Error('Unable ot set value beyond current length'); throw new Error('Unable to set value beyond current length');
} }
const obj = (value as any) || {}; const obj = (value as any) || {};
@@ -43,11 +43,14 @@ export function compareDataFrameStructures(a: DataFrame, b: DataFrame, skipConfi
return false; return false;
} }
const cfgA = fA.config as any; const cfgA = fA.config;
const cfgB = fB.config as any; const cfgB = fB.config;
let aKeys = Object.keys(cfgA); // need to type assert on the object keys here
let bKeys = Object.keys(cfgB); // see e.g. https://github.com/Microsoft/TypeScript/issues/12870
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
let aKeys = Object.keys(cfgA) as Array<keyof typeof cfgA>;
let bKeys = Object.keys(cfgB) as Array<keyof typeof cfgB>;
if (aKeys.length !== bKeys.length) { if (aKeys.length !== bKeys.length) {
return false; return false;
@@ -180,7 +180,7 @@ const NUMBER = /^\s*(-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?|NAN)\s*$/i;
/** /**
* Given a name and value, this will pick a reasonable field type * Given a name and value, this will pick a reasonable field type
*/ */
export function guessFieldTypeFromNameAndValue(name: string, v: any): FieldType { export function guessFieldTypeFromNameAndValue(name: string, v: unknown): FieldType {
if (name) { if (name) {
name = name.toLowerCase(); name = name.toLowerCase();
if (name === 'date' || name === 'time') { if (name === 'date' || name === 'time') {
@@ -193,7 +193,7 @@ export function guessFieldTypeFromNameAndValue(name: string, v: any): FieldType
/** /**
* Check the field type to see what the contents are * Check the field type to see what the contents are
*/ */
export function getFieldTypeFromValue(v: any): FieldType { export function getFieldTypeFromValue(v: unknown): FieldType {
if (v instanceof Date || isDateTime(v)) { if (v instanceof Date || isDateTime(v)) {
return FieldType.time; return FieldType.time;
} }
@@ -218,7 +218,7 @@ export function getFieldTypeFromValue(v: any): FieldType {
* *
* NOTE: this is will try to see if string values can be mapped to other types (like number) * NOTE: this is will try to see if string values can be mapped to other types (like number)
*/ */
export function guessFieldTypeFromValue(v: any): FieldType { export function guessFieldTypeFromValue(v: unknown): FieldType {
if (v instanceof Date || isDateTime(v)) { if (v instanceof Date || isDateTime(v)) {
return FieldType.time; return FieldType.time;
} }
@@ -299,9 +299,9 @@ export const guessFieldTypes = (series: DataFrame, guessDefined = false): DataFr
return series; return series;
}; };
export const isTableData = (data: any): data is DataFrame => data && data.hasOwnProperty('columns'); export const isTableData = (data: unknown): data is DataFrame => Boolean(data && data.hasOwnProperty('columns'));
export const isDataFrame = (data: any): data is DataFrame => data && data.hasOwnProperty('fields'); export const isDataFrame = (data: unknown): data is DataFrame => Boolean(data && data.hasOwnProperty('fields'));
/** /**
* Inspect any object and return the results as a DataFrame * Inspect any object and return the results as a DataFrame
@@ -86,11 +86,11 @@ const hiddenRangeOptions: TimeOption[] = [
{ from: 'now', to: 'now+5y', display: 'Next 5 years' }, { from: 'now', to: 'now+5y', display: 'Next 5 years' },
]; ];
const rangeIndex: any = {}; const rangeIndex: Record<string, any> = {};
each(rangeOptions, (frame: any) => { each(rangeOptions, (frame) => {
rangeIndex[frame.from + ' to ' + frame.to] = frame; rangeIndex[frame.from + ' to ' + frame.to] = frame;
}); });
each(hiddenRangeOptions, (frame: any) => { each(hiddenRangeOptions, (frame) => {
rangeIndex[frame.from + ' to ' + frame.to] = frame; rangeIndex[frame.from + ' to ' + frame.to] = frame;
}); });
@@ -100,7 +100,7 @@ each(hiddenRangeOptions, (frame: any) => {
// now/d to now // now/d to now
// now/d // now/d
// if no to <expr> then to now is assumed // if no to <expr> then to now is assumed
export function describeTextRange(expr: any) { export function describeTextRange(expr: string) {
const isLast = expr.indexOf('+') !== 0; const isLast = expr.indexOf('+') !== 0;
if (expr.indexOf('now') === -1) { if (expr.indexOf('now') === -1) {
expr = (isLast ? 'now-' : 'now') + expr; expr = (isLast ? 'now-' : 'now') + expr;
@@ -211,7 +211,7 @@ export const convertRawToRange = (raw: RawTimeRange, timeZone?: TimeZone, fiscal
function isRelativeTime(v: DateTime | string) { function isRelativeTime(v: DateTime | string) {
if (typeof v === 'string') { if (typeof v === 'string') {
return (v as string).indexOf('now') >= 0; return v.indexOf('now') >= 0;
} }
return false; return false;
} }
@@ -21,7 +21,10 @@ function assertSame(input: any, processors: DisplayProcessor[], match: DisplayVa
processors.forEach((processor) => { processors.forEach((processor) => {
const value = processor(input); const value = processor(input);
for (const key of Object.keys(match)) { for (const key of Object.keys(match)) {
expect((value as any)[key]).toEqual((match as any)[key]); // need to type assert on the object keys here
// see e.g. https://github.com/Microsoft/TypeScript/issues/12870
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
expect(value[key as keyof typeof match]).toEqual(match[key as keyof typeof match]);
} }
}); });
} }
@@ -82,7 +82,7 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
const formatFunc = getValueFormat(unit || 'none'); const formatFunc = getValueFormat(unit || 'none');
const scaleFunc = getScaleCalculator(field, options.theme); const scaleFunc = getScaleCalculator(field, options.theme);
return (value: any, adjacentDecimals?: DecimalCount) => { return (value: unknown, adjacentDecimals?: DecimalCount) => {
const { mappings } = config; const { mappings } = config;
const isStringUnit = unit === 'string'; const isStringUnit = unit === 'string';
@@ -186,12 +186,12 @@ export function getDisplayProcessor(options?: DisplayProcessorOptions): DisplayP
}; };
} }
function toStringProcessor(value: any): DisplayValue { function toStringProcessor(value: unknown): DisplayValue {
return { text: toString(value), numeric: anyToNumber(value) }; return { text: toString(value), numeric: anyToNumber(value) };
} }
export function getRawDisplayProcessor(): DisplayProcessor { export function getRawDisplayProcessor(): DisplayProcessor {
return (value: any) => ({ return (value: unknown) => ({
text: getFieldTypeFromValue(value) === 'other' ? `${JSON.stringify(value, getCircularReplacer())}` : `${value}`, text: getFieldTypeFromValue(value) === 'other' ? `${JSON.stringify(value, getCircularReplacer())}` : `${value}`,
numeric: null as unknown as number, numeric: null as unknown as number,
}); });
@@ -12,7 +12,7 @@ function getTestField(mode: string): Field {
config: { config: {
color: { color: {
mode: mode, mode: mode,
} as any, },
}, },
state: {}, state: {},
}; };
@@ -1,6 +1,6 @@
import { isNumber } from 'lodash'; import { isNumber } from 'lodash';
import { dateTime } from '../datetime'; import { dateTime, isDateTime } from '../datetime';
import { Field, FieldType } from '../types/dataFrame'; import { Field, FieldType } from '../types/dataFrame';
import { Vector } from '../types/vector'; import { Vector } from '../types/vector';
@@ -25,7 +25,7 @@ export const fieldIndexComparer = (field: Field, reverse = false): IndexComparer
}; };
/** @public */ /** @public */
export const timeComparer = (a: any, b: any): number => { export const timeComparer = (a: unknown, b: unknown): number => {
if (!a || !b) { if (!a || !b) {
return falsyComparer(a, b); return falsyComparer(a, b);
} }
@@ -34,12 +34,14 @@ export const timeComparer = (a: any, b: any): number => {
return numericComparer(a, b); return numericComparer(a, b);
} }
if (dateTime(a).isBefore(b)) { if (isDateTime(a) && isDateTime(b)) {
return -1; if (dateTime(a).isBefore(b)) {
} return -1;
}
if (dateTime(b).isBefore(a)) { if (dateTime(b).isBefore(a)) {
return 1; return 1;
}
} }
return 0; return 0;
@@ -62,7 +64,7 @@ export const booleanComparer = (a: boolean, b: boolean): number => {
return falsyComparer(a, b); return falsyComparer(a, b);
}; };
const falsyComparer = (a: any, b: any): number => { const falsyComparer = (a: unknown, b: unknown): number => {
if (!a && b) { if (!a && b) {
return 1; return 1;
} }
@@ -74,7 +76,7 @@ const falsyComparer = (a: any, b: any): number => {
return 0; return 0;
}; };
const timeIndexComparer = (values: Vector<any>, reverse: boolean): IndexComparer => { const timeIndexComparer = (values: Vector<unknown>, reverse: boolean): IndexComparer => {
return (a: number, b: number): number => { return (a: number, b: number): number => {
const vA = values.get(a); const vA = values.get(a);
const vB = values.get(b); const vB = values.get(b);
@@ -82,26 +84,26 @@ const timeIndexComparer = (values: Vector<any>, reverse: boolean): IndexComparer
}; };
}; };
const booleanIndexComparer = (values: Vector<any>, reverse: boolean): IndexComparer => { const booleanIndexComparer = (values: Vector<boolean>, reverse: boolean): IndexComparer => {
return (a: number, b: number): number => { return (a: number, b: number): number => {
const vA: boolean = values.get(a); const vA = values.get(a);
const vB: boolean = values.get(b); const vB = values.get(b);
return reverse ? booleanComparer(vB, vA) : booleanComparer(vA, vB); return reverse ? booleanComparer(vB, vA) : booleanComparer(vA, vB);
}; };
}; };
const numericIndexComparer = (values: Vector<any>, reverse: boolean): IndexComparer => { const numericIndexComparer = (values: Vector<number>, reverse: boolean): IndexComparer => {
return (a: number, b: number): number => { return (a: number, b: number): number => {
const vA: number = values.get(a); const vA = values.get(a);
const vB: number = values.get(b); const vB = values.get(b);
return reverse ? numericComparer(vB, vA) : numericComparer(vA, vB); return reverse ? numericComparer(vB, vA) : numericComparer(vA, vB);
}; };
}; };
const stringIndexComparer = (values: Vector<any>, reverse: boolean): IndexComparer => { const stringIndexComparer = (values: Vector<string>, reverse: boolean): IndexComparer => {
return (a: number, b: number): number => { return (a: number, b: number): number => {
const vA: string = values.get(a); const vA = values.get(a);
const vB: string = values.get(b); const vB = values.get(b);
return reverse ? stringComparer(vB, vA) : stringComparer(vA, vB); return reverse ? stringComparer(vB, vA) : stringComparer(vA, vB);
}; };
}; };
@@ -321,7 +321,7 @@ describe('FieldDisplay', () => {
}); });
const cache = { numeric: 10, text: 'Value' }; const cache = { numeric: 10, text: 'Value' };
options.data![0].fields[1].display = (v: any) => { options.data![0].fields[1].display = () => {
return cache; return cache;
}; };
@@ -528,5 +528,5 @@ function createDisplayOptions(extend: Partial<GetFieldDisplayValuesOptions> = {}
theme: createTheme(), theme: createTheme(),
}; };
return merge<GetFieldDisplayValuesOptions, any>(options, extend); return merge(options, extend);
} }
@@ -19,38 +19,41 @@ export function getFieldDisplayValuesProxy(options: {
rowIndex: number; rowIndex: number;
timeZone?: TimeZone; timeZone?: TimeZone;
}): Record<string, DisplayValue> { }): Record<string, DisplayValue> {
return new Proxy({} as Record<string, DisplayValue>, { return new Proxy(
get: (obj: any, key: string): DisplayValue | undefined => { {},
// 1. Match the name {
let field = options.frame.fields.find((f) => key === f.name); get: (obj, key): DisplayValue | undefined => {
if (!field) { // 1. Match the name
// 2. Match the array index let field = options.frame.fields.find((f) => key === f.name);
const k = toNumber(key); if (!field) {
field = options.frame.fields[k]; // 2. Match the array index
} const k = toNumber(key);
if (!field) { field = options.frame.fields[k];
// 3. Match the config displayName }
field = options.frame.fields.find((f) => key === f.config.displayName); if (!field) {
} // 3. Match the config displayName
if (!field) { field = options.frame.fields.find((f) => key === f.config.displayName);
// 4. Match the name label }
field = options.frame.fields.find((f) => { if (!field) {
if (f.labels) { // 4. Match the name label
return key === f.labels.name; field = options.frame.fields.find((f) => {
} if (f.labels) {
return false; return key === f.labels.name;
}); }
} return false;
if (!field) { });
return undefined; }
} if (!field) {
// TODO: we could supply the field here for the getDisplayProcessor fallback but we would also need theme which return undefined;
// we do not have access to here }
const displayProcessor = field.display ?? getDisplayProcessor(); // TODO: we could supply the field here for the getDisplayProcessor fallback but we would also need theme which
const raw = field.values.get(options.rowIndex); // we do not have access to here
const disp = displayProcessor(raw); const displayProcessor = field.display ?? getDisplayProcessor();
disp.toString = () => formattedValueToString(disp); const raw = field.values.get(options.rowIndex);
return disp; const disp = displayProcessor(raw);
}, disp.toString = () => formattedValueToString(disp);
}); return disp;
},
}
);
} }
@@ -9,9 +9,9 @@ import { getFieldDisplayName } from './fieldState';
*/ */
export function getTemplateProxyForField(field: Field, frame?: DataFrame, frames?: DataFrame[]): any { export function getTemplateProxyForField(field: Field, frame?: DataFrame, frames?: DataFrame[]): any {
return new Proxy( return new Proxy(
{} as any, // This object shows up in test snapshots {}, // This object shows up in test snapshots
{ {
get: (obj: Field, key: string, reciever: any) => { get: (obj, key) => {
if (key === 'name') { if (key === 'name') {
return field.name; return field.name;
} }
@@ -33,7 +33,7 @@ export function getTemplateProxyForField(field: Field, frame?: DataFrame, frames
}, },
}; };
} }
return undefined; // (field as any)[key]; // any property? return undefined;
}, },
} }
); );
@@ -50,8 +50,8 @@ export function createFieldConfigRegistry<TFieldConfigOptions>(
} }
} }
if (config.standardOptions) { if (config.standardOptions) {
const customDefault: any = config.standardOptions[fieldConfigProp.id as FieldConfigProperty]?.defaultValue; const customDefault = config.standardOptions[fieldConfigProp.id as FieldConfigProperty]?.defaultValue;
const customSettings: any = config.standardOptions[fieldConfigProp.id as FieldConfigProperty]?.settings; const customSettings = config.standardOptions[fieldConfigProp.id as FieldConfigProperty]?.settings;
if (customDefault) { if (customDefault) {
fieldConfigProp = { fieldConfigProp = {
...fieldConfigProp, ...fieldConfigProp,
+2 -2
View File
@@ -1,10 +1,10 @@
import { sanitizeUrl as braintreeSanitizeUrl } from '@braintree/sanitize-url'; import { sanitizeUrl as braintreeSanitizeUrl } from '@braintree/sanitize-url';
import * as xss from 'xss'; import * as xss from 'xss';
const XSSWL = Object.keys(xss.whiteList).reduce((acc, element) => { const XSSWL = Object.keys(xss.whiteList).reduce<xss.IWhiteList>((acc, element) => {
acc[element] = xss.whiteList[element]?.concat(['class', 'style']); acc[element] = xss.whiteList[element]?.concat(['class', 'style']);
return acc; return acc;
}, {} as xss.IWhiteList); }, {});
const sanitizeXSS = new xss.FilterXSS({ const sanitizeXSS = new xss.FilterXSS({
whiteList: XSSWL, whiteList: XSSWL,
+1 -1
View File
@@ -73,7 +73,7 @@ export function stringToMs(str: string): number {
} }
export function toNumberString(value: number | undefined | null): string { export function toNumberString(value: number | undefined | null): string {
if (value !== null && value !== undefined && Number.isFinite(value as number)) { if (value !== null && value !== undefined && Number.isFinite(value)) {
return value.toString(); return value.toString();
} }
return ''; return '';
@@ -177,7 +177,7 @@ export function decomposeColor(color: string | DecomposeColor): DecomposeColor {
*/ */
export function recomposeColor(color: DecomposeColor) { export function recomposeColor(color: DecomposeColor) {
const { type, colorSpace } = color; const { type, colorSpace } = color;
let values: any = color.values; let values = color.values;
if (type.indexOf('rgb') !== -1) { if (type.indexOf('rgb') !== -1) {
// Only convert the first 3 values to int (i.e. not alpha) // Only convert the first 3 values to int (i.e. not alpha)
@@ -10,7 +10,7 @@ import { fieldReducers, ReducerID, reduceField } from './fieldReducer';
/** /**
* Run a reducer and get back the value * Run a reducer and get back the value
*/ */
function reduce(field: Field, id: string): any { function reduce(field: Field, id: string) {
return reduceField({ field, reducers: [id] })[id]; return reduceField({ field, reducers: [id] })[id];
} }
@@ -1,7 +1,7 @@
// Libraries // Libraries
import { isNumber } from 'lodash'; import { isNumber } from 'lodash';
import { NullValueMode, Field, FieldState, FieldCalcs, FieldType } from '../types/index'; import { NullValueMode, Field, FieldCalcs, FieldType } from '../types/index';
import { Registry, RegistryItem } from '../utils/Registry'; import { Registry, RegistryItem } from '../utils/Registry';
export enum ReducerID { export enum ReducerID {
@@ -72,7 +72,7 @@ export function reduceField(options: ReduceFieldOptions): FieldCalcs {
} }
} }
if (!field.state) { if (!field.state) {
field.state = {} as FieldState; field.state = {};
} }
const queue = fieldReducers.list(reducers); const queue = fieldReducers.list(reducers);
@@ -81,7 +81,7 @@ export function reduceField(options: ReduceFieldOptions): FieldCalcs {
// This lets the concrete implementations assume at least one row // This lets the concrete implementations assume at least one row
const data = field.values; const data = field.values;
if (data.length < 1) { if (data.length < 1) {
const calcs = { ...field.state.calcs } as FieldCalcs; const calcs: FieldCalcs = { ...field.state.calcs };
for (const reducer of queue) { for (const reducer of queue) {
calcs[reducer.id] = reducer.emptyInputResult !== null ? reducer.emptyInputResult : null; calcs[reducer.id] = reducer.emptyInputResult !== null ? reducer.emptyInputResult : null;
} }
@@ -266,7 +266,7 @@ export const fieldReducers = new Registry<FieldReducerInfo>(() => [
]); ]);
export function doStandardCalcs(field: Field, ignoreNulls: boolean, nullAsZero: boolean): FieldCalcs { export function doStandardCalcs(field: Field, ignoreNulls: boolean, nullAsZero: boolean): FieldCalcs {
const calcs = { const calcs: FieldCalcs = {
sum: 0, sum: 0,
max: -Number.MAX_VALUE, max: -Number.MAX_VALUE,
min: Number.MAX_VALUE, min: Number.MAX_VALUE,
@@ -288,7 +288,7 @@ export function doStandardCalcs(field: Field, ignoreNulls: boolean, nullAsZero:
// Just used for calculations -- not exposed as a stat // Just used for calculations -- not exposed as a stat
previousDeltaUp: true, previousDeltaUp: true,
} as FieldCalcs; };
const data = field.values; const data = field.values;
calcs.count = ignoreNulls ? data.length : data.toArray().filter((val) => val != null).length; calcs.count = ignoreNulls ? data.length : data.toArray().filter((val) => val != null).length;
@@ -466,7 +466,7 @@ function calculateChangeCount(field: Field, ignoreNulls: boolean, nullAsZero: bo
const data = field.values; const data = field.values;
let count = 0; let count = 0;
let first = true; let first = true;
let last: any = null; let last = null;
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
let currentValue = data.get(i); let currentValue = data.get(i);
if (currentValue === null) { if (currentValue === null) {
@@ -489,7 +489,7 @@ function calculateChangeCount(field: Field, ignoreNulls: boolean, nullAsZero: bo
function calculateDistinctCount(field: Field, ignoreNulls: boolean, nullAsZero: boolean): FieldCalcs { function calculateDistinctCount(field: Field, ignoreNulls: boolean, nullAsZero: boolean): FieldCalcs {
const data = field.values; const data = field.values;
const distinct = new Set<any>(); const distinct = new Set();
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
let currentValue = data.get(i); let currentValue = data.get(i);
if (currentValue === null) { if (currentValue === null) {