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

WIP adds any1 #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/DangerLinqFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum DangerLinqFunction {
Aggregate = "jreina/902da5688cb96d55af41e05c023b374e/raw/3866f8a3c86a26dc14832d42fe6ab062b9baa478/Aggregate.js",
Aggregate1 = "jreina/0f4dab07c6be88054df6de0f350d3f76/raw/84116870af9aa3c42997c89b1240d4ad6d85c095/Aggregate1.js",
Aggregate2 = "jreina/de6bfd33867a60ee9c06537b49dfa9f7/raw/45fe74147e683dd74daf29de7d038407a094f501/Aggregate2.js",
Any1 = "todo.js",
Batch = "the-pat/d5a1bfca21be2beafad0a6a7559013e0/raw/a994a899302f07475ebb99041ac9d4c53aa81776/Batch.js",
Batch1 = "the-pat/f7588fca09549c30de33808a900cc60f/raw/1532ea284bac98acd72a4ff3d49b203969def63d/Batch1.js",
GroupBy = "jreina/655b2016649fcb644af3f73d6cbab699/raw/c7c24e39862e90d83c7ba410b72ef5d537e73d2e/GroupBy.js",
Expand Down
3 changes: 3 additions & 0 deletions src/DangerLinqFunctionMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const DangerLinqFunctionMapping = {
[DangerLinqFunction.Aggregate1]: <T>(xs: Array<T>, reducer: (memo: T, val: T) => T): T => {
throw new DontCallThisError();
},
[DangerLinqFunction.Any1]: <T>(xs: Array<T>, predicate: (x: T) => boolean): boolean => {
throw new DontCallThisError();
},
[DangerLinqFunction.Batch]: <T>(xs: Array<T>, size: number): Array<T> => {
throw new DontCallThisError();
},
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export function DangerLinq(func: DangerLinqFunction.Aggregate): Promise<typeof D
export function DangerLinq(func: DangerLinqFunction.Aggregate1): Promise<typeof DangerLinqFunctionMapping[DangerLinqFunction.Aggregate1]>;
/** Applies an accumulator function over every element in the source array using the specified seed value as the initial memo value, and applies the result selector to the result. */
export function DangerLinq(func: DangerLinqFunction.Aggregate2): Promise<typeof DangerLinqFunctionMapping[DangerLinqFunction.Aggregate2]>;
/** Determines whether any element of a sequence satisfies a condition */
export function DangerLinq(func: DangerLinqFunction.Any1): Promise<typeof DangerLinqFunctionMapping[DangerLinqFunction.Any1]>;
/** Applies a function to batch the source sequence into sized buckets. */
export function DangerLinq(func: DangerLinqFunction.Batch): Promise<typeof DangerLinqFunctionMapping[DangerLinqFunction.Batch]>;
/** Applies a function to batch the source sequence into sized buckets and applies a projection to each bucket. */
Expand All @@ -25,7 +27,6 @@ export function DangerLinq(func: DangerLinqFunction.Map): Promise<typeof DangerL
export function DangerLinq(func: DangerLinqFunction.SleepSort): Promise<typeof DangerLinqFunctionMapping[DangerLinqFunction.SleepSort]>;
/**Filters a sequence of values based on a predicate */
export function DangerLinq(func: DangerLinqFunction.Where): Promise<typeof DangerLinqFunctionMapping[DangerLinqFunction.Where]>;

export async function DangerLinq(func: DangerLinqFunction) {
// @ts-ignore
if (cache.has(func)) {
Expand Down
54 changes: 54 additions & 0 deletions test/any1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { expect } from "chai";
import { DangerLinq } from "../src";
import { DangerLinqFunction } from "../src/DangerLinqFunction";

/**
* TODO
* Test cases:
* - Nonempty array, predicate matches an item in array
* - Nonempty array, predicate doesn't match an item in array
* - Empty array, predicate doesn't matter
* - Array is null, predicate doesn't matter
* - Array is undefined, predicate doesn't matter
* - Array is nonempty, predicate is null
* - Array is nonempty, predicate is undefined
*/

describe("DangerLinqFunction.Any", () => {
it("Should be a function", async () => {
const any = await DangerLinq(DangerLinqFunction.Any1);
expect(any).to.be.a("function");
});

it("Should return true for a non empty list", async () => {
const any = await DangerLinq(DangerLinqFunction.Any1);
const input = [0, 1, 2, 3];
const actual = any(input);
const expected = true;

expect(actual).to.eql(expected);
});

it("Should return false for empty list", async () => {
const any = await DangerLinq(DangerLinqFunction.Any1);
const input: any[] = [];
const actual = any(input);
const expected = false;

expect(actual).to.eql(expected);
});

it("Should throw an error when null is passed in", async () => {
const any = await DangerLinq(DangerLinqFunction.Any1);

// @ts-ignore
expect(() => any(null)).to.throw();
});

it("Should throw an error when undefined is passed in", async () => {
const any = await DangerLinq(DangerLinqFunction.Any1);

// @ts-ignore
expect(() => any(undefined)).to.throw();
});
});