@bizone-ai/json-transform-utils
Version:
Utilities for handling JSON transformers
181 lines • 6.58 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const convert_1 = require("../../utils/convert");
const definitions_1 = __importDefault(require("../../functions/definitions"));
(0, vitest_1.describe)("convert", () => {
(0, vitest_1.test)("object to inline - test 1", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
$$map: {
$$flat: "$.names",
},
to: "##current.name",
})).toEqual("$$map('##current.name'):$$flat:$.names");
});
(0, vitest_1.test)("object to inline - test 2", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
$$map: {
$$flat: "$.names",
},
to: {
name: "##current.name",
},
})).toEqual({
$$map: "$$flat:$.names",
to: {
name: "##current.name",
},
});
});
(0, vitest_1.test)("object to inline - test 3", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
$$map: "$.names",
to: {
name: "##current.name",
},
})).toEqual({
$$map: "$.names",
to: {
name: "##current.name",
},
});
});
(0, vitest_1.test)("object to inline - test 4", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
$$map: {
$$flat: "$.names",
},
to: "$$substring(0,5):##current.name",
})).toEqual("$$map('$$substring(0,5):##current.name'):$$flat:$.names");
});
(0, vitest_1.test)("object to inline - test 5", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
$$pad: "$.total",
direction: "LEFT",
width: 8,
})).toEqual("$$pad(LEFT,8):$.total");
});
(0, vitest_1.test)("object to inline - test 6", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
$$range: [1, 10, 2],
})).toEqual("$$range(1,10,2)");
});
(0, vitest_1.test)("object to inline - test 7", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
$$math: [1, "/", 2],
})).toEqual("$$math(1,/,2)");
});
vitest_1.test.skip("object to inline - test 8", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
$$math: 1,
op: "/",
op2: 2,
})).toEqual("$$math(1,/,2)");
});
(0, vitest_1.test)("object to inline - test 9", () => {
(0, vitest_1.expect)((0, convert_1.tryConvertFunctionsToInline)({
"*": "$",
full_name: {
$$join: ["$.first_name", "$.last_name"],
delimiter: " ",
},
age: {
$$math: [
{
$$math: [
{
$$date: "#now",
format: "EPOCH",
},
"-",
{
$$date: "$.date_of_birth",
format: "EPOCH",
},
],
},
"//",
{
$$math: [
365,
"*",
{
$$math: [24, "*", 3600],
},
],
},
],
},
})).toEqual({
"*": "$",
age: "$$math('$$math(\\'$$date(EPOCH):#now\\',-,\\'$$date(EPOCH):$.date_of_birth\\')',//,'$$math(365,*,\\'$$math(24,*,3600)\\')')",
full_name: {
$$join: ["$.first_name", "$.last_name"],
delimiter: " ",
},
});
});
// -------------------------------------------
(0, vitest_1.test)("inline to object - test 1", () => {
(0, vitest_1.expect)((0, convert_1.convertFunctionsToObjects)("$$map('##current.name'):$$flat:$.names")).toEqual({
$$map: {
$$flat: "$.names",
},
to: "##current.name",
});
});
(0, vitest_1.test)("inline to object - test 2", () => {
(0, vitest_1.expect)((0, convert_1.convertFunctionsToObjects)("$$reduce('$$math(##accumulator,+,##current.amount)',0):$.items")).toEqual({
$$reduce: "$.items",
identity: "0",
to: {
$$math: ["##accumulator", "+", "##current.amount"],
},
});
});
(0, vitest_1.test)("inline to object - test 3", () => {
(0, vitest_1.expect)((0, convert_1.convertFunctionsToObjects)("$$pad(LEFT,8):$.total")).toEqual({
$$pad: "$.total",
direction: "LEFT",
width: 8,
});
});
(0, vitest_1.test)("inline to object - test 4", () => {
(0, vitest_1.expect)((0, convert_1.convertFunctionsToObjects)("$$range(1,10,2)")).toEqual({
$$range: [1, 10, 2],
});
});
(0, vitest_1.test)("inline to object - test 5", () => {
(0, vitest_1.expect)((0, convert_1.convertFunctionsToObjects)("$$math(1,/,2)")).toEqual({
$$math: [1, "/", 2],
});
});
vitest_1.test.skip("test 999", () => {
// is, lookup, sort
// math, range
const x = [];
Object.keys(definitions_1.default).forEach(def => {
if (!definitions_1.default[def].arguments) {
return;
}
let hasInline = false;
let hasObjectOnly = false;
definitions_1.default[def].arguments?.forEach(arg => {
if (typeof arg.position === "number") {
hasInline = true;
}
else {
hasObjectOnly = true;
}
});
if (hasInline && hasObjectOnly) {
x.push(def);
}
});
(0, vitest_1.expect)(x).toEqual([]);
});
});
//# sourceMappingURL=convert.test.js.map