Skip to content
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
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export interface QueryOptions<T> {

export function arrayQL<T, R = any>(
array: T[],
queryOptions: QueryOptions<T>
queryOptions: QueryOptions<T> | ((object: T) => QueryOptions<T>)
): R[];
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export interface QueryOptions<T> {

export function arrayQL<T, R = any>(
array: T[],
queryOptions: QueryOptions<T>
queryOptions: QueryOptions<T> | ((object: T) => QueryOptions<T>)
): R[];
7 changes: 4 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ function objectQL<T>(object: T, booleanKeys: BooleanKeys<T>) {

export function arrayQL<T, R = any>(
array: T[],
queryOptions: QueryOptions<T>
queryOptions: QueryOptions<T> | ((object: T) => QueryOptions<T>)
): 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;
}
33 changes: 33 additions & 0 deletions test/callback.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 3 additions & 0 deletions test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const unorganizedData = [
{
name: "jamelin",
id: 1,
age: 20,
},
],
},
Expand All @@ -36,10 +37,12 @@ export const unorganizedData = [
{
name: "jamelão",
id: 1,
age: 30,
},
{
name: "rafael",
id: 9,
age: 29,
},
],
},
Expand Down