All files / format / time.ts

100.00% Branches 0/0
14.29% Lines 2/14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
x2
 
 
 
 
 
 
 
 
x2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

























import type { Step } from '../index.ts';
import { Name, Pipe } from './function.ts';

export function timestamp (d = new Date()) {
  return d.toISOString()
    .replace(/[-:\.Z]/g, '')
    .replace(/[T]/g, '_')
    .slice(0, -3)
}

export const msec = (t: number) => (t/1000).toFixed(3)+'s'

export const dT = (t0: number) =>
  performance.now() - t0;

/** Start time and duration. */
export type Timed = {
  /** Starting time in milliseconds. */
  t0?: number,
  /** Duration in milliseconds. */
  tD?: number,
};

export const interval = (msec: number, ...steps: Step[]) =>
  Name(null, async function interval (..._: unknown[]) {
    return setInterval(() => { Pipe(...steps)(performance.now()); }, msec);
  }, { msec });