diff --git a/packages/runtime/src/builtin/from_mixed.ts b/packages/runtime/src/builtin/from_mixed.ts new file mode 100644 index 000000000..be722cad7 --- /dev/null +++ b/packages/runtime/src/builtin/from_mixed.ts @@ -0,0 +1,40 @@ +/* eslint-disable @typescript-eslint/ban-types */ +import {throwError} from "../throw_error"; +import {String} from "../types"; +import {ICharacter} from "../types/_character"; +import {INumeric} from "../types/_numeric"; + +export function from_mixed(input: { + val: ICharacter | string, + sep?: ICharacter | string, + case?: ICharacter | string, + min?: INumeric | number }): String { + + let sep = input.sep; + if (sep === undefined) { + sep = "_"; + } + if (typeof sep !== "string") { + sep = sep.get(); + } + if (sep.length === 0) { + throwError("CX_SY_STRG_PAR_VAL"); + } + + const min = 1; + if (min < 0) { + throwError("CX_SY_STRG_PAR_VAL"); + } + + let val = input.val; + if (typeof val !== "string") { + val = val.get(); + } + +// todo: handle "case" and "min" ? + + const regex = new RegExp(/([A-Z])/, "g"); + val = val.substring(0, 1) + val.substring(1).replace(regex, "_$1"); + + return new String().set(val.toUpperCase()); +} diff --git a/packages/runtime/src/builtin/index.ts b/packages/runtime/src/builtin/index.ts index d72320630..20d460cba 100644 --- a/packages/runtime/src/builtin/index.ts +++ b/packages/runtime/src/builtin/index.ts @@ -13,6 +13,7 @@ export * from "./escape"; export * from "./find"; export * from "./floor"; export * from "./frac"; +export * from "./from_mixed"; export * from "./insert"; export * from "./ipow"; export * from "./lines"; @@ -26,8 +27,8 @@ export * from "./replace"; export * from "./reverse"; export * from "./round"; export * from "./segment"; -export * from "./shift_right"; export * from "./shift_left"; +export * from "./shift_right"; export * from "./sign"; export * from "./sin"; export * from "./sqrt"; diff --git a/test/builtin/from_mixed.ts b/test/builtin/from_mixed.ts index d95a1f47b..8a84c7909 100644 --- a/test/builtin/from_mixed.ts +++ b/test/builtin/from_mixed.ts @@ -14,24 +14,34 @@ describe("Builtin functions - from_mixed", () => { abap = new ABAP({console: new MemoryConsole()}); }); - it.only("test", async () => { + it("test1", async () => { const code = ` WRITE / from_mixed( 'putData' ). +`; + const js = await run(code); + const f = new AsyncFunction("abap", js); + await f(abap); + expect(abap.console.get()).to.equal(`PUT_DATA`); + }); + + it("more testing", async () => { + const code = ` WRITE / from_mixed( 'PutData' ). WRITE / from_mixed( 'PUTDATA' ). WRITE / from_mixed( 'putdata' ). WRITE / from_mixed( 'put data' ). WRITE / from_mixed( 'put Data' ). +WRITE / from_mixed( '_PutData' ). `; const js = await run(code); const f = new AsyncFunction("abap", js); await f(abap); expect(abap.console.get()).to.equal(`PUT_DATA -PUT_DATA P_U_T_D_A_T_A PUTDATA PUT DATA -PUT _DATA`); +PUT _DATA +__PUT_DATA`); }); });