@ezez/utils 1.8.1 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +1 -0
  5. package/dist/index.js.map +1 -1
  6. package/dist/removeCommonProperties.d.ts +4 -0
  7. package/dist/removeCommonProperties.d.ts.map +1 -0
  8. package/dist/removeCommonProperties.js +15 -0
  9. package/dist/removeCommonProperties.js.map +1 -0
  10. package/docs/assets/search.js +1 -1
  11. package/docs/functions/cap.html +6 -5
  12. package/docs/functions/capitalize.html +6 -5
  13. package/docs/functions/coalesce.html +6 -5
  14. package/docs/functions/compareArrays.html +6 -5
  15. package/docs/functions/compareProps.html +6 -5
  16. package/docs/functions/ensureArray.html +6 -5
  17. package/docs/functions/ensureDate.html +6 -5
  18. package/docs/functions/ensureError.html +6 -5
  19. package/docs/functions/ensurePrefix.html +6 -5
  20. package/docs/functions/ensureSuffix.html +6 -5
  21. package/docs/functions/ensureTimestamp.html +6 -5
  22. package/docs/functions/escapeRegExp.html +6 -5
  23. package/docs/functions/formatDate.html +6 -5
  24. package/docs/functions/get.html +6 -5
  25. package/docs/functions/getMultiple.html +6 -5
  26. package/docs/functions/insertSeparator.html +6 -5
  27. package/docs/functions/isEmpty.html +6 -5
  28. package/docs/functions/isNumericString.html +6 -5
  29. package/docs/functions/isPlainObject.html +6 -5
  30. package/docs/functions/last.html +6 -5
  31. package/docs/functions/later-1.html +6 -5
  32. package/docs/functions/mapAsync.html +6 -5
  33. package/docs/functions/mapValues.html +6 -5
  34. package/docs/functions/match.html +6 -5
  35. package/docs/functions/merge.html +14 -13
  36. package/docs/functions/mostFrequent.html +6 -5
  37. package/docs/functions/noop.html +6 -5
  38. package/docs/functions/occurrences.html +6 -5
  39. package/docs/functions/omit.html +6 -5
  40. package/docs/functions/pick.html +6 -5
  41. package/docs/functions/pull.html +6 -5
  42. package/docs/functions/remove.html +6 -5
  43. package/docs/functions/removeCommonProperties.html +151 -0
  44. package/docs/functions/replace.html +6 -5
  45. package/docs/functions/rethrow.html +6 -5
  46. package/docs/functions/safe.html +7 -6
  47. package/docs/functions/scale.html +6 -5
  48. package/docs/functions/seq.html +6 -5
  49. package/docs/functions/seqEarlyBreak.html +6 -5
  50. package/docs/functions/set.html +6 -5
  51. package/docs/functions/setImmutable.html +6 -5
  52. package/docs/functions/sortBy.html +6 -5
  53. package/docs/functions/sortProps.html +6 -5
  54. package/docs/functions/stripPrefix.html +6 -5
  55. package/docs/functions/stripSuffix.html +6 -5
  56. package/docs/functions/throttle.html +6 -5
  57. package/docs/functions/truthy.html +6 -5
  58. package/docs/functions/unique.html +6 -5
  59. package/docs/functions/wait.html +6 -5
  60. package/docs/functions/waitFor.html +6 -5
  61. package/docs/functions/waitSync.html +6 -5
  62. package/docs/index.html +5 -4
  63. package/docs/interfaces/ComparePropsOptions.html +6 -6
  64. package/docs/interfaces/GetMultipleSource.html +6 -5
  65. package/docs/interfaces/GetSource.html +6 -5
  66. package/docs/interfaces/IsNumericStringOptions.html +9 -9
  67. package/docs/interfaces/OccurencesOptions.html +6 -6
  68. package/docs/interfaces/SetImmutableSource.html +6 -5
  69. package/docs/interfaces/SetSource.html +6 -5
  70. package/docs/interfaces/ThrottleOptions.html +7 -7
  71. package/docs/interfaces/ThrottledFunctionExtras.html +7 -7
  72. package/docs/modules.html +6 -4
  73. package/docs/pages/CHANGELOG.html +46 -34
  74. package/docs/pages/Introduction.html +5 -4
  75. package/docs/types/Later.html +6 -5
  76. package/docs/types/MapValuesFn.html +6 -5
  77. package/docs/types/MatchCallback.html +6 -5
  78. package/docs/types/SeqEarlyBreaker.html +6 -5
  79. package/docs/types/SeqFn.html +6 -5
  80. package/docs/types/SeqFunctions.html +6 -5
  81. package/docs/types/SetImmutablePath.html +6 -5
  82. package/docs/types/ThrottledFunction.html +6 -5
  83. package/docs/variables/mapValuesUNSET.html +6 -5
  84. package/docs/variables/mergeUNSET.html +6 -5
  85. package/esm/index.d.ts +1 -0
  86. package/esm/index.d.ts.map +1 -1
  87. package/esm/index.js +1 -0
  88. package/esm/index.js.map +1 -1
  89. package/esm/removeCommonProperties.d.ts +4 -0
  90. package/esm/removeCommonProperties.d.ts.map +1 -0
  91. package/esm/removeCommonProperties.js +12 -0
  92. package/esm/removeCommonProperties.js.map +1 -0
  93. package/package.json +3 -3
  94. package/src/index.ts +1 -0
  95. package/src/removeCommonProperties.spec.ts +34 -0
  96. package/src/removeCommonProperties.ts +39 -0
@@ -0,0 +1,39 @@
1
+ type Obj = Record<string, unknown>;
2
+
3
+ /**
4
+ * Removes properties from all targets if they are the same as in source. It mutates the targets!
5
+ * Values are JSON-serialized before comparison, therefore, it will consider arrays and objects with the same content
6
+ * as equal.
7
+ *
8
+ * The property must be exactly the same in all targets. If even one target has different value for given property - the
9
+ * property will stay untouched on all targets.
10
+ *
11
+ * If you want to remove properties from each target that has given property the same as source - just run this
12
+ * function in a loop, giving one target at a time.
13
+ *
14
+ * Note: This function is intended to be used on small data sets that are JSON serializable. It will crash on circular
15
+ * references and may produce unexpected results on non-serializable data (like functions).
16
+ *
17
+ * @param source - source object
18
+ * @param target1 - first target object (required)
19
+ * @param ...targetN - more target objects (optional)
20
+ * @example removeCommonProperties({ title: "Hello", author: "John"}, { title: "Hello 2", author: "John" }, { title: "Hello 3", author: "John" });
21
+ * // targets: { title: "Hello 2" }, { title: "Hello 3" }
22
+ * @example removeCommonProperties({ title: "Hello", author: "John"}, { title: "Hello 2", author: "John" }, { title: "Hello 3", author: "Matt" });
23
+ * // targets: { title: "Hello 2", author: "John" }, { title: "Hello 3", author: "Matt" }
24
+ */
25
+ const removeCommonProperties = (source: Obj, target1: Obj, ...targetN: Obj[]) => {
26
+ const targets = [target1, ...targetN];
27
+ const keys = Object.keys(source);
28
+ for (const key of keys) {
29
+ const allTheSame = targets.every((target) => JSON.stringify(target[key]) === JSON.stringify(source[key]));
30
+ if (allTheSame) {
31
+ // eslint-disable-next-line no-param-reassign,@typescript-eslint/no-dynamic-delete
32
+ targets.forEach((target) => delete target[key]);
33
+ }
34
+ }
35
+ };
36
+
37
+ export {
38
+ removeCommonProperties,
39
+ };