-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TS - add length, long, lookup function
- Loading branch information
Showing
10 changed files
with
618 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
javascript/json-transform/src/__tests__/JsonHelpers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { mergeInto } from "../JsonHelpers"; | ||
|
||
describe("JsonHelpers", () => { | ||
test("mergeInto - GivenMutuallyExclusiveKeysWithDot", () => { | ||
var root = { | ||
"numbers.roman": { I: 1, II: 2 }, | ||
}; | ||
var mergee = { | ||
"numbers.exist": true, | ||
}; | ||
var expected = { | ||
"numbers.roman": { I: 1, II: 2 }, | ||
"numbers.exist": true, | ||
}; | ||
expect(expected).toEqual(mergeInto(root, mergee, null)); | ||
}); | ||
|
||
test("mergeInto - GivenNoPath", () => { | ||
var root = { | ||
"a.b.c[0]": "foovalue", | ||
}; | ||
var mergee = { | ||
a: { z: "barvalue" }, | ||
}; | ||
var expected = { | ||
a: { z: "barvalue" }, | ||
"a.b.c[0]": "foovalue", | ||
}; | ||
expect(expected).toEqual(mergeInto(root, mergee, null)); | ||
}); | ||
|
||
test("mergeInto - GivenMutuallyExclusiveKeysAndDollarPath", () => { | ||
var root = { | ||
roman: { I: 1, II: 2 }, | ||
}; | ||
var mergee = { | ||
arithmetics: { exist: true }, | ||
symbols: ["I", "V", "X", "L", "C", "D", "M"], | ||
}; | ||
var expected = { | ||
roman: { I: 1, II: 2 }, | ||
arithmetics: { exist: true }, | ||
symbols: ["I", "V", "X", "L", "C", "D", "M"], | ||
}; | ||
expect(expected).toEqual(mergeInto(root, mergee, "$")); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
javascript/json-transform/src/__tests__/functions/TransformerFunctionLength.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { describe, test } from "vitest"; | ||
import { assertTransformation } from "../BaseTransformationTest"; | ||
|
||
describe("TransformerFunctionLength", () => { | ||
test("autoDetect", async () => { | ||
const str = "Hello World"; | ||
await assertTransformation(str, "$$length:hello world", 11); | ||
await assertTransformation(str, "$$length():$", 11); | ||
const arr = ["Hello", "World"]; | ||
await assertTransformation(arr, "$$length:$", 2); | ||
const obj = { a: "Hello", b: "World", c: "foo", d: "bar" }; | ||
await assertTransformation(obj, "$$length:$", 4); | ||
}); | ||
|
||
test("stringOnly", async () => { | ||
const str = "Hello World"; | ||
await assertTransformation(str, "$$length(STRING):hello world", 11); | ||
await assertTransformation(str, "$$length(STRING):$", 11); | ||
const arr = ["Hello", "World"]; | ||
await assertTransformation(arr, "$$length(STRING):$", null); | ||
const obj = { a: "Hello", b: "World", c: "foo", d: "bar" }; | ||
await assertTransformation(obj, "$$length(STRING):$", null); | ||
}); | ||
|
||
test("arrayOnly", async () => { | ||
const str = "Hello World"; | ||
await assertTransformation(str, "$$length(ARRAY):hello world", null); | ||
await assertTransformation(str, "$$length(ARRAY):$", null); | ||
const arr = ["Hello", "World"]; | ||
await assertTransformation(arr, "$$length(ARRAY):$", 2); | ||
const obj = { a: "Hello", b: "World", c: "foo", d: "bar" }; | ||
await assertTransformation(obj, "$$length(ARRAY):$", null); | ||
}); | ||
|
||
test("objectOnly", async () => { | ||
const str = "Hello World"; | ||
await assertTransformation(str, "$$length(OBJECT):hello world", null); | ||
await assertTransformation(str, "$$length(OBJECT):$", null); | ||
const arr = ["Hello", "World"]; | ||
await assertTransformation(arr, "$$length(OBJECT):$", null); | ||
const obj = { a: "Hello", b: "World", c: "foo", d: "bar" }; | ||
await assertTransformation(obj, "$$length(OBJECT):$", 4); | ||
}); | ||
|
||
test("zeroDefault", async () => { | ||
await assertTransformation(null, "$$length:$", null); | ||
await assertTransformation(null, "$$length(AUTO,true):$", 0); | ||
await assertTransformation(42, "$$length(AUTO,true):$", 0); | ||
await assertTransformation(true, "$$length(AUTO,true):$", 0); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
javascript/json-transform/src/__tests__/functions/TransformerFunctionLong.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, test } from "vitest"; | ||
import { assertTransformation } from "../BaseTransformationTest"; | ||
import { BigDecimal } from "../../functions/common/FunctionHelpers"; | ||
import BigNumber from "bignumber.js"; | ||
|
||
describe("TransformerFunctionLong", () => { | ||
test("convert", async () => { | ||
const decimals = "123456789.87654321"; | ||
const longVal = BigInt(new BigDecimal(decimals).toFixed(0, BigNumber.ROUND_DOWN)); | ||
await assertTransformation(decimals, "$$long:$", longVal); | ||
await assertTransformation(decimals, "$$long():$", longVal); | ||
await assertTransformation(123456789.87654321, "$$long:$", longVal); | ||
await assertTransformation(null, "$$long:$", null); | ||
}); | ||
test("maxLong", async () => { | ||
const max = BigInt("9223372036854775807"); // MAX_LONG | ||
await assertTransformation(max, "$$long:$", max); | ||
}); | ||
}); |
139 changes: 139 additions & 0 deletions
139
javascript/json-transform/src/__tests__/functions/TransformerFunctionLookup.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { describe, test } from "vitest"; | ||
import { assertTransformation } from "../BaseTransformationTest"; | ||
import { BigDecimal } from "../../functions/common/FunctionHelpers"; | ||
import BigNumber from "bignumber.js"; | ||
|
||
describe("TransformerFunctionLookup", () => { | ||
test("mergeWithBy", async () => { | ||
await assertTransformation( | ||
{ | ||
a: [ | ||
{ id: 2, a: 1 }, | ||
{ id: 5, a: 2 }, | ||
], | ||
b: [ | ||
{ id: 2, a: "x" }, | ||
{ id: 5, e: true }, | ||
], | ||
}, | ||
|
||
{ | ||
$$lookup: "$.a", | ||
using: [ | ||
{ | ||
with: "$.b", | ||
as: "match", | ||
on: { | ||
$$is: "##current.id", | ||
eq: "##match.id", | ||
}, | ||
}, | ||
], | ||
}, | ||
[ | ||
{ id: 2, a: "x" }, | ||
{ id: 5, a: 2, e: true }, | ||
], | ||
); | ||
|
||
await assertTransformation( | ||
{ | ||
a: [ | ||
{ id: 2, a: 1 }, | ||
{ id: 5, a: 2 }, | ||
], | ||
b: [ | ||
{ key: 2, a: "x" }, | ||
{ key: 5, e: true }, | ||
], | ||
}, | ||
|
||
{ | ||
$$lookup: "$.a", | ||
using: [ | ||
{ | ||
with: "$.b", | ||
as: "match", | ||
on: { | ||
$$is: "##current.id", | ||
eq: "##match.key", | ||
}, | ||
}, | ||
], | ||
}, | ||
[ | ||
{ id: 2, a: "x", key: 2 }, | ||
{ id: 5, a: 2, e: true, key: 5 }, | ||
], | ||
); | ||
}); | ||
|
||
test("mergeWithTo", async () => { | ||
// don't override a, just copy e | ||
await assertTransformation( | ||
{ | ||
a: [ | ||
{ id: 2, a: 1 }, | ||
{ id: 5, a: 2 }, | ||
], | ||
b: [ | ||
{ id: 2, a: "x" }, | ||
{ id: 5, e: true }, | ||
], | ||
}, | ||
|
||
{ | ||
$$lookup: "$.a", | ||
using: [ | ||
{ | ||
with: "$.b", | ||
as: "match", | ||
on: { | ||
$$is: "##current.id", | ||
eq: "##match.id", | ||
}, | ||
}, | ||
], | ||
to: { | ||
"*": "##current", | ||
e: "##match.e", | ||
}, | ||
}, | ||
[ | ||
{ id: 2, a: 1 }, | ||
{ id: 5, a: 2, e: true }, | ||
], | ||
); | ||
|
||
await assertTransformation( | ||
{ | ||
a1: [ | ||
{ id: "aaa", val: "a" }, | ||
{ id: "bbb", val: "b" }, | ||
], | ||
a2: [ | ||
{ name: "aaa", val: "A" }, | ||
{ name: "bbb", val: "B" }, | ||
], | ||
}, | ||
{ | ||
$$lookup: "$.a1", | ||
using: [ | ||
{ | ||
with: "$.a2", | ||
as: "a2", | ||
on: { | ||
$$is: "##current.id", | ||
eq: "##a2.name", | ||
}, | ||
}, | ||
], | ||
to: ["##current.val", "##a2.val"], | ||
}, | ||
[ | ||
["a", "A"], | ||
["b", "B"], | ||
], | ||
); | ||
}); | ||
}); |
Oops, something went wrong.