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(array): unanimous #5

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions src/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,41 @@ describe("array", () => {
expect(result).toEqual([{ foo: 1, bar: 2 }, { foo: 2 }]);
});
});

describe("unanimous", () => {
it("returns true if all elements match", () => {
// given everything has the same 'name' property
const a = [{ name: "Homebound" }, { name: "Homebound" }, { name: "Homebound" }];
// then expect them to unanimously match
expect(a.unanimous((el) => el.name)).toBe(true);
});

it("returns false if elements don't match", () => {
// given 1 item has a different name
const a = [{ name: "Homebound" }, { name: "Homebound" }, { name: "Nikki" }];
// then expect them NOT to be unanimous
expect(a.unanimous((el) => el.name)).toBe(false);
});

it("works for simple data", () => {
// given even numbers
const a = [0, 2, 4, 6, 8];
// then a check against each of them is unanimous
expect(a.unanimous((el) => el % 2 === 0)).toBe(true);
});

it("returns true for arrays-of-1", () => {
// given 1 item
const a = [1];
// then it is unanimous with itself
expect(a.unanimous((el) => el)).toBe(true);
});

it("returns true for empty array", () => {
TylerR909 marked this conversation as resolved.
Show resolved Hide resolved
// given an empty array
const a = [];
// then it's unanimously silent
expect(a.unanimous((el) => el)).toBe(true);
});
});
});
8 changes: 8 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ declare global {
min(this: number[] | Date[]): T;
min<R extends number | Date>(this: Array<T>, f: (el: T, index: number, array: Array<T>) => R): R;
count(f: (el: T, index: number, array: T[]) => boolean): number;
unanimous(f: (el: T) => unknown): boolean;
}

interface ReadonlyArray<T> {
Expand Down Expand Up @@ -132,6 +133,7 @@ declare global {
min(this: readonly number[] | readonly Date[]): T;
min<R extends number | Date>(f: (el: T, index: number, array: readonly T[]) => R): R;
count(f: (el: T, index: number, array: readonly T[]) => boolean): number;
unanimous(f: (el: T) => unknown): boolean;
TylerR909 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -401,6 +403,12 @@ Array.prototype.count = function <T>(this: T[], f: (el: T, index: number, array:
return this.reduce((count, ...args) => (f(...args) ? count + 1 : count), 0);
};

Array.prototype.unanimous = function <T>(this: T[], f: (el: T) => unknown): boolean {
const [first, ...rest] = this;
const comparator = f(first);
TylerR909 marked this conversation as resolved.
Show resolved Hide resolved
return rest.every((el) => f(el) === comparator);
};

Object.defineProperty(Array.prototype, "isEmpty", {
enumerable: false,
get: function () {
Expand Down