Skip to content

Commit

Permalink
TS - add normalize, not, numberformat, numberparse, object, or, pad, …
Browse files Browse the repository at this point in the history
…partition functions
  • Loading branch information
elisherer committed Sep 15, 2024
1 parent 59a0db4 commit b6eb956
Show file tree
Hide file tree
Showing 22 changed files with 948 additions and 17 deletions.
3 changes: 3 additions & 0 deletions javascript/json-transform/src/JsonHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const JSONPATH_ROOT = "$",
JSONPATH_ALT_PREFIX_ESC = "\\#";

const isNullOrUndefined = (value: any): value is null | undefined => value == null || typeof value === "undefined";
const isNullOrEmpty = (value?: string | null): value is null | undefined =>
value == null || typeof value === "undefined" || value === "";
const isMap = (value: any): value is Record<string, any> => value && typeof value === "object" && !Array.isArray(value);

function getAsString<T extends string | number | boolean | object>(value: T): string;
Expand Down Expand Up @@ -349,6 +351,7 @@ function createComparator(type: string | null) {

export {
isNullOrUndefined,
isNullOrEmpty,
isMap,
getAsString,
createPayloadResolver,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionNormalize", () => {
test("inline", async () => {
var strVal = "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ abcABC…";
await assertTransformation(strVal, "$$normalize:$", "This is a funky String abcABC...");
await assertTransformation(strVal, "$$normalize(NFKD):$", "This is a funky String abcABC...");
await assertTransformation(strVal, "$$normalize(NFKD,NONE):$", "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ abcABC...");
await assertTransformation(strVal, "$$normalize(NFD):$", "This is a funky String abcABC…");
await assertTransformation(strVal, "$$normalize(NFD,NONE):$", "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ abcABC…");

// removeMarks on NFC/NFKC does not affect because marks are composed (and gone) before replacement
await assertTransformation(strVal, "$$normalize(NFKC):$", "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ abcABC...");
await assertTransformation(strVal, "$$normalize(NFKC,NONE):$", "Tĥïŝ ĩš â fůňķŷ Šťŕĭńġ abcABC...");
await assertTransformation(strVal, "$$normalize(NFC):$", strVal);
await assertTransformation(strVal, "$$normalize(NFC,NONE):$", strVal);
});

test("unicodeTableBasicLatin", async () => {
var strVal = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
await assertTransformation(strVal, "$$normalize:$", strVal);
});
test("unicodeTableLatinExtendedA", async () => {
var strVal =
"ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſ";
var expect =
"AaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIıIJijJjKkĸLlLlLlLlLlNnNnNnnNnOoOoOoOEoeRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzs";
await assertTransformation(strVal, "$$normalize:$", expect);
});

test("unicodeTableLatinExtendedB", async () => {
var strVal =
"ƀƁƂƃƄƅƆƇƈƉƊƋƌƍƎƏƐƑƒƓƔƕƖƗƘƙƚƛƜƝƞƟƠơƢƣƤƥƦƧƨƩƪƫƬƭƮƯưƱƲƳƴƵƶƷƸƹƺƻƼƽƾƿǀǁǂǃDŽDždžLJLjljNJNjnjǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟǠǡǢǣǤǥǦǧǨǩǪǫǬǭǮǯǰDZDzdzǴǵǶǷǸǹǺǻǼǽǾǿȀȁȂȃȄȅȆȇȈȉȊȋȌȍȎȏȐȑȒȓȔȕȖȗȘșȚțȜȝȞȟȠȡȢȣȤȥȦȧȨȩȪȫȬȭȮȯȰȱȲȳȴȵȶȷȸȹȺȻȼȽȾȿɀɁɂɃɄɅɆɇɈɉɊɋɌɍɎɏ";
var expect =
"bBƂƃbbCCcDDƋƌƍƎƏƐFfGƔhƖIKklƛƜƝƞƟOoƢƣPpRSsƩƪtTtƮUuƱƲYyZzƷƸƹƺƻƼƽƾƿǀǁǂǃDZDzdzLJLjljNJNjnjAaIiOoUuUuUuUuUuǝAaAaAEaeGgGgKkOoOoƷʒjDZDzdzGgHpNnAaAEaeOoAaAaEeEeIiIiOoOoRrRrUuUuSsTtȜȝHhȠdȢȣZzAaEeOoOoOoOoYylntȷȸȹACcLTszɁɂBUAEeJjQqRrYy";
await assertTransformation(strVal, "$$normalize:$", expect);
});

test("unicodeTableLatin1Supplement", async () => {
var strVal = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
var expect = "AAAAAAAECEEEEIIIIDNOOOOO×OUUUUYÞßaaaaaaaeceeeeiiiiðnooooo÷ouuuuyþy";
await assertTransformation(strVal, "$$normalize:$", expect);
});
test("exceptionsAeOe", async () => {
var strVal = "ŒœÆæǢǣǼǽ";
var expect = "OEoeAEaeAEaeAEae";
await assertTransformation(strVal, "$$normalize:$", expect);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionNot", () => {
test("inline", async () => {
await assertTransformation(true, "$$not:$", false);
await assertTransformation(false, "$$not:$", true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";
import { BigDecimal } from "../../functions/common/FunctionHelpers";

describe("TransformerFunctionNumberFormat", () => {
test("inline", async () => {
const decimals = "123456789.87654321";
const val = BigDecimal(decimals);
// must supply type
await assertTransformation(val, "$$numberformat:$", decimals);
await assertTransformation(val, "$$numberformat():$", decimals);
// literal
await assertTransformation(val, "$$numberformat(DECIMAL):$", "123456789.88");
await assertTransformation(val, "$$numberformat(DECIMAL,en-US,'#,##0.00'):$", "123,456,789.88");
await assertTransformation(val, "$$numberformat(DECIMAL,en-US,'#,##0.00','.',','):$", "123.456.789,88");

// literal
await assertTransformation(val, "$$numberformat(CURRENCY):$", "$123,456,789.88");
await assertTransformation(val, "$$numberformat(CURRENCY,en-GB,GBP):$", "£123,456,789.88");
await assertTransformation(val, "$$numberformat(PERCENT):$", "12,345,678,988%");
await assertTransformation(val, "$$numberformat(INTEGER):$", "123,456,790");
await assertTransformation(val, "$$numberformat(COMPACT):$", "123M");
await assertTransformation(val, "$$numberformat(COMPACT,en-US,LONG):$", "123 million");
await assertTransformation(val, "$$numberformat(COMPACT,he-IL,LONG):$", "\u200F123 מיליון");

// BASE
await assertTransformation(val, "$$numberformat(BASE,16):$", "75bcd15");
await assertTransformation(val, "$$numberformat(BASE,2):$", "111010110111100110100010101");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";
import { BigDecimal, DEFAULT_LOCALE } from "../../functions/common/FunctionHelpers";

describe("TransformerFunctionNumberParse", () => {
test("inline", async () => {
const decimals = "123456789.88";
const val = new BigDecimal(decimals);

await assertTransformation(decimals, "$$numberparse:$", val);
await assertTransformation(decimals, "$$numberparse():$", val);
// literal
await assertTransformation("123,456,789.88", "$$numberparse('#,##0.00'):$", val);
await assertTransformation("123.456.789,88", "$$numberparse('#,##0.00',de):$", val);
await assertTransformation("123.456.789,88", "$$numberparse('#,##0.00',en-US,'.',','):$", val);

// HEX
const hex = "75bcd15";
await assertTransformation(hex, "$$numberparse(BASE,16):$", BigDecimal("123456789"));
const binary = "00001010";
await assertTransformation(binary, "$$numberparse(BASE,2):$", BigDecimal("10"));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionObject", () => {
test("inline", async () => {
await assertTransformation(
[
["a", 1],
["b", true],
["c", "C"],
],
"$$object:$",
{
a: 1,
b: true,
c: "C",
},
);
await assertTransformation(
[
[0, 1],
[1, true],
[2, "C"],
],
"$$object:$",
{ 0: 1, 1: true, 2: "C" },
);

// invalid input will yield an empty object
await assertTransformation(null, "$$object", {});
await assertTransformation(0.5, "$$object:$", {});
await assertTransformation("test", "$$object:$", {});
await assertTransformation(false, "$$object:$", {});
});

test("object", async () => {
await assertTransformation(
[
["a", 1],
["b", true],
["c", "C"],
],

{ $$object: "$" },
{
a: 1,
b: true,
c: "C",
},
);
await assertTransformation(
[
[0, 1],
[1, true],
[2, "C"],
],

{ $$object: "$" },
{ 0: 1, 1: true, 2: "C" },
);
await assertTransformation(null, { $$object: "$" }, {});
await assertTransformation(0.5, { $$object: "$" }, {});
await assertTransformation(false, { $$object: "$" }, {});
// explicit

await assertTransformation(
{
key: 1,
value: true,
},
{ $$object: [["$.key", "$.value"]] },
{ 1: true },
);

await assertTransformation(
{
key: "a",
value: 0.5,
},
{ $$object: [["$.key", "$.value"]] },
{ a: 0.5 },
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionOr", () => {
test("assertOr", async () => {
const twoOrThree = {
$$or: [
{ $$is: "$[0]", eq: 2 },
{ $$is: "$[1]", eq: 3 },
],
};
await assertTransformation([2, 3], twoOrThree, true);
await assertTransformation([1, 3], twoOrThree, true);
await assertTransformation([2, 4], twoOrThree, true);
await assertTransformation([1, 4], twoOrThree, false);
const firstOrSecond = { $$or: ["$[0]", "$[1]"] };
await assertTransformation([null, 1], firstOrSecond, true);
await assertTransformation([null, null], firstOrSecond, false);
});

test("inline", async () => {
await assertTransformation([null, 0], "$$or:$", false);
await assertTransformation([1, 0], "$$or:$", true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionPad", () => {
test("inline", async () => {
const strVal = "text";
await assertTransformation(strVal, "$$pad(right,3,2):$", strVal);
await assertTransformation(strVal, "$$pad(end,6):" + strVal, strVal + "00");
await assertTransformation(strVal, "$$pad(end,6):$", strVal + "00");
await assertTransformation(strVal, "$$pad(right,6,0):$", strVal + "00");
await assertTransformation(strVal, "$$pad(end,6,' '):$", strVal + " ");
await assertTransformation(strVal, "$$pad(left,6,' '):$", " " + strVal);
await assertTransformation(strVal, "$$pad(left,6,x):$", "xx" + strVal);
await assertTransformation(strVal, "$$pad(start,6,xy):$", "xy" + strVal);
await assertTransformation(strVal, "$$pad(start,9,xy):$", "xyxyx" + strVal);

// bad inputs
await assertTransformation(strVal, "$$pad:$", strVal);
await assertTransformation(strVal, "$$pad:$", strVal);
await assertTransformation(strVal, "$$pad():$", strVal);
await assertTransformation(strVal, "$$pad(start):$", strVal);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionPartition", () => {
test("inline", async () => {
await assertTransformation([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "$$partition(3):$", [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10],
]);
});

test("object", async () => {
await assertTransformation([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], { $$partition: "$", size: 3 }, [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10],
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionTest", () => {
test("inline", async () => {
const input = "hello";
await assertTransformation(input, "$$test([le]):$", true);
await assertTransformation(input, "$$test(hell):$", true);
await assertTransformation(input, "$$test(hello$):$", true);
await assertTransformation(input, "$$test(hell$):$", false);
await assertTransformation(input, "$$test('^hello$'):$", true);
await assertTransformation(input, "$$test('^(hello|world)$'):$", true);
await assertTransformation("HELLO", "$$test('^hello$'):$", false);
await assertTransformation("HELLO", "$$test('(?i)^hello$'):$", true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, test } from "vitest";
import { assertTransformation } from "../BaseTransformationTest";

describe("TransformerFunctionUpper", () => {
test("uppercase", async () => {
const strVal = "text";
await assertTransformation(strVal, "$$upper:$", strVal.toUpperCase());
await assertTransformation(strVal, "$$upper():$", strVal.toUpperCase());
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import BigNumber from "bignumber.js";
import TransformerFunction from "./common/TransformerFunction";
import { ArgType } from "./common/ArgType";
import FunctionContext from "./common/FunctionContext";
import { FunctionDescription } from "./common/FunctionDescription";
import { getAsString, isNullOrUndefined } from "../JsonHelpers";
import { BigDecimal, MAX_SCALE, MAX_SCALE_ROUNDING } from "./common/FunctionHelpers";
import BigNumber from "bignumber.js";

enum MathOp {
ADDITION = 1,
Expand Down
Loading

0 comments on commit b6eb956

Please sign in to comment.