Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: merge objects #25

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"files": [
"README.md"
],
"files": ["README.md"],
"imageSize": 100,
"commit": false,
"commitType": "docs",
Expand All @@ -12,46 +10,35 @@
"name": "Aral Roca Gomez",
"avatar_url": "https://avatars.githubusercontent.com/u/13313058?v=4",
"profile": "https://aralroca.com",
"contributions": [
"code",
"maintenance"
]
"contributions": ["code", "maintenance"]
},
{
"login": "robinpokorny",
"name": "Robin Pokorny",
"avatar_url": "https://avatars.githubusercontent.com/u/68341?v=4",
"profile": "http://robinpokorny.com",
"contributions": [
"code"
]
"contributions": ["code"]
},
{
"login": "leeferwagen",
"name": "Muslim Idris",
"avatar_url": "https://avatars.githubusercontent.com/u/1287545?v=4",
"profile": "https://github.com/leeferwagen",
"contributions": [
"code"
]
"contributions": ["code"]
},
{
"login": "namhtpyn",
"name": "namhtpyn",
"avatar_url": "https://avatars.githubusercontent.com/u/16488178?v=4",
"profile": "https://github.com/namhtpyn",
"contributions": [
"infra"
]
"contributions": ["infra"]
},
{
"login": "Josuto",
"name": "Josu Martinez",
"avatar_url": "https://avatars.githubusercontent.com/u/6097850?v=4",
"profile": "https://github.com/Josuto",
"contributions": [
"bug"
]
"contributions": ["bug"]
}
],
"contributorsPerLine": 7,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _A **tiny** (~500B) **JavaScript library** that allows you to set **default valu
[![Weekly downloads](https://badgen.net/npm/dw/default-composer?color=blue)](https://www.npmjs.com/package/default-composer)
[![PRs Welcome][badge-prwelcome]][prwelcome]<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

[badge-prwelcome]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@
"@babel/preset-typescript": "7.21.5",
"microbundle": "0.15.1"
}
}
}
22 changes: 13 additions & 9 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe("defaultComposer", () => {
nested: {
example: ["a", "d", "e"],
nums: [1, 4, 5],
objects: [{ a: 1 }, { b: 2 }],
objects: [{ c: 1 }, { d: 2 }, { e: 3 }],
notDefaultableKey: ["shouldNotBeMerged"],
},
};
Expand All @@ -297,7 +297,11 @@ describe("defaultComposer", () => {
nested: {
example: ["a", "b", "c", "d", "e"],
nums: [1, 2, 3, 4, 5],
objects: [{ a: 1 }, { b: 2 }, { a: 1 }, { b: 2 }],
objects: [
{ a: 1, c: 1 },
{ b: 2, d: 2 },
{ e: 3 }
],
notDefaultableKey: ["shouldNotBeMerged"],
},
};
Expand All @@ -310,13 +314,13 @@ describe("defaultComposer", () => {
expect(defaultComposer<any>(defaults, object)).toEqual(expected);
});

it('should respect the Date object', () => {
it("should respect the Date object", () => {
const defaults = {
date: new Date(),
range: {
startDate: new Date(2020, 1, 1),
endDate: new Date(2020, 1, 2),
}
},
};

const object = {
Expand All @@ -332,29 +336,29 @@ describe("defaultComposer", () => {
range: {
startDate: new Date(2020, 1, 1),
endDate: new Date(2020, 1, 2),
}
},
};

expect(output).toStrictEqual(expected);
});

it('should respect instance of any class', () => {
it("should respect instance of any class", () => {
class TestClass {
constructor(public name: string) { }
}

const defaults = {
test: new TestClass('test'),
test: new TestClass("test"),
};

const object = {
test: new TestClass('test2'),
test: new TestClass("test2"),
};

const output = defaultComposer<any>(defaults, object);

const expected = {
test: new TestClass('test2'),
test: new TestClass("test2"),
};

expect(output).toStrictEqual(expected);
Expand Down
28 changes: 25 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,23 @@ function compose<T>(defaults: Partial<T>, obj: Partial<T>): Partial<T> {
Array.isArray(defaultsValue) &&
Array.isArray(originalObjectValue)
) {
result[key] = [...new Set([...defaultsValue, ...originalObjectValue])];
if (!defaultsValue.some(isObject)) {
result[key] = [...new Set([...defaultsValue, ...originalObjectValue])];
continue;
}

const maxLength = Math.max(
defaultsValue.length,
originalObjectValue.length,
);
const setOfObjects = new Set();

for (let i = 0; i < maxLength; i++) {
setOfObjects.add(compose(defaultsValue[i] ?? {}, originalObjectValue[i] ?? {}));
}

result[key] = [...setOfObjects];

continue;
}

Expand All @@ -68,11 +84,17 @@ function compose<T>(defaults: Partial<T>, obj: Partial<T>): Partial<T> {
}

function isObject(value: any): boolean {
return typeof value === "object" && value !== null && value.constructor === Object && !Array.isArray(value);
return (
typeof value === "object" &&
value !== null &&
value.constructor === Object &&
!Array.isArray(value)
);
}

function isEmptyObjectOrArray<T>(object: T): boolean {
if (typeof object !== "object" || object === null || object instanceof Date) return false;
if (typeof object !== "object" || object === null || object instanceof Date)
return false;
return getAllKeys(object).length === 0;
}

Expand Down