All files / src/UUID makeVersionThreeOrFiveUUIDValues.ts

100% Statements 13/13
100% Branches 6/6
100% Functions 1/1
100% Lines 13/13
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 552x                 2x             2x 9x 1x 8x 1x     7x               7x         7x         7x         7x             2x  
import {
  getHashFromNamespaceIdAndName,
} from '../getHashFromNamespaceIdAndName';
import {
  IUUIDComponentValues,
} from './IUUIDComponentValues';
import {
  IUUIDOptions,
} from './UUIDOptions/IUUIDOptions';
import {
  strings,
} from '../strings';
import {
  UUIDVersions,
} from '../Enums/UUIDVersions';
 
export const makeVersionThreeOrFiveUUIDValues = (options: IUUIDOptions): IUUIDComponentValues => {
  if (!options || !options.namespaceId) {
    throw new Error(strings.NAMESPACE_ID_MISSING);
  } else if (!options.name) {
    throw new Error(strings.NAME_MISSING);
  }
 
  const hash = getHashFromNamespaceIdAndName(
    options.version as UUIDVersions.Three | UUIDVersions.Five,
    options.namespaceId,
    options.name,
  );
 
  /* Clock sequence is highly dependent on other values and their 
   * availability, so it should be generated first. */
  const clockSequence = options.clockSequenceGetter(
    options.version,
    hash,
  );
 
  const nodeIdentifier = options.nodeIdentifierGetter(
    options.version,
    hash,
  );
 
  const timestamp = options.timestampGetter(
    options.version,
    hash,
  );
 
  return {
    clockSequence,
    nodeIdentifier,
    timestamp,
  };
};
 
export default makeVersionThreeOrFiveUUIDValues;