import { isJsxValue } from "./visual-node-editor/utils"; import moment from "moment"; import React from "react"; // eslint-disable-next-line @typescript-eslint/no-unused-vars export interface OMap<_, V> { [k: string]: V | undefined; } type Entries = Array<[string, V]>; // tslint:disable-next-line:one-variable-per-declaration export const entries = (map: OMap): Entries => { return Object.keys(map).map((k) => { return [k, map[k]] as [string, V]; }); }; export const keys = (map: OMap): string[] => { return Object.keys(map); }; export const values = (map: OMap): V[] => { return Object.keys(map).map((k) => map[k] as V); }; export const toOmap = (map: Map): OMap => { return Array.from(map.entries()).reduce( (p, [k, v]) => ({ ...p, [k]: v, }), {} ); }; export const createOmap = (entr: Entries = []): OMap => { return entr.reduce((p, c) => { return { ...p, [c[0]]: c[1] }; }, {}); }; export const set = (map: OMap, k: string, v: V) => { map[k] = v; }; export const isDefined = (o: T | undefined): o is T => { return typeof o !== "undefined"; }; export type Size = { width: number; height: number }; export const toString = (v: any): string => { const type = typeof v; if (v === "") { return "(empty string)"; } if (isJsxValue(v)) { return "JSX Value"; } switch (type) { case "object": try { const str = JSON.stringify(v); return str === "{}" ? "Empty object" : str; } catch (e) { return `Object (cannot stringify)`; } default: return `${v}`; } }; export const timeAgo = (d: number) => { return moment(new Date(d)).fromNow(); }; export const timeAgoFromDt = (dt: number) => { return moment(Date.now() - dt).fromNow(); }; export const fullTime = (d: number) => { return moment(new Date(d)).toString(); }; export const isLocal = () => { return location.href.includes(":300"); // hacky way because we're forcing env for react to perform faster }; export const preventDefaultAnd = any>(fn: T) => (e: React.MouseEvent) => { e.preventDefault(); fn(e); };