Skip to content

Commit

Permalink
fix merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Mar 26, 2024
1 parent 15f432a commit 139d64f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/helpers/merge.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { PlainObject } from "../types";
import { isKey, isObject } from "../validators";
import { getKeys } from "./getKeys";

/**
* @description Simple merge function that merges two objects, arrays get overwritten, no options
*
*/
export const merge = (target: PlainObject, source: PlainObject) => {
const merger: PlainObject = {};
getKeys(target).forEach((key) => {
Object.keys(target).forEach((key) => {
merger[key] = isObject(target[key]) ? merge({}, target[key]) : target[key];
});
getKeys(source).forEach((key) => {
Object.keys(source).forEach((key) => {
if (isKey(key, target))
merger[key] =
isObject(target[key]) && isObject(source[key])
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/objectDiff.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { PlainObject } from "../types";
import { isSame } from "../validators/isSame";
import { getKeys } from "./getKeys";

export const objectDiff = (
leftObject: PlainObject,
rightObject: PlainObject
) => {
var diff: PlainObject = {};
const allKeys = new Set([...getKeys(leftObject), ...getKeys(rightObject)]);
const allKeys = new Set([
...Object.keys(leftObject),
...Object.keys(rightObject),
]);

for (const key of allKeys) {
if (!isSame(rightObject[key], leftObject[key])) {
Expand Down
12 changes: 6 additions & 6 deletions src/types/HashMap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Key } from "./Object";
import { PlainKey } from "./Object";

// I don't like the Dict keyword, but it's a possibility...
export type HashMap<T> = Record<Key, T>;
export type NumberMap = Record<Key, number>;
export type StringMap = Record<Key, string>;
export type BoolMap = Record<Key, boolean>;
export type TrueMap = Record<Key, true>;
export type HashMap<T> = Record<PlainKey, T>;
export type NumberMap = Record<PlainKey, number>;
export type StringMap = Record<PlainKey, string>;
export type BoolMap = Record<PlainKey, boolean>;
export type TrueMap = Record<PlainKey, true>;

0 comments on commit 139d64f

Please sign in to comment.