timm.ts 20 KB
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
/* eslint-disable @typescript-eslint/ban-types */

/*!
 * Timm
 *
 * Immutability helpers with fast reads and acceptable writes.
 *
 * @copyright Guillermo Grau Panea 2016
 * @license MIT
 */

const INVALID_ARGS = 'INVALID_ARGS';
const IS_DEV = (process as any).env.NODE_ENV !== 'production';

type Key = string | number;

// ===============================================
// ### Helpers
// ===============================================
function throwStr(msg: string): never {
  throw new Error(msg);
}

function getKeysAndSymbols(obj: object): string[] {
  const keys = Object.keys(obj);
  if (Object.getOwnPropertySymbols) {
    // @ts-ignore
    return keys.concat(Object.getOwnPropertySymbols(obj));
  }
  return keys;
}

const hasOwnProperty = {}.hasOwnProperty;

export function clone<T extends object>(obj0: T): T {
  // As array
  if (Array.isArray(obj0)) return obj0.slice() as T;

  // As object
  const obj: any = obj0;
  const keys = getKeysAndSymbols(obj);
  const out: any = {};
  for (let i = 0; i < keys.length; i++) {
    const key = keys[i];
    out[key] = obj[key];
  }
  // @ts-ignore (see type tests)
  return out;
}

// Custom guard
function isObject(o: unknown): any {
  return o != null && typeof o === 'object';
}

// _deepFreeze = (obj) ->
//   Object.freeze obj
//   for key in Object.getOwnPropertyNames obj
//     val = obj[key]
//     if isObject(val) and not Object.isFrozen val
//       _deepFreeze val
//   obj

// ===============================================
// -- ### Arrays
// ===============================================

// -- #### addLast()
// -- Returns a new array with an appended item or items.
// --
// -- Usage: `addLast(array, val)`
// --
// -- ```js
// -- arr = ['a', 'b']
// -- arr2 = addLast(arr, 'c')
// -- // ['a', 'b', 'c']
// -- arr2 === arr
// -- // false
// -- arr3 = addLast(arr, ['c', 'd'])
// -- // ['a', 'b', 'c', 'd']
// -- ```
// `array.concat(val)` also handles the scalar case,
// but is apparently very slow
export function addLast<T>(array: T[], val: T[] | T): T[] {
  if (Array.isArray(val)) return array.concat(val);
  return array.concat([val]);
}

// -- #### addFirst()
// -- Returns a new array with a prepended item or items.
// --
// -- Usage: `addFirst(array, val)`
// --
// -- ```js
// -- arr = ['a', 'b']
// -- arr2 = addFirst(arr, 'c')
// -- // ['c', 'a', 'b']
// -- arr2 === arr
// -- // false
// -- arr3 = addFirst(arr, ['c', 'd'])
// -- // ['c', 'd', 'a', 'b']
// -- ```
export function addFirst<T>(array: T[], val: T[] | T): T[] {
  if (Array.isArray(val)) return val.concat(array);
  return [val].concat(array);
}

// -- #### removeLast()
// -- Returns a new array removing the last item.
// --
// -- Usage: `removeLast(array)`
// --
// -- ```js
// -- arr = ['a', 'b']
// -- arr2 = removeLast(arr)
// -- // ['a']
// -- arr2 === arr
// -- // false
// --
// -- // The same array is returned if there are no changes:
// -- arr3 = []
// -- removeLast(arr3) === arr3
// -- // true
// -- ```
export function removeLast<T>(array: T[]): T[] {
  if (!array.length) return array;
  return array.slice(0, array.length - 1);
}

// -- #### removeFirst()
// -- Returns a new array removing the first item.
// --
// -- Usage: `removeFirst(array)`
// --
// -- ```js
// -- arr = ['a', 'b']
// -- arr2 = removeFirst(arr)
// -- // ['b']
// -- arr2 === arr
// -- // false
// --
// -- // The same array is returned if there are no changes:
// -- arr3 = []
// -- removeFirst(arr3) === arr3
// -- // true
// -- ```
export function removeFirst<T>(array: T[]): T[] {
  if (!array.length) return array;
  return array.slice(1);
}

