extension/src/experiments/model/util.ts
import get from 'lodash.get'
import { formatNumber } from '../../util/number'
import { truncateFromLeft } from '../../util/string'
import { splitColumnPath } from '../columns/paths'
import { Experiment } from '../webview/contract'
import { formatDate } from '../../util/date'
type Value = undefined | null | [] | string | number
const isDate = (value: Value): boolean =>
!!(typeof value === 'string' && Date.parse(value))
const getStringifiedValue = (value: Value): string => {
if (Number.isNaN(value)) {
return 'NaN'
}
if (isDate(value)) {
return formatDate(value as string)
}
if (Array.isArray(value)) {
return `[${value?.toString()}]`
}
if (value === undefined) {
return '-'
}
if (typeof value === 'number') {
return formatNumber(value)
}
return String(value)
}
const getDataFromColumnPath = (
experiment: Experiment,
columnPath: string
): {
type: string
value: string | number
columnPath: string
splitUpPath: string[]
truncatedValue: string
} => {
const splitUpPath = splitColumnPath(columnPath)
const collectedVal = get(experiment, splitUpPath) as { value?: Value }
const value = (collectedVal?.value || collectedVal) as Value
const [type] = splitUpPath
return {
columnPath: columnPath.slice(type.length + 1) || columnPath,
splitUpPath,
truncatedValue: getStringifiedValue(value),
type,
value: typeof value === 'number' ? value : getStringifiedValue(value)
}
}
export const getDataFromColumnPaths = (
experiment: Experiment,
columnPaths: string[]
) => columnPaths.map(path => getDataFromColumnPath(experiment, path))
export const getColumnPathsQuickPickDetail = (
experiment: Experiment,
columnPaths: string[]
): string =>
getDataFromColumnPaths(experiment, columnPaths)
.map(
({ splitUpPath, truncatedValue: value }) =>
`${truncateFromLeft(splitUpPath[splitUpPath.length - 1], 15)}:${value}`
)
.join(', ')