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

100% Statements 34/34
100% Branches 0/0
100% Functions 12/12
100% Lines 22/22

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 482x   2x               2x 1x   2x 10x 10x     2x 1x       3x     2x 3x     2x 2x 1x 1x         1x 1x       2x 2x 2x 2x  
import * as P from 'maasglobal-prelude-ts';
 
import {
  Authority,
  defaultHostnameDelimiter as dot,
  defaultHostPortDelimiter as colon,
  Hostname,
  HostnameLabel,
} from '../../../../_types/core/components/units';
 
export const fromAuthority = (authority: Authority): Hostname =>
  P.pipe(authority.split(colon), ([hostname, _port]) => hostname as Hostname);
 
export const normalize = (h: Hostname): Hostname => {
  const space = ' ';
  return h.split(dot).join(space).trim().split(space).join(dot) as Hostname;
};
 
export const labels = (h: Hostname): P.NonEmptyArray<HostnameLabel> =>
  P.pipe(
    h,
    P.string_.split(dot),
    P.NonEmptyArray_.fromReadonlyNonEmptyArray,
    P.NonEmptyArray_.map((x): HostnameLabel => x as unknown as HostnameLabel),
  );
 
export const fromLabels = (labels: P.NonEmptyArray<HostnameLabel>): Hostname => {
  return labels.join(dot) as Hostname;
};
 
export const stripBase =
  (base: Hostname) =>
  (full: Hostname): P.Option<Hostname> => {
    return P.pipe(
      {
        b: normalize(base),
        f: normalize(full),
      },
      P.Option_.fromPredicate(({ f, b }) => f.endsWith(b)),
      P.Option_.map(({ f, b }) => normalize(f.slice(0, 0 - b.length) as Hostname)),
    );
  };
 
export const concat =
  (b: Hostname) =>
  (a: Hostname): Hostname =>
    normalize([normalize(a), normalize(b)].join(dot) as Hostname);