All files / _utils/core/components/units uuid.ts

100% Statements 28/28
100% Branches 0/0
100% Functions 9/9
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 592x   2x       2x       2x 7x         1x                         2x 6x   5x 5x     12x       1x   4x                       2x 3x 3x    
import * as P from 'maasglobal-prelude-ts';
 
import * as Tuple_ from '../../../../_private/tuple';
import { Uuid } from '../../../../_types/core/components/units';
 
export type Separator = '-';
export const separator: Separator = '-';
 
export type Group = string;
export type Groups = [Group, Group, Group, Group, Group];
export const groups = (uuid: Uuid): Groups =>
  P.pipe(
    uuid.split(separator),
    Tuple_.fromArray(5),
    P.Option_.getOrElse((): Groups => {
      // eslint-disable-next-line fp/no-throw
      throw new Error('Uuid has more than five groups');
    }),
  );
 
export type Field = string;
export type Fields = {
  timeLow: Field;
  timeMid: Field;
  timeHiAndVersion: Field;
  clockSeqHiAndReserved: Field;
  clockSeqLow: Field;
  node: Field;
};
export const fields = (uuid: Uuid): Fields =>
  P.pipe(
    groups(uuid),
    ([timeLow, timeMid, timeHiAndVersion, clockSeq, node]): Fields =>
      P.pipe(
        clockSeq.split(P.string_.empty),
        P.Array_.chunksOf(2),
        P.Array_.map((chunk) => chunk.join(P.string_.empty)),
        Tuple_.fromArray(2),
        P.Option_.getOrElse((): [string, string] => {
          // eslint-disable-next-line fp/no-throw
          throw new Error('Uuid ClockSeq hi/lo not 2x2 hex long');
        }),
        ([clockSeqHiAndReserved, clockSeqLow]) => ({
          timeLow,
          timeMid,
          timeHiAndVersion,
          clockSeqHiAndReserved,
          clockSeqLow,
          node,
        }),
      ),
  );
 
export type Version = number;
export const version = (uuid: Uuid): Version =>
  P.pipe(fields(uuid), ({ timeHiAndVersion }) =>
    parseInt(timeHiAndVersion.charAt(0), 16),
  );