// -- #### insert()
// -- Returns a new array obtained by inserting an item or items
// -- at a specified index.
// --
// -- Usage: `insert(array, idx, val)`
// --
// -- ```js
// -- arr = ['a', 'b', 'c']
// -- arr2 = insert(arr, 1, 'd')
// -- // ['a', 'd', 'b', 'c']
// -- arr2 === arr
// -- // false
// -- insert(arr, 1, ['d', 'e'])
// -- // ['a', 'd', 'e', 'b', 'c']
// -- ```
export function insert<T>(array: T[], idx: number, val: T[] | T): T[] {
  return array
    .slice(0, idx)
    .concat(Array.isArray(val) ? val : [val])
    .concat(array.slice(idx));
}

// -- #### removeAt()
// -- Returns a new array obtained by removing an item at
// -- a specified index.
// --
// -- Usage: `removeAt(array, idx)`
// --
// -- ```js
// -- arr = ['a', 'b', 'c']
// -- arr2 = removeAt(arr, 1)
// -- // ['a', 'c']
// -- arr2 === arr
// -- // false
// --
// -- // The same array is returned if there are no changes:
// -- removeAt(arr, 4) === arr
// -- // true
// -- ```
export function removeAt<T>(array: T[], idx: number): T[] {
  if (idx >= array.length || idx < 0) return array;
  return array.slice(0, idx).concat(array.slice(idx + 1));
}

// -- #### replaceAt()
// -- Returns a new array obtained by replacing an item at
// -- a specified index. If the provided item is the same as
// -- (*referentially equal to*) the previous item at that position,
// -- the original array is returned.
// --
// -- Usage: `replaceAt(array, idx, newItem)`
// --
// -- ```js
// -- arr = ['a', 'b', 'c']
// -- arr2 = replaceAt(arr, 1, 'd')
// -- // ['a', 'd', 'c']
// -- arr2 === arr
// -- // false
// --
// -- // The same object is returned if there are no changes:
// -- replaceAt(arr, 1, 'b') === arr
// -- // true
// -- ```
export function replaceAt<T>(array: T[], idx: number, newItem: T): T[] {
  if (array[idx] === newItem) return array;
  const len = array.length;
  const result = Array(len);
  for (let i = 0; i < len; i++) {
    result[i] = array[i];
  }
  result[idx] = newItem;
  return result;
}

// ===============================================
// -- ### Collections (objects and arrays)
// ===============================================
// -- #### getIn()
// -- Returns a value from an object at a given path. Works with
// -- nested arrays and objects. If the path does not exist, it returns
// -- `undefined`.
// --
// -- Usage: `getIn(obj, path)`
// --
// -- ```js
// -- obj = { a: 1, b: 2, d: { d1: 3, d2: 4 }, e: ['a', 'b', 'c'] }
// -- getIn(obj, ['d', 'd1'])
// -- // 3
// -- getIn(obj, ['e', 1])
// -- // 'b'
// -- ```
export function getIn(obj: undefined, path: Key[]): undefined;
export function getIn(obj: null, path: Key[]): null;
export function getIn(obj: object, path: Key[]): unknown;
export function getIn(obj: any, path: Key[]): unknown {
  if (!Array.isArray(path)) {
    throwStr(
      IS_DEV
        ? 'A path array should be provided when calling getIn()'
        : INVALID_ARGS
    );
  }
  if (obj == null) return undefined;
  let ptr: any = obj;
  for (let i = 0; i < path.length; i++) {
    const key = path[i];
    ptr = ptr != null ? ptr[key] : undefined;
    if (ptr === undefined) return ptr;
  }
  return ptr;
}

