object.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. const hasOwnProperty = Object.prototype.hasOwnProperty;
  8. /** Create new object if any property in "update" changes in "source". */
  9. export function shallowMerge2<T>(source: T, update: Partial<T>): T {
  10. // Adapted from LiteMol (https://github.com/dsehnal/LiteMol)
  11. let changed = false;
  12. for (let k of Object.keys(update)) {
  13. if (!hasOwnProperty.call(update, k)) continue;
  14. if ((update as any)[k] !== (source as any)[k]) {
  15. changed = true;
  16. break;
  17. }
  18. }
  19. if (!changed) return source;
  20. return Object.assign({}, source, update);
  21. }
  22. export function shallowEqual<T>(a: T, b: T) {
  23. if (!a) {
  24. if (!b) return true;
  25. return false;
  26. }
  27. if (!b) return false;
  28. let keys = Object.keys(a);
  29. if (Object.keys(b).length !== keys.length) return false;
  30. for (let k of keys) {
  31. if (!hasOwnProperty.call(a, k) || (a as any)[k] !== (b as any)[k]) return false;
  32. }
  33. return true;
  34. }
  35. export function shallowMerge<T>(source: T, ...rest: (Partial<T> | undefined)[]): T {
  36. return shallowMergeArray(source, rest);
  37. }
  38. export function shallowMergeArray<T>(source: T, rest: (Partial<T> | undefined)[]): T {
  39. // Adapted from LiteMol (https://github.com/dsehnal/LiteMol)
  40. let ret: any = source;
  41. for (let s = 0; s < rest.length; s++) {
  42. if (!rest[s]) continue;
  43. ret = shallowMerge2(source, rest[s] as T);
  44. if (ret !== source) {
  45. for (let i = s + 1; i < rest.length; i++) {
  46. ret = Object.assign(ret, rest[i]);
  47. }
  48. break;
  49. }
  50. }
  51. return ret;
  52. }
  53. /** Simple deep clone for number, boolean, string, null, undefined, object, array */
  54. export function deepClone<T>(source: T): T {
  55. if (null === source || 'object' !== typeof source) return source;
  56. if (source instanceof Array) {
  57. const copy: any[] = [];
  58. for (let i = 0, len = source.length; i < len; i++) {
  59. copy[i] = deepClone(source[i]);
  60. }
  61. return copy as any as T;
  62. }
  63. if (source instanceof Object) {
  64. const copy: { [k: string]: any } = {};
  65. for (let k in source) {
  66. if (hasOwnProperty.call(source, k)) copy[k] = deepClone(source[k]);
  67. }
  68. return copy as any as T;
  69. }
  70. throw new Error(`Can't clone, type "${typeof source}" unsupported`);
  71. }
  72. export function mapObjectMap<T, S>(o: { [k: string]: T }, f: (v: T) => S): { [k: string]: S } {
  73. const ret: any = { };
  74. for (const k of Object.keys(o)) {
  75. ret[k] = f((o as any)[k]);
  76. }
  77. return ret;
  78. }
  79. export function objectForEach<T>(o: { [k: string]: T }, f: (v: T, k: string) => void) {
  80. for (const k of Object.keys(o)) {
  81. f((o as any)[k], k);
  82. }
  83. }