Skip to content

Commit c809249

Browse files
committed
Fix MapExtended when literal array was provided. Add PageToObject function to fix issue in MapExtended when an actual page was provided
1 parent 8385e93 commit c809249

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/fql-lib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { MapExtended } from "./functions/MapExtended"
22
export { ObjectKeys } from "./functions/ObjectKeys"
3+
export { PageToObject } from "./functions/PageToObject"
34
export { PaginateReverse } from "./functions/PaginateReverse"
45
export { Reverse } from "./functions/Reverse"

src/functions/MapExtended.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { query as q, ExprArg, Lambda } from "faunadb"
1+
import { query as q, ExprArg, Lambda, ExprVal } from "faunadb"
2+
import { PageToObject } from "./PageToObject"
23

34
export const MapExtended = (
45
collection: ExprArg,
56
lambdaExpr: ExprArg | Lambda
6-
) =>
7-
q.If(
7+
): ExprVal => {
8+
return q.If(
89
q.IsArray(collection),
9-
q.Map(q.Select(["data"], collection), lambdaExpr),
10+
q.Map(collection, lambdaExpr),
1011
q.Let(
1112
{
1213
data: q.Map(q.Select(["data"], collection), lambdaExpr),
1314
},
14-
q.Merge(collection, { data: q.Var("data") })
15+
q.Merge(PageToObject(collection), { data: q.Var("data") })
1516
)
1617
)
18+
}

src/functions/PageToObject.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { query as q, ExprArg, ExprVal } from "faunadb"
2+
3+
export const PageToObject = (page: ExprArg): ExprVal =>
4+
/*
5+
Is this a bug? Using as a feature in this case.
6+
q.Merge({}, { foo: null }) // => {}
7+
q.Merge({foo: null }, { foo: null }) // => {}
8+
q.Merge({foo: null }, {}) // => { foo: null }
9+
*/
10+
q.Merge(
11+
{},
12+
{
13+
before: q.Select(["before"], page, null),
14+
after: q.Select(["after"], page, null),
15+
data: q.Select(["data"], page),
16+
}
17+
)

0 commit comments

Comments
 (0)