// -- #### set()
// -- Returns a new object with a modified attribute.
// -- If the provided value is the same as (*referentially equal to*)
// -- the previous value, the original object is returned.
// --
// -- Usage: `set(obj, key, val)`
// --
// -- ```js
// -- obj = { a: 1, b: 2, c: 3 }
// -- obj2 = set(obj, 'b', 5)
// -- // { a: 1, b: 5, c: 3 }
// -- obj2 === obj
// -- // false
// --
// -- // The same object is returned if there are no changes:
// -- set(obj, 'b', 2) === obj
// -- // true
// -- ```
// When called with an undefined/null `obj`, `set()` returns either
// a single-element array, or a single-key object
export function set<K extends string, V>(
  obj: undefined | null,
  key: K,
  val: V
): { [P in K]: V };
export function set<V>(obj: undefined | null, key: number, val: V): [V];
// Normal operation with an object/array
export function set<T extends object, K extends string, V>(
  obj: T,
  key: K,
  val: V
): Omit<T, keyof { [P in K]: any }> & { [P in K]: V };
export function set<V>(obj: V[], key: number, val: V): V[];
// Implementation
export function set(obj0: any, key: Key, val: any): any {
  let obj = obj0;
  if (obj == null) obj = typeof key === 'number' ? [] : {};
  if (obj[key] === val) return obj;
  const obj2 = clone(obj);
  obj2[key] = val;
  return obj2;
}

// -- #### setIn()
// -- Returns a new object with a modified **nested** attribute.
// --
// -- Notes:
// --
// -- * If the provided value is the same as (*referentially equal to*)
// -- the previous value, the original object is returned.
// -- * If the path does not exist, it will be created before setting
// -- the new value.
// --
// -- Usage: `setIn(obj, path, val)`
// --
// -- ```js
// -- obj = { a: 1, b: 2, d: { d1: 3, d2: 4 }, e: { e1: 'foo', e2: 'bar' } }
// -- obj2 = setIn(obj, ['d', 'd1'], 4)
// -- // { a: 1, b: 2, d: { d1: 4, d2: 4 }, e: { e1: 'foo', e2: 'bar' } }
// -- obj2 === obj
// -- // false
// -- obj2.d === obj.d
// -- // false
// -- obj2.e === obj.e
// -- // true
// --
// -- // The same object is returned if there are no changes:
// -- obj3 = setIn(obj, ['d', 'd1'], 3)
// -- // { a: 1, b: 2, d: { d1: 3, d2: 4 }, e: { e1: 'foo', e2: 'bar' } }
// -- obj3 === obj
// -- // true
// -- obj3.d === obj.d
// -- // true
// -- obj3.e === obj.e
// -- // true
// --
// -- // ... unknown paths create intermediate keys. Numeric segments are treated as array indices:
// -- setIn({ a: 3 }, ['unknown', 0, 'path'], 4)
// -- // { a: 3, unknown: [{ path: 4 }] }
// -- ```
export function setIn(
  obj: object | null | undefined,
  path: Key[],
  val: any
): unknown {
  if (!path.length) return val;
  return doSetIn(obj, path, val, 0);
}

function doSetIn(
  obj: object | null | undefined,
  path: Key[],
  val: any,
  idx: number
): unknown {
  let newValue;
  const key: any = path[idx];
  if (idx === path.length - 1) {
    newValue = val;
  } else {
    const nestedObj =
      isObject(obj) && isObject((obj as any)[key])
        ? (obj as any)[key]
        : typeof path[idx + 1] === 'number'
        ? []
        : {};
    newValue = doSetIn(nestedObj, path, val, idx + 1);
  }
  return set(obj as any, key, newValue);
}

