All files / src/UUID/UUIDOptions mergeUUIDOptions.ts

100% Statements 20/20
100% Branches 19/19
100% Functions 1/1
100% Lines 20/20
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 492x           2x       2x 10x   10x   9x 8x     9x 1x     9x 1x     9x 1x     9x     4x 2x     4x 2x         10x     2x  
import {
  isUUIDVersion,
} from '../../TypeGuards/isUUIDVersion';
import {
  IUUIDOptions,
} from './IUUIDOptions';
import {
  UUIDVersions,
} from '../../Enums/UUIDVersions';
 
export const mergeUUIDOptions = (base: IUUIDOptions, toMerge: Partial<IUUIDOptions>) => {
  const options = Object.assign({}, base);
 
  if (toMerge && typeof toMerge === 'object') {
    /* istanbul ignore else */
    if (isUUIDVersion(toMerge.version)) {
      options.version = toMerge.version;
    }
 
    if (typeof toMerge.clockSequenceGetter === 'function') {
      options.clockSequenceGetter = toMerge.clockSequenceGetter;
    }
    
    if (typeof toMerge.nodeIdentifierGetter === 'function') {
      options.nodeIdentifierGetter = toMerge.nodeIdentifierGetter;
    }
 
    if (typeof toMerge.timestampGetter === 'function') {
      options.timestampGetter = toMerge.timestampGetter;
    }
 
    if (options.version === UUIDVersions.Three ||
        options.version === UUIDVersions.Five)
    {
      if (toMerge.namespaceId) {
        options.namespaceId = toMerge.namespaceId;
      }
 
      if (toMerge.name) {
        options.name = toMerge.name;
      }
    }
  }
 
  return options;
}
 
export default mergeUUIDOptions;