import _ from "lodash"; import { isElectron } from "./electron"; export { isElectron } from "./electron"; export { GraphQLError, NotFoundError, ServerError } from "./errors"; export * from "./fetch"; export * from "./theme"; export * from "./Resource"; interface O { [key: string]: O | any; } export const toCamelCase = (obj: O): O => _.transform(obj, (acc, value, key, target) => { const camelKey = _.isArray(target) ? key : _.camelCase(key); acc[ `${typeof key === "string" && key.startsWith("_") ? "_" : ""}${camelKey}` ] = _.isObject(value) ? toCamelCase(value) : value; }); export const toSnakeCase = (obj: O): O => _.transform(obj, (acc, value, key, target) => { const snakeKey = _.isArray(target) ? key : _.snakeCase(key); acc[snakeKey] = _.isObject(value) ? toSnakeCase(value) : value; }); export const move = ( array: Array, moveIndex: number, toIndex: number ): Array => { const item = array[moveIndex]; const length = array.length; const diff = moveIndex - toIndex; if (diff > 0) { // move left return [ ...array.slice(0, toIndex), item, ...array.slice(toIndex, moveIndex), ...array.slice(moveIndex + 1, length), ]; } else if (diff < 0) { // move right const targetIndex = toIndex + 1; return [ ...array.slice(0, moveIndex), ...array.slice(moveIndex + 1, targetIndex), item, ...array.slice(targetIndex, length), ]; } return array; }; type KeyValue = { [key: string]: T; }; export const removeKeys = ( obj: KeyValue, keys: Iterable, startsWith: boolean = false ): KeyValue => { const set = new Set(keys); const values = Array.from(keys); return Object.fromEntries( Object.entries(obj).filter( startsWith ? ([key]) => values.every((k) => !key.startsWith(k)) : ([key]) => !set.has(key) ) ); }; interface BaseField { ftype: string; dbField: string | null; name: string; embeddedDocType: string | null; subfield: string | null; } export interface StrictField extends BaseField { fields?: StrictField[]; } export interface Field extends BaseField { fields: Schema; } export interface Schema { [key: string]: Field; } export interface Stage { _cls: string; kwargs: [string, object][]; } export const meetsFieldType = ( field: Field, { ftype, embeddedDocType, acceptLists = true, }: { ftype: string | string[]; embeddedDocType?: string | string[]; acceptLists?: boolean; } ) => { if (!Array.isArray(ftype)) { ftype = [ftype]; } if (!ftype.includes(EMBEDDED_DOCUMENT_FIELD) && embeddedDocType) { throw new Error("invalid parameters"); } if (!Array.isArray(embeddedDocType)) { embeddedDocType = [embeddedDocType]; } if ( ftype.some( (f) => field.ftype === f || (field.subfield === f && acceptLists) ) ) { return embeddedDocType.some((doc) => field.embeddedDocType === doc || !doc); } return false; }; export const NONFINITES = new Set(["-inf", "inf", "nan"]); export const CLASSIFICATION = "Classification"; export const CLASSIFICATIONS = "Classifications"; export const DETECTION = "Detection"; export const DETECTIONS = "Detections"; export const GEOLOCATION = "GeoLocation"; export const GEOLOCATIONS = "GeoLocations"; export const HEATMAP = "Heatmap"; export const KEYPOINT = "Keypoint"; export const KEYPOINTS = "Keypoints"; export const POLYLINE = "Polyline"; export const POLYLINES = "Polylines"; export const REGRESSION = "Regression"; export const SEGMENTATION = "Segmentation"; export const TEMPORAL_DETECTION = "TemporalDetection"; export const TEMPORAL_DETECTIONS = "TemporalDetections"; export const LABEL_LISTS_MAP = { [CLASSIFICATIONS]: "classifications", [DETECTIONS]: "detections", [KEYPOINTS]: "keypoints", [POLYLINES]: "polylines", [TEMPORAL_DETECTIONS]: "detections", }; export const LABELS_MAP = { [CLASSIFICATION]: CLASSIFICATION, [CLASSIFICATIONS]: CLASSIFICATIONS, [DETECTION]: DETECTION, [DETECTIONS]: DETECTIONS, [GEOLOCATION]: GEOLOCATION, [GEOLOCATIONS]: GEOLOCATIONS, [HEATMAP]: HEATMAP, [KEYPOINT]: KEYPOINT, [KEYPOINTS]: KEYPOINTS, [POLYLINE]: POLYLINE, [POLYLINES]: POLYLINES, [SEGMENTATION]: SEGMENTATION, [REGRESSION]: REGRESSION, [TEMPORAL_DETECTION]: TEMPORAL_DETECTION, [TEMPORAL_DETECTIONS]: TEMPORAL_DETECTIONS, }; export const MASK_LABELS = new Set([DETECTION, SEGMENTATION]); export const VALID_OBJECT_TYPES = [ DETECTION, DETECTIONS, KEYPOINT, KEYPOINTS, POLYLINE, POLYLINES, TEMPORAL_DETECTION, TEMPORAL_DETECTIONS, ]; export const VALID_CLASS_TYPES = ["Classification", "Classifications"]; export const VALID_MASK_TYPES = ["Heatmap", "Segmentation"]; export const VALID_LIST_TYPES = [ "Classifications", "Detections", "Keypoints", "Polylines", "TemporalDetections", ]; export const VALID_LABEL_TYPES = [ ...VALID_CLASS_TYPES, ...VALID_OBJECT_TYPES, ...VALID_MASK_TYPES, ]; export const LABEL_LISTS = [ "Classifications", "Detections", "Keypoints", "Polylines", "TemporalDetections", ]; export const LABEL_LIST = { Classifications: "classifications", Detections: "detections", Keypoints: "keypoints", Polylines: "polylines", TemporalDetections: "detections", }; export const LABEL_DOC_TYPES = VALID_LABEL_TYPES.filter( (label) => !LABEL_LISTS.includes(label) ); export const AGGS = { BOUNDS: "Bounds", COUNT: "Count", COUNT_VALUES: "CountValues", DISTINCT: "Distinct", }; export const BOOLEAN_FIELD = "fiftyone.core.fields.BooleanField"; export const DATE_FIELD = "fiftyone.core.fields.DateField"; export const DATE_TIME_FIELD = "fiftyone.core.fields.DateTimeField"; export const DICT_FIELD = "fiftyone.core.fields.DictField"; export const EMBEDDED_DOCUMENT_FIELD = "fiftyone.core.fields.EmbeddedDocumentField"; export const FLOAT_FIELD = "fiftyone.core.fields.FloatField"; export const FRAME_NUMBER_FIELD = "fiftyone.core.fields.FrameNumberField"; export const FRAME_SUPPORT_FIELD = "fiftyone.core.fields.FrameSupportField"; export const INT_FIELD = "fiftyone.core.fields.IntField"; export const OBJECT_ID_FIELD = "fiftyone.core.fields.ObjectIdField"; export const STRING_FIELD = "fiftyone.core.fields.StringField"; export const LIST_FIELD = "fiftyone.core.fields.ListField"; export const VALID_LIST_FIELDS = [FRAME_SUPPORT_FIELD, LIST_FIELD]; export const VALID_PRIMITIVE_TYPES = [ BOOLEAN_FIELD, DATE_FIELD, DATE_TIME_FIELD, FLOAT_FIELD, FRAME_NUMBER_FIELD, FRAME_SUPPORT_FIELD, INT_FIELD, OBJECT_ID_FIELD, STRING_FIELD, ]; export const VALID_NUMERIC_TYPES = [ DATE_FIELD, DATE_TIME_FIELD, FLOAT_FIELD, FRAME_NUMBER_FIELD, FRAME_SUPPORT_FIELD, INT_FIELD, ]; export const LABELS_PATH = "fiftyone.core.labels"; export const PATCHES_FIELDS = withPath(LABELS_PATH, [ "Detections", "Polylines", ]); export const CLIPS_SAMPLE_FIELDS = withPath(LABELS_PATH, [ "TemporalDetection", "TemporalDetections", ]); export const CLIPS_FRAME_FIELDS = withPath(LABELS_PATH, [ "Classifications", "Detections", "Keypoints", "Polylines", ]); export function withPath(path: string, types: string): string; export function withPath(path: string, types: string[]): string[]; export function withPath( path: string, types: string | string[] ): string | string[] { if (Array.isArray(types)) { return types.map((type) => [path, type].join(".")); } return [path, types].join("."); } export const LABELS = withPath(LABELS_PATH, VALID_LABEL_TYPES); export const VALID_KEYPOINTS = withPath(LABELS_PATH, [KEYPOINT, KEYPOINTS]); export const isNotebook = () => { return Boolean(new URLSearchParams(window.location.search).get("context")); }; export const useExternalLink = (href) => { let openExternal; if (isElectron()) { try { openExternal = require("electron").shell.openExternal; } catch {} } return openExternal ? (e) => { e.preventDefault(); e.stopPropagation(); openExternal(href); } : (e) => e.stopPropagation(); }; const isURL = (() => { const protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/; const localhostDomainRE = /^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/; const nonLocalhostDomainRE = /^[^\s\.]+\.\S{2,}$/; return (string) => { if (typeof string !== "string") { return false; } var match = string.match(protocolAndDomainRE); if (!match) { return false; } var everythingAfterProtocol = match[1]; if (!everythingAfterProtocol) { return false; } if ( localhostDomainRE.test(everythingAfterProtocol) || nonLocalhostDomainRE.test(everythingAfterProtocol) ) { return true; } return false; }; })(); export const prettify = ( v: boolean | string | null | undefined | number | number[] ): URL | string => { if (typeof v === "string") { if (isURL(v)) { try { return new URL(v); } catch {} } return v; } else if (typeof v === "number") { return Number(v.toFixed(3)).toLocaleString(); } else if (v === true) { return "True"; } else if (v === false) { return "False"; } else if ([undefined, null].includes(v)) { return "None"; } else if (Array.isArray(v)) { return `[${v.join(", ")}]`; } return null; }; export const formatDateTime = (timeStamp: number, timeZone: string): string => { const twoDigit = "2-digit"; const MS = 1000; const S = 60 * MS; const M = 60 * S; const H = 24 * M; const options: Intl.DateTimeFormatOptions = { timeZone, year: "numeric", day: twoDigit, month: twoDigit, hour: twoDigit, minute: twoDigit, second: twoDigit, }; if (!(timeStamp % S)) { delete options.second; } if (!(timeStamp % M)) { delete options.minute; } if (!(timeStamp % H)) { delete options.hour; } return new Intl.DateTimeFormat("en-ZA", options) .format(timeStamp) .replaceAll("/", "-"); }; export const formatDate = (timeStamp: number): string => { const twoDigit = "2-digit"; const options: Intl.DateTimeFormatOptions = { timeZone: "UTC", year: "numeric", day: twoDigit, month: twoDigit, }; return new Intl.DateTimeFormat("en-ZA", options) .format(timeStamp) .replaceAll("/", "-"); }; type Mutable = { -readonly [K in keyof T]: Mutable; }; export const clone = (data: T): Mutable => { return JSON.parse(JSON.stringify(data)); };