Skip to content

Commit

Permalink
Support symbol properties (#10)
Browse files Browse the repository at this point in the history
* support symbol properties

* fix type to support all peoperty keys
  • Loading branch information
robinpokorny committed Jun 16, 2023
1 parent 9dcca68 commit 5308602
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type User = {
emails: string[];
toString: string;
constructor: string | null;
[Symbol.isConcatSpreadable]: boolean;
};

describe("defaultComposer", () => {
Expand All @@ -38,6 +39,7 @@ describe("defaultComposer", () => {
emails: ["[email protected]"],
hobbies: ["programming"],
toString: "I am Aral",
[Symbol.isConcatSpreadable]: false,
};

const originalObject = {
Expand Down Expand Up @@ -70,6 +72,7 @@ describe("defaultComposer", () => {
hobbies: ["parkour", "computer science", "books", "nature"],
toString: "I am Aral",
constructor: null,
[Symbol.isConcatSpreadable]: false,
};

expect(defaultComposer<User>(defaults, originalObject)).toEqual(expected);
Expand Down Expand Up @@ -137,6 +140,30 @@ describe("defaultComposer", () => {
expect(mockFn).toBeCalledTimes(1);
});

it("should work with enumerable symbol properties", () => {
const tag = Symbol.for("tag");
const version = Symbol.for("version");
const createdAt = Symbol.for("createdAt");

const defaults = {
[tag]: "user",
[createdAt]: Date.UTC(2020, 1, 1),
};

Object.defineProperty(defaults, version, { value: 1, enumerable: false });

const object = {
[createdAt]: Date.UTC(2023, 6, 1),
};

const expected = {
[tag]: "user",
[createdAt]: Date.UTC(2023, 6, 1),
};

expect(defaultComposer<any>(defaults, object)).toEqual(expected);
});

it("should work with a custom isDefaultableValue", () => {
const defaults = {
original: {
Expand Down
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type isDefaultableValueInputType = {
defaultableValue: boolean;
key: string;
key: PropertyKey;
value: unknown;
};

Expand All @@ -26,7 +26,7 @@ export function defaultComposer<T>(...args: Partial<T>[]): T {

function compose<T>(defaults: Partial<T>, obj: Partial<T>): Partial<T> {
const result: Partial<T> = {};
const allKeys = new Set([defaults, obj].flatMap(Object.keys));
const allKeys = new Set([defaults, obj].flatMap(getAllKeys));

for (let key of allKeys) {
const defaultsValue = defaults[key];
Expand Down Expand Up @@ -60,7 +60,7 @@ function isObject(value: any): boolean {

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

function checkDefaultableValue({ value }: { value: unknown }): boolean {
Expand All @@ -78,3 +78,12 @@ function hasOwn<T extends PropertyKey>(
): key is T {
return Object.prototype.hasOwnProperty.call(obj, key);
}

function getAllKeys(object: {}): PropertyKey[] {
return [
...Object.keys(object),
...Object.getOwnPropertySymbols(object).filter(
(key) => Object.getOwnPropertyDescriptor(object, key)?.enumerable
),
];
}

0 comments on commit 5308602

Please sign in to comment.