diff --git a/index.d.ts b/index.d.ts index 10706f2..a8e129c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -26,5 +26,5 @@ export interface QueryOptions { export function arrayQL( array: T[], - queryOptions: QueryOptions + queryOptions: QueryOptions | ((object: T) => QueryOptions) ): R[]; diff --git a/lib/index.d.ts b/lib/index.d.ts index 10706f2..a8e129c 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -26,5 +26,5 @@ export interface QueryOptions { export function arrayQL( array: T[], - queryOptions: QueryOptions + queryOptions: QueryOptions | ((object: T) => QueryOptions) ): R[]; diff --git a/lib/index.js b/lib/index.js index 8ff9221..ac0e379 100644 --- a/lib/index.js +++ b/lib/index.js @@ -51,13 +51,14 @@ var __assign = (this && this.__assign) || function () { } function arrayQL(array, queryOptions) { var _a; - (_a = queryOptions.where) !== null && _a !== void 0 ? _a : (queryOptions.where = function () { return true; }); var mappedArray = []; for (var _i = 0, array_1 = array; _i < array_1.length; _i++) { var object = array_1[_i]; - if (!queryOptions.where(object)) + var options = typeof queryOptions === "function" ? queryOptions(object) : queryOptions; + (_a = options.where) !== null && _a !== void 0 ? _a : (options.where = function () { return true; }); + if (!options.where(object)) continue; - mappedArray.push(objectQL(object, queryOptions.keys)); + mappedArray.push(objectQL(object, options.keys)); } return mappedArray; } diff --git a/src/index.ts b/src/index.ts index 8fbc8be..b3091f8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,13 +38,16 @@ function objectQL(object: T, booleanKeys: BooleanKeys) { export function arrayQL( array: T[], - queryOptions: QueryOptions + queryOptions: QueryOptions | ((object: T) => QueryOptions) ): R[] { - queryOptions.where ??= () => true; const mappedArray = []; + for (const object of array) { - if (!queryOptions.where(object)) continue; - mappedArray.push(objectQL(object, queryOptions.keys)); + const options = + typeof queryOptions === "function" ? queryOptions(object) : queryOptions; + options.where ??= () => true; + if (!options.where(object)) continue; + mappedArray.push(objectQL(object, options.keys)); } return mappedArray; } diff --git a/test/callback.test.ts b/test/callback.test.ts new file mode 100644 index 0000000..3806445 --- /dev/null +++ b/test/callback.test.ts @@ -0,0 +1,33 @@ +import { arrayQL } from "../src"; +import { unorganizedData } from "./mocks"; + +describe("arrayql queries (map, filter)", () => { + it("should map same age user and their friends", () => { + const result = arrayQL(unorganizedData, ({ age }) => { + return { + keys: { + age: true, + friends: { + where: (friend) => age === friend.age, + keys: { + age: true, + }, + }, + }, + }; + }); + + const expectedResult = [ + { + age: 18, + friends: [], + }, + { + age: 30, + friends: [{ age: 30 }], + }, + ]; + + expect(result).toEqual(expectedResult); + }); +}); diff --git a/test/mocks.ts b/test/mocks.ts index d76095f..de849d2 100644 --- a/test/mocks.ts +++ b/test/mocks.ts @@ -16,6 +16,7 @@ export const unorganizedData = [ { name: "jamelin", id: 1, + age: 20, }, ], }, @@ -36,10 +37,12 @@ export const unorganizedData = [ { name: "jamelão", id: 1, + age: 30, }, { name: "rafael", id: 9, + age: 29, }, ], },