// -- #### update()
// -- Returns a new object with a modified attribute,
// -- calculated via a user-provided callback based on the current value.
// -- If the calculated value is the same as (*referentially equal to*)
// -- the previous value, the original object is returned.
// --
// -- Usage: `update(obj, key, fnUpdate)`
// --
// -- ```js
// -- obj = { a: 1, b: 2, c: 3 }
// -- obj2 = update(obj, 'b', (val) => val + 1)
// -- // { a: 1, b: 3, c: 3 }
// -- obj2 === obj
// -- // false
// --
// -- // The same object is returned if there are no changes:
// -- update(obj, 'b', (val) => val) === obj
// -- // true
// -- ```
export function update(
  obj: object | null | undefined,
  key: Key,
  fnUpdate: (prevValue: any) => any
): unknown {
  const prevVal = obj == null ? undefined : (obj as any)[key];
  const nextVal = fnUpdate(prevVal);
  return set(obj as any, key as any, nextVal);
}

// -- #### updateIn()
// -- Returns a new object with a modified **nested** attribute,
// -- calculated via a user-provided callback based on the current value.
// -- If the calculated value is the same as (*referentially equal to*)
// -- the previous value, the original object is returned.
// --
// -- Usage: `updateIn<T: ArrayOrObject>(obj: T, path: Array<Key>,
// -- fnUpdate: (prevValue: any) => any): T`
// --
// -- ```js
// -- obj = { a: 1, d: { d1: 3, d2: 4 } }
// -- obj2 = updateIn(obj, ['d', 'd1'], (val) => val + 1)
// -- // { a: 1, d: { d1: 4, d2: 4 } }
// -- obj2 === obj
// -- // false
// --
// -- // The same object is returned if there are no changes:
// -- obj3 = updateIn(obj, ['d', 'd1'], (val) => val)
// -- // { a: 1, d: { d1: 3, d2: 4 } }
// -- obj3 === obj
// -- // true
// -- ```
export function updateIn(
  obj: object | null | undefined,
  path: Key[],
  fnUpdate: (prevValue: any) => any
): unknown {
  const prevVal = getIn(obj as any, path);
  const nextVal = fnUpdate(prevVal);
  return setIn(obj, path, nextVal);
}

// -- #### merge()
// -- Returns a new object built as follows: the overlapping keys from the
// -- second one overwrite the corresponding entries from the first one.
// -- Similar to `Object.assign()`, but immutable.
// --
// -- Usage:
// --
// -- * `merge(obj1, obj2)`
// -- * `merge(obj1, ...objects)`
// --
// -- The unmodified `obj1` is returned if `obj2` does not *provide something
// -- new to* `obj1`, i.e. if either of the following
// -- conditions are true:
// --
// -- * `obj2` is `null` or `undefined`
// -- * `obj2` is an object, but it is empty
// -- * All attributes of `obj2` are `undefined`
// -- * All attributes of `obj2` are referentially equal to the
// --   corresponding attributes of `obj1`
// --
// -- Note that `undefined` attributes in `obj2` do not modify the
// -- corresponding attributes in `obj1`.
// --
// -- ```js
// -- obj1 = { a: 1, b: 2, c: 3 }
// -- obj2 = { c: 4, d: 5 }
// -- obj3 = merge(obj1, obj2)
// -- // { a: 1, b: 2, c: 4, d: 5 }
// -- obj3 === obj1
// -- // false
// --
// -- // The same object is returned if there are no changes:
// -- merge(obj1, { c: 3 }) === obj1
// -- // true
// -- ```

// Signatures:
// - 1 arg
export function merge<T extends object>(a: T): T;
// - 2 args
export function merge<T extends object>(a: T, b: undefined | null): T;
export function merge<T extends object, U extends object>(
  a: T,
  b: U
): Omit<T, keyof U> & U;
// - 3 args
export function merge<T extends object, V extends object>(
  a: T,
  b: undefined | null,
  c: V
): Omit<T, keyof V> & V;
export function merge<T extends object, U extends object>(
  a: T,
  b: U,
  c: undefined | null
): Omit<T, keyof U> & U;
export function merge<T extends object>(
  a: T,
  b: undefined | null,
  c: undefined | null
): T;
export function merge<T extends object, U extends object, V extends object>(
  a: T,
  b: U,
  c: V
): Omit<Omit<T, keyof U> & U, keyof V> & V;
// - X args
export function merge(a: object, ...rest: Array<object | null>): object;

