diff --git a/src/DangerLinqFunction.ts b/src/DangerLinqFunction.ts index de59a1a..0b6fa04 100644 --- a/src/DangerLinqFunction.ts +++ b/src/DangerLinqFunction.ts @@ -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", diff --git a/src/DangerLinqFunctionMapping.ts b/src/DangerLinqFunctionMapping.ts index 8528f8f..98436b0 100644 --- a/src/DangerLinqFunctionMapping.ts +++ b/src/DangerLinqFunctionMapping.ts @@ -12,6 +12,9 @@ export const DangerLinqFunctionMapping = { [DangerLinqFunction.Aggregate1]: (xs: Array, reducer: (memo: T, val: T) => T): T => { throw new DontCallThisError(); }, + [DangerLinqFunction.Any1]: (xs: Array, predicate: (x: T) => boolean): boolean => { + throw new DontCallThisError(); + }, [DangerLinqFunction.Batch]: (xs: Array, size: number): Array => { throw new DontCallThisError(); }, diff --git a/src/index.ts b/src/index.ts index 97dbdba..5406e18 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,8 @@ export function DangerLinq(func: DangerLinqFunction.Aggregate): Promise; /** 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; +/** Determines whether any element of a sequence satisfies a condition */ +export function DangerLinq(func: DangerLinqFunction.Any1): Promise; /** Applies a function to batch the source sequence into sized buckets. */ export function DangerLinq(func: DangerLinqFunction.Batch): Promise; /** Applies a function to batch the source sequence into sized buckets and applies a projection to each bucket. */ @@ -25,7 +27,6 @@ export function DangerLinq(func: DangerLinqFunction.Map): Promise; /**Filters a sequence of values based on a predicate */ export function DangerLinq(func: DangerLinqFunction.Where): Promise; - export async function DangerLinq(func: DangerLinqFunction) { // @ts-ignore if (cache.has(func)) { diff --git a/test/any1.spec.ts b/test/any1.spec.ts new file mode 100644 index 0000000..dd9dc8c --- /dev/null +++ b/test/any1.spec.ts @@ -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(); + }); +}); \ No newline at end of file