@augment-vir/common 21.0.0 → 21.1.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.
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mergeDeep = void 0;
4
4
  const runtime_type_of_1 = require("../runtime-type-of");
5
5
  const tuple_1 = require("../tuple");
6
+ const object_1 = require("./object");
6
7
  /**
7
8
  * Accepts multiple objects and merges their key-value pairs recursively.
8
9
  *
@@ -10,28 +11,40 @@ const tuple_1 = require("../tuple");
10
11
  */
11
12
  function mergeDeep(...inputs) {
12
13
  if (!(0, tuple_1.isLengthAtLeast)(inputs, 1)) {
14
+ // nothing to merge if no inputs
13
15
  return {};
14
16
  }
15
17
  if (inputs.length === 1) {
18
+ // nothing to merge if only one input
16
19
  return inputs[0];
17
20
  }
18
- let result = {};
21
+ let result = undefined;
19
22
  const mergeProps = {};
20
23
  inputs.forEach((individualInput) => {
21
24
  try {
25
+ if (!(0, object_1.isObject)(individualInput)) {
26
+ /** If not an object, we can't merge. So overwrite instead. */
27
+ result = individualInput;
28
+ return;
29
+ }
22
30
  Object.entries(individualInput).forEach(([key, value,]) => {
23
- if ((0, runtime_type_of_1.isRuntimeTypeOf)(value, 'object')) {
24
- const mergePropsArray = mergeProps[key] || [];
25
- if (!mergeProps[key]) {
26
- mergeProps[key] = mergePropsArray;
27
- }
28
- mergePropsArray.push(value);
31
+ const mergePropsArray = mergeProps[key] || [];
32
+ if (!mergeProps[key]) {
33
+ mergeProps[key] = mergePropsArray;
29
34
  }
35
+ mergePropsArray.push(value);
30
36
  });
31
- result = {
32
- ...result,
33
- ...individualInput,
34
- };
37
+ if (result) {
38
+ if ((0, runtime_type_of_1.isRuntimeTypeOf)(result, 'object')) {
39
+ result = {
40
+ ...result,
41
+ ...individualInput,
42
+ };
43
+ }
44
+ }
45
+ else {
46
+ result = individualInput;
47
+ }
35
48
  }
36
49
  catch (error) {
37
50
  /** Ignore errors, such as individualInput not actually being an object. */
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.copyThroughJson = exports.isObject = void 0;
4
+ /** Checks if the input is non-null and an object. Includes arrays. */
4
5
  function isObject(input) {
5
6
  return !!input && typeof input === 'object';
6
7
  }
@@ -1,5 +1,6 @@
1
1
  import { isRuntimeTypeOf } from '../runtime-type-of';
2
2
  import { isLengthAtLeast } from '../tuple';
3
+ import { isObject } from './object';
3
4
  /**
4
5
  * Accepts multiple objects and merges their key-value pairs recursively.
5
6
  *
@@ -7,28 +8,40 @@ import { isLengthAtLeast } from '../tuple';
7
8
  */
8
9
  export function mergeDeep(...inputs) {
9
10
  if (!isLengthAtLeast(inputs, 1)) {
11
+ // nothing to merge if no inputs
10
12
  return {};
11
13
  }
12
14
  if (inputs.length === 1) {
15
+ // nothing to merge if only one input
13
16
  return inputs[0];
14
17
  }
15
- let result = {};
18
+ let result = undefined;
16
19
  const mergeProps = {};
17
20
  inputs.forEach((individualInput) => {
18
21
  try {
22
+ if (!isObject(individualInput)) {
23
+ /** If not an object, we can't merge. So overwrite instead. */
24
+ result = individualInput;
25
+ return;
26
+ }
19
27
  Object.entries(individualInput).forEach(([key, value,]) => {
20
- if (isRuntimeTypeOf(value, 'object')) {
21
- const mergePropsArray = mergeProps[key] || [];
22
- if (!mergeProps[key]) {
23
- mergeProps[key] = mergePropsArray;
24
- }
25
- mergePropsArray.push(value);
28
+ const mergePropsArray = mergeProps[key] || [];
29
+ if (!mergeProps[key]) {
30
+ mergeProps[key] = mergePropsArray;
26
31
  }
32
+ mergePropsArray.push(value);
27
33
  });
28
- result = {
29
- ...result,
30
- ...individualInput,
31
- };
34
+ if (result) {
35
+ if (isRuntimeTypeOf(result, 'object')) {
36
+ result = {
37
+ ...result,
38
+ ...individualInput,
39
+ };
40
+ }
41
+ }
42
+ else {
43
+ result = individualInput;
44
+ }
32
45
  }
33
46
  catch (error) {
34
47
  /** Ignore errors, such as individualInput not actually being an object. */
@@ -1,3 +1,4 @@
1
+ /** Checks if the input is non-null and an object. Includes arrays. */
1
2
  export function isObject(input) {
2
3
  return !!input && typeof input === 'object';
3
4
  }
@@ -4,6 +4,7 @@ export type PartialAndNullable<T extends object> = {
4
4
  export type PartialAndUndefined<T extends object> = {
5
5
  [Prop in keyof T]?: T[Prop] | undefined;
6
6
  };
7
+ /** Checks if the input is non-null and an object. Includes arrays. */
7
8
  export declare function isObject(input: any): input is NonNullable<object>;
8
9
  /** The input here must be serializable otherwise JSON parsing errors will be thrown */
9
10
  export declare function copyThroughJson<T>(input: T): T;
@@ -1,6 +1,7 @@
1
+ import { AnyFunction } from '../function';
1
2
  import { NestedKeys } from './nested-keys';
2
3
  type InnerPickDeep<OriginalObjectGeneric extends object, DeepKeys extends any[]> = DeepKeys extends [infer CurrentLevelPick, ...infer RemainingKeys] ? {
3
- [CurrentProp in Extract<CurrentLevelPick, keyof OriginalObjectGeneric>]: OriginalObjectGeneric[CurrentProp] extends object ? InnerPickDeep<OriginalObjectGeneric[CurrentProp], RemainingKeys> : OriginalObjectGeneric[CurrentProp];
4
+ [CurrentProp in Extract<CurrentLevelPick, keyof OriginalObjectGeneric>]: OriginalObjectGeneric[CurrentProp] extends AnyFunction ? OriginalObjectGeneric[CurrentProp] : OriginalObjectGeneric[CurrentProp] extends Array<any> ? OriginalObjectGeneric[CurrentProp] : OriginalObjectGeneric[CurrentProp] extends object ? InnerPickDeep<OriginalObjectGeneric[CurrentProp], RemainingKeys> : OriginalObjectGeneric[CurrentProp];
4
5
  } : DeepKeys extends [] ? OriginalObjectGeneric : DeepKeys extends [infer CurrentLevelPick] ? CurrentLevelPick extends keyof OriginalObjectGeneric ? Pick<OriginalObjectGeneric, CurrentLevelPick> : never : never;
5
6
  /**
6
7
  * Pick nested keys with more strict type parameter requirements. However, these stricter type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "21.0.0",
3
+ "version": "21.1.0",
4
4
  "homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
5
5
  "bugs": {
6
6
  "url": "https://github.com/electrovir/augment-vir/issues"