All files / src getHashFromNamespaceIdAndName.ts

100% Statements 14/14
100% Branches 6/6
100% Functions 1/1
100% Lines 14/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 28 29 30 31 323x           3x       3x 3x 3x   3x         5x 1x 4x 1x     3x 3x 3x     3x  
import {
  strings,
} from './strings';
import {
  TNamespaceId,
} from './TypeAliases/TNamespaceId';
import {
  UUIDVersions,
} from './Enums/UUIDVersions';
 
const MD5 = require('crypto-js/md5');
const SHA1 = require('crypto-js/sha1');
const hex = require('crypto-js/enc-hex');
 
export const getHashFromNamespaceIdAndName = (
  version: UUIDVersions.Three | UUIDVersions.Five,
  namespaceId: TNamespaceId,
  name: string,
): string => {
  if (!namespaceId) {
    throw new Error(strings.NAMESPACE_ID_MISSING);
  } else if (!name) {
    throw new Error(strings.NAME_MISSING);
  }
 
  const toHash = namespaceId + name;
  const hash = version === UUIDVersions.Three ? MD5(toHash) : SHA1(toHash);
  return hex.stringify(hash);
}
 
export default getHashFromNamespaceIdAndName;