Skip to content

Commit

Permalink
TS - add group, if
Browse files Browse the repository at this point in the history
  • Loading branch information
elisherer committed Aug 22, 2024
1 parent 238434e commit 94e7156
Show file tree
Hide file tree
Showing 9 changed files with 707 additions and 5 deletions.
8 changes: 5 additions & 3 deletions javascript/json-transform/src/JsonHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const JSONPATH_ROOT = "$",
const isNullOrUndefined = (value: any): value is null | undefined => value == null || typeof value === "undefined";
const isMap = (value: any): value is Record<string, any> => value && typeof value === "object" && !Array.isArray(value);

const getAsString = (value: any): null | string => {
function getAsString<T extends string | number | boolean | object>(value: T): string;
function getAsString<T extends null | undefined>(value: T): null;
function getAsString(value: any): string | null {
if (isNullOrUndefined(value)) {
return null;
}
Expand All @@ -26,7 +28,7 @@ const getAsString = (value: any): null | string => {
return value ? "true" : "false";
}
return JSON.stringify(value);
};
}

const numberCompare = (a: number, b: number) => {
return a < b ? -1 : a === b ? 0 : 1;
Expand Down Expand Up @@ -191,8 +193,8 @@ const isEqual = (value: any, other: any): boolean => {
export {
isNullOrUndefined,
isMap,
createPayloadResolver,
getAsString,
createPayloadResolver,
compareTo,
getDocumentContext,
lenientJsonParse,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionGroup", () => {
test("inline", async () => {
await assertTransformation(
[
{ o: 1, p: 11, w: "aaa" },
{ o: 1, p: 13, w: "bbb" },
{ o: 1, p: 11, w: "ccc" },
{ o: 2, p: 11, w: "ddd" },
{ o: 2, p: 13, w: "eee" },
{ o: 3, p: 12, w: "fff" },
{ o: 1, p: 9, w: "zzz" },
{ no_o: false, p: 9, w: "zzz" },
],
"$$group(##current.o):$",
{
"": [{ no_o: false, p: 9, w: "zzz" }],
"1": [
{ o: 1, p: 11, w: "aaa" },
{ o: 1, p: 13, w: "bbb" },
{ o: 1, p: 11, w: "ccc" },
{ o: 1, p: 9, w: "zzz" },
],
"2": [
{ o: 2, p: 11, w: "ddd" },
{ o: 2, p: 13, w: "eee" },
],
"3": [{ o: 3, p: 12, w: "fff" }],
},
);
await assertTransformation(
[
["a", 0, 1],
["a", 1, true],
["a", 2, "C"],
["b", 1, 6],
],
"$$group(##current[0]):$",
{
a: [
["a", 0, 1],
["a", 1, true],
["a", 2, "C"],
],
b: [["b", 1, 6]],
},
);

// invalid input will yield an empty object
await assertTransformation(null, "$$group", {});
await assertTransformation(0.5, "$$group(##current[0]):$", {});
await assertTransformation("test", "$$group(##current[0]):$", {});
await assertTransformation(false, "$$group:$", {});
});

test("object", async () => {
await assertTransformation(
[
{ o: 1, p: 11, w: "aaa" },
{ o: 1, p: 13, w: "bbb" },
{ o: 1, p: 11, w: "ccc" },
{ o: 2, p: 11, w: "ddd" },
{ o: 2, p: 13, w: "eee" },
{ o: 3, p: 12, w: "fff" },
{ o: 1, p: 9, w: "zzz" },
{ no_o: false, p: 9, w: "zzz" },
],

{
$$group: "$",
by: "##current.o",
then: [
{
by: {
$$join: ["p_", "##current.p"],
},
order: "DESC",
},
],
},

{
"": {
p_9: [{ no_o: false, p: 9, w: "zzz" }],
},
"1": {
p_9: [{ o: 1, p: 9, w: "zzz" }],
p_13: [{ o: 1, p: 13, w: "bbb" }],
p_11: [
{ o: 1, p: 11, w: "aaa" },
{ o: 1, p: 11, w: "ccc" },
],
},
"2": {
p_13: [{ o: 2, p: 13, w: "eee" }],
p_11: [{ o: 2, p: 11, w: "ddd" }],
},
"3": {
p_12: [{ o: 3, p: 12, w: "fff" }],
},
},
);

await assertTransformation(null, { $$group: "$" }, {});
await assertTransformation(0.5, { $$group: "$" }, {});
await assertTransformation(false, { $$group: "$" }, {});
});

test("objectLazyBy", async () => {
await assertTransformation(
[
{ o: 1, p: 11, w: "aaa" },
{ o: 1, p: 13, w: "bbb" },
{ o: 1, p: 11, w: "ccc" },
{ o: 2, p: 11, w: "ddd" },
{ o: 2, p: 13, w: "eee" },
{ o: 3, p: 12, w: "fff" },
{ o: 1, p: 9, w: "zzz" },
{ no_o: false, p: 9, w: "zzz" },
],

{
$$group: "$",
by: { $$join: ["##current.o", "##current.p"] },
},

{
"111": [
{ o: 1, p: 11, w: "aaa" },
{ o: 1, p: 11, w: "ccc" },
],
"113": [{ o: 1, p: 13, w: "bbb" }],
"19": [{ o: 1, p: 9, w: "zzz" }],
"211": [{ o: 2, p: 11, w: "ddd" }],
"213": [{ o: 2, p: 13, w: "eee" }],
"312": [{ o: 3, p: 12, w: "fff" }],
"9": [{ no_o: false, p: 9, w: "zzz" }],
},
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionIf", () => {
test("arrayForm", async () => {
await assertTransformation(
null,
{
$$if: [true, "b", "c"],
},
"b",
);
await assertTransformation(
null,
{
$$if: [true, "b"],
},
"b",
);
await assertTransformation(
null,
{
$$if: [false, "b"],
},
null,
);
await assertTransformation(
null,
{
$$if: [null, "b"],
},
null,
);

var arr = ["a", "b", "c"];
// $[0] && $[1] -> "d"
await assertTransformation(
arr,
{
$$if: [
"$[0]",
{
$$if: ["$[1]", "d"],
},
],
},
"d",
);

// $[0] || $[1] || $[2] -> "d"
await assertTransformation(
[false, 0, "true"],
{
$$if: ["$[0]", "d", { $$if: ["$[1]", "d", { $$if: ["$[2]", "d"] }] }],
},
"d",
);

var arr2 = ["a", "b", "c"];
await assertTransformation(
arr2,
{
$$if: ["$[0]", "$[1]", "$[2]"],
},
arr2[1],
);

var arr3 = [null, "b", "c"];
await assertTransformation(
arr3,
{
$$if: ["$[0]", "$[1]", "$[2]"],
},
arr3[2],
);

var arr4 = ["false", "b", "c"];
await assertTransformation(
arr4,
{
$$if: ["$$boolean:$[0]", "$[1]", "$[2]"],
},
arr4[2],
);

// invalid input - treat as object
await assertTransformation([true], { $$if: "$" }, null);
await assertTransformation("hello", { $$if: "$" }, null);
});

test("objectForm", async () => {
await assertTransformation(
null,
{
$$if: true,
then: "b",
else: "c",
},
"b",
);
await assertTransformation(
null,
{
$$if: true,
then: "b",
},
"b",
);
await assertTransformation(
null,
{
$$if: false,
then: "b",
},
null,
);
await assertTransformation(
null,
{
$$if: "$",
then: "b",
},
null,
);

var arr = ["a", "b", "c"];
// $[0] && $[1] -> "d"
await assertTransformation(
arr,
{
$$if: "$[0]",
then: { $$if: "$[1]", then: "d" },
},
"d",
);

// $[0] || $[1] || $[2] -> "d"
await assertTransformation(
[false, 0, "true"],
{
$$if: "$[0]",
then: "d",
else: {
$$if: "$[1]",
then: "d",
else: {
$$if: "$[2]",
then: "d",
},
},
},
"d",
);

var arr2 = ["a", "b", "c"];
await assertTransformation(
arr2,
{
$$if: "$[0]",
then: "$[1]",
else: "$[2]",
},
arr2[1],
);

var arr3 = [null, "b", "c"];
await assertTransformation(
arr3,
{
$$if: "$[0]",
then: "$[1]",
else: "$[2]",
},
arr3[2],
);

var arr4 = ["false", "b", "c"];
await assertTransformation(
arr4,
{
$$if: "$$boolean:$[0]",
then: "$[1]",
else: "$[2]",
},
arr4[2],
);
});

test("inline", async () => {
await assertTransformation(true, "$$if(a,b):$", "a");
await assertTransformation(false, "$$if(a,b):$", "b");
await assertTransformation(false, "$$if(a):$", null);
});
});
Loading

0 comments on commit 94e7156

Please sign in to comment.