// Implementation
export function merge(
  a: object,
  b?: object | null,
  c?: object | null,
  d?: object | null,
  e?: object | null,
  f?: object | null,
  ...rest: Array<object | null>
): object {
  return rest.length
    ? doMerge.call(null, false, false, a, b, c, d, e, f, ...rest)
    : doMerge(false, false, a, b, c, d, e, f);
}

// -- #### mergeDeep()
// -- Returns a new object built as follows: the overlapping keys from the
// -- second one overwrite the corresponding entries from the first one.
// -- If both the first and second entries are objects they are merged recursively.
// -- Similar to `Object.assign()`, but immutable, and deeply merging.
// --
// -- Usage:
// --
// -- * `mergeDeep(obj1, obj2)`
// -- * `mergeDeep(obj1, ...objects)`
// --
// -- The unmodified `obj1` is returned if `obj2` does not *provide something
// -- new to* `obj1`, i.e. if either of the following
// -- conditions are true:
// --
// -- * `obj2` is `null` or `undefined`
// -- * `obj2` is an object, but it is empty
// -- * All attributes of `obj2` are `undefined`
// -- * All attributes of `obj2` are referentially equal to the
// --   corresponding attributes of `obj1`
// --
// -- Note that `undefined` attributes in `obj2` do not modify the
// -- corresponding attributes in `obj1`.
// --
// -- ```js
// -- obj1 = { a: 1, b: 2, c: { a: 1 } }
// -- obj2 = { b: 3, c: { b: 2 } }
// -- obj3 = mergeDeep(obj1, obj2)
// -- // { a: 1, b: 3, c: { a: 1, b: 2 }  }
// -- obj3 === obj1
// -- // false
// --
// -- // The same object is returned if there are no changes:
// -- mergeDeep(obj1, { c: { a: 1 } }) === obj1
// -- // true
// -- ```
export function mergeDeep(
  a: object,
  b?: object | null,
  c?: object | null,
  d?: object | null,
  e?: object | null,
  f?: object | null,
  ...rest: Array<object | null>
): object {
  return rest.length
    ? doMerge.call(null, false, true, a, b, c, d, e, f, ...rest)
    : doMerge(false, true, a, b, c, d, e, f);
}

// -- #### mergeIn()
// -- Similar to `merge()`, but merging the value at a given nested path.
// --
// -- Usage examples:
// --
// -- * `mergeIn(obj1, path, obj2)`
// -- * `mergeIn(obj1, path, ...objects)`
// --
// -- ```js
// -- obj1 = { a: 1, d: { b: { d1: 3, d2: 4 } } }
// -- obj2 = { d3: 5 }
// -- obj3 = mergeIn(obj1, ['d', 'b'], obj2)
// -- // { a: 1, d: { b: { d1: 3, d2: 4, d3: 5 } } }
// -- obj3 === obj1
// -- // false
// --
// -- // The same object is returned if there are no changes:
// -- mergeIn(obj1, ['d', 'b'], { d2: 4 }) === obj1
// -- // true
// -- ```
export function mergeIn(
  a: any,
  path: Key[],
  b?: object | null,
  c?: object | null,
  d?: object | null,
  e?: object | null,
  f?: object | null,
  ...rest: Array<object | null>
): unknown {
  let prevVal: any = getIn(a, path);
  if (prevVal == null) prevVal = {};
  let nextVal;
  if (rest.length) {
    nextVal = doMerge.call(null, false, false, prevVal, b, c, d, e, f, ...rest);
  } else {
    nextVal = doMerge(false, false, prevVal, b, c, d, e, f);
  }
  return setIn(a, path, nextVal);
}

