From aeb7fb4d1d70462abcad45d4e159c796d57a3e94 Mon Sep 17 00:00:00 2001 From: Rafael Antunes Date: Sat, 15 May 2021 11:23:51 -0400 Subject: [PATCH 1/4] test: callback that returns QueryOptions --- test/callback.test.ts | 33 +++++++++++++++++++++++++++++++++ test/mocks.ts | 3 +++ 2 files changed, 36 insertions(+) create mode 100644 test/callback.test.ts 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, }, ], }, From 0ee76b8c832b5e72a2e8a154b73eabca749774e8 Mon Sep 17 00:00:00 2001 From: Rafael Antunes Date: Sat, 15 May 2021 11:24:43 -0400 Subject: [PATCH 2/4] feat: arrayQL 2nd param to be a function --- index.d.ts | 2 +- src/index.ts | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) 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/src/index.ts b/src/index.ts index 8fbc8be..36a9a1a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,13 +38,17 @@ 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; } From 331460cdbd33896801adefd597b1d585d81d2adf Mon Sep 17 00:00:00 2001 From: Rafael Antunes Date: Sat, 15 May 2021 11:33:04 -0400 Subject: [PATCH 3/4] lib: arrayQL 2nd param to be a function --- lib/index.d.ts | 2 +- lib/index.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) 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; } From 42fda4589e45859e0f8f079775dab3b12b63d735 Mon Sep 17 00:00:00 2001 From: Rafael Antunes Date: Sat, 15 May 2021 11:34:43 -0400 Subject: [PATCH 4/4] fix: remove blank line --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 36a9a1a..b3091f8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,6 @@ export function arrayQL( for (const object of array) { const options = typeof queryOptions === "function" ? queryOptions(object) : queryOptions; - options.where ??= () => true; if (!options.where(object)) continue; mappedArray.push(objectQL(object, options.keys));