Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
larshp committed Sep 12, 2024
1 parent 04d7f3d commit 1c99c9a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
40 changes: 40 additions & 0 deletions packages/runtime/src/builtin/from_mixed.ts
Original file line number Diff line number Diff line change
@@ -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());
}
3 changes: 2 additions & 1 deletion packages/runtime/src/builtin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down
16 changes: 13 additions & 3 deletions test/builtin/from_mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});

});

0 comments on commit 1c99c9a

Please sign in to comment.