// -- #### omit()
// -- Returns an object excluding one or several attributes.
// --
// -- Usage: `omit(obj, attrs)`
//
// -- ```js
// -- obj = { a: 1, b: 2, c: 3, d: 4 }
// -- omit(obj, 'a')
// -- // { b: 2, c: 3, d: 4 }
// -- omit(obj, ['b', 'c'])
// -- // { a: 1, d: 4 }
// --
// -- // The same object is returned if there are no changes:
// -- omit(obj, 'z') === obj1
// -- // true
// -- ```
export function omit<T extends object, K extends string>(
  obj: T,
  attrs: K | K[]
): Omit<T, keyof { [P in K]: any }> {
  const omitList = Array.isArray(attrs) ? attrs : [attrs];
  let fDoSomething = false;
  for (let i = 0; i < omitList.length; i++) {
    if (hasOwnProperty.call(obj, omitList[i])) {
      fDoSomething = true;
      break;
    }
  }
  if (!fDoSomething) return obj;
  const out: any = {};
  const keys = getKeysAndSymbols(obj);
  for (let i = 0; i < keys.length; i++) {
    const key: any = keys[i];
    if (omitList.indexOf(key) >= 0) continue;
    out[key] = (obj as any)[key];
  }
  return out;
}

// -- #### addDefaults()
// -- Returns a new object built as follows: `undefined` keys in the first one
// -- are filled in with the corresponding values from the second one
// -- (even if they are `null`).
// --
// -- Usage:
// --
// -- * `addDefaults(obj, defaults)`
// -- * `addDefaults(obj, ...defaultObjects)`
// --
// -- ```js
// -- obj1 = { a: 1, b: 2, c: 3 }
// -- obj2 = { c: 4, d: 5, e: null }
// -- obj3 = addDefaults(obj1, obj2)
// -- // { a: 1, b: 2, c: 3, d: 5, e: null }
// -- obj3 === obj1
// -- // false
// --
// -- // The same object is returned if there are no changes:
// -- addDefaults(obj1, { c: 4 }) === obj1
// -- // true
// -- ```
// Signatures:
// - 2 args
export function addDefaults<T extends object, U extends object>(
  a: T,
  b: U
): Omit<U, keyof T> & T;
// - X args
export function addDefaults(
  a: object,
  b: object,
  ...rest: Array<object | null>
): object;

// Implementation and catch-all
export function addDefaults(
  a: object,
  b: object,
  c?: object | null,
  d?: object | null,
  e?: object | null,
  f?: object | null,
  ...rest: Array<object | null>
): object {
  return rest.length
    ? doMerge.call(null, true, false, a, b, c, d, e, f, ...rest)
    : doMerge(true, false, a, b, c, d, e, f);
}

function doMerge(
  fAddDefaults: boolean,
  fDeep: boolean,
  first: object,
  ...rest: Array<object | null | undefined>
) {
  let out: any = first;
  if (!(out != null)) {
    throwStr(
      IS_DEV
        ? 'At least one object should be provided to merge()'
        : INVALID_ARGS
    );
  }
  let fChanged = false;
  for (let idx = 0; idx < rest.length; idx++) {
    const obj = rest[idx];
    if (obj == null) continue;
    const keys = getKeysAndSymbols(obj);
    if (!keys.length) continue;
    for (let j = 0; j <= keys.length; j++) {
      const key = keys[j];
      if (fAddDefaults && out[key] !== undefined) continue;
      let nextVal = (obj as any)[key];
      if (fDeep && isObject(out[key]) && isObject(nextVal)) {
        nextVal = doMerge(fAddDefaults, fDeep, out[key], nextVal);
      }
      if (nextVal === undefined || nextVal === out[key]) continue;
      if (!fChanged) {
        fChanged = true;
        out = clone(out);
      }
      out[key] = nextVal;
    }
  }
  return out;
}

// ===============================================
// ### Public API
// ===============================================
const timm = {
  clone,
  addLast,
  addFirst,
  removeLast,
  removeFirst,
  insert,
  removeAt,
  replaceAt,

  getIn,
  set,
  setIn,
  update,
  updateIn,
  merge,
  mergeDeep,
  mergeIn,
  omit,
  addDefaults,
};

export default timm;