All files / src/UUID/UUIDOptions UUIDOptions.ts

100% Statements 31/31
100% Branches 21/21
100% Functions 1/1
100% Lines 31/31
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 762x     2x           2x     2x     2x           2x       2x 18x     18x 18x 18x     18x 18x 10x 10x 1x     9x     17x 1x     17x 1x     17x 1x     17x     8x 6x   2x     6x 4x   2x           2x  
import {
  clockSequenceGetter,
} from '../../clockSequenceGetter';
import {
  isUUIDVersion,
} from '../../TypeGuards/isUUIDVersion';
import {
  IUUIDOptions,
} from './IUUIDOptions';
import {
  nodeIdentifierGetter,
} from '../../nodeIdentifierGetter';
import {
  strings,
} from '../../strings';
import {
  timestampGetter,
} from '../../timestampGetter';
import {
  TNamespaceId,
} from '../../TypeAliases/TNamespaceId';
import {
  UUIDVersions,
} from '../../Enums/UUIDVersions';
 
export class UUIDOptions implements IUUIDOptions {
  version: UUIDVersions = UUIDVersions.Four;
  namespaceId?: TNamespaceId;
  name?: string;
  clockSequenceGetter = clockSequenceGetter;
  nodeIdentifierGetter = nodeIdentifierGetter;
  timestampGetter = timestampGetter;
 
  constructor(_args: Partial<IUUIDOptions> = {}) {
    const args = _args || {};
    if (args.version) {
      let ver = args.version.toString();
      if (!isUUIDVersion(ver)) {
        throw new Error(strings.UUID_VERSION_INVALID);
      }
 
      this.version = ver;
    }
 
    if (typeof args.clockSequenceGetter === 'function') {
      this.clockSequenceGetter = args.clockSequenceGetter;
    }
 
    if (typeof args.nodeIdentifierGetter === 'function') {
      this.nodeIdentifierGetter = args.nodeIdentifierGetter; 
    }
 
    if (typeof args.timestampGetter === 'function') {
      this.timestampGetter = args.timestampGetter;
    }
 
    if (this.version === UUIDVersions.Three ||
        this.version === UUIDVersions.Five)
    {
      if (args.namespaceId) {
        this.namespaceId = args.namespaceId;
      } else {
        throw new Error(strings.NAMESPACE_ID_MISSING);
      }
 
      if (args.name) {
        this.name = args.name;
      } else {
        throw new Error(strings.NAME_MISSING);
      }
    }
  }
}
 
export default UUIDOptions;