@olenbetong/appframe-core 2.10.2 → 2.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/af.common.js +131 -146
- package/dist/esm/af.common.legacy.js +137 -152
- package/dist/esm/af.common.legacy.min.js +1 -1
- package/dist/esm/af.common.legacy.min.js.map +4 -4
- package/dist/esm/af.common.min.js +2 -2
- package/dist/esm/af.common.min.js.map +4 -4
- package/dist/iife/af.common.js +131 -146
- package/dist/iife/af.common.legacy.js +137 -152
- package/dist/iife/af.common.legacy.min.js +1 -1
- package/dist/iife/af.common.legacy.min.js.map +4 -4
- package/dist/iife/af.common.min.js +2 -2
- package/dist/iife/af.common.min.js.map +4 -4
- package/es/SessionHandler.js +1 -3
- package/es/UserSession.js +33 -29
- package/es/date.d.ts +16 -0
- package/es/date.js +15 -3
- package/es/date.test.d.ts +1 -0
- package/es/date.test.js +23 -0
- package/es/downloadRequest.js +6 -11
- package/es/errorHandler.d.ts +1 -0
- package/es/errorHandler.js +5 -21
- package/es/expose.d.ts +1 -1
- package/es/expose.test.d.ts +10 -0
- package/es/expose.test.js +36 -0
- package/es/getUserImage.test.d.ts +1 -0
- package/es/getUserImage.test.js +30 -0
- package/es/index.d.ts +1 -1
- package/es/index.js +2 -2
- package/es/invariant.test.d.ts +1 -0
- package/es/invariant.test.js +19 -0
- package/es/localStorage.js +1 -3
- package/es/parseFunc.js +1 -1
- package/es/parseFunc.test.d.ts +1 -0
- package/es/parseFunc.test.js +14 -0
- package/package.json +5 -3
- package/src/date.test.ts +27 -0
- package/src/date.ts +22 -3
- package/src/downloadRequest.ts +6 -12
- package/src/errorHandler.ts +6 -21
- package/src/expose.test.ts +59 -0
- package/src/expose.ts +1 -1
- package/src/getUserImage.test.ts +50 -0
- package/src/index.ts +1 -1
- package/src/invariant.test.ts +23 -0
- package/src/parseFunc.test.ts +17 -0
- package/src/parseFunc.ts +1 -1
package/es/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { UserSession } from "./UserSession.js";
|
|
|
2
2
|
import "./errorHandler.js";
|
|
3
3
|
import afLocalStorage from "./localStorage.js";
|
|
4
4
|
export { default as mitt } from "mitt";
|
|
5
|
-
export { toLocalISODate, toLocalISODateTime } from "./date.js";
|
|
5
|
+
export { toLocalISODate, toLocalISODateTime, toLocalDateTime } from "./date.js";
|
|
6
6
|
export { downloadBlob } from "./downloadBlob.js";
|
|
7
7
|
export { downloadRequest } from "./downloadRequest.js";
|
|
8
8
|
export { expose } from "./expose.js";
|
|
@@ -17,4 +17,4 @@ export { default as SessionHandler } from "./SessionHandler.js";
|
|
|
17
17
|
export { UserSession };
|
|
18
18
|
export { getSettings, saveSettings } from "./settings.js";
|
|
19
19
|
export { alert, confirm, ConfirmButtons, openAlertDialog, waitForDialog } from "./alert.js";
|
|
20
|
-
export const version = "2.
|
|
20
|
+
export const version = "2.11.1";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { invariant } from "./invariant";
|
|
3
|
+
describe("invariant", () => {
|
|
4
|
+
test("should throw error when condition is false", () => {
|
|
5
|
+
expect(() => {
|
|
6
|
+
invariant(false, "Test message");
|
|
7
|
+
}).toThrowError("Test message");
|
|
8
|
+
});
|
|
9
|
+
test("should throw error when condition is false and message is a function", () => {
|
|
10
|
+
expect(() => {
|
|
11
|
+
invariant(false, () => "Test message");
|
|
12
|
+
}).toThrowError("Test message");
|
|
13
|
+
});
|
|
14
|
+
test("should not throw error when condition is true", () => {
|
|
15
|
+
expect(() => {
|
|
16
|
+
invariant(true, "Test message");
|
|
17
|
+
}).not.toThrowError();
|
|
18
|
+
});
|
|
19
|
+
});
|
package/es/localStorage.js
CHANGED
|
@@ -2,9 +2,7 @@ function getLocalKey(key) {
|
|
|
2
2
|
return [af?.article?.id || "", "common", key].join(".");
|
|
3
3
|
}
|
|
4
4
|
class LocalStorage {
|
|
5
|
-
|
|
6
|
-
this.isPersisted = () => true;
|
|
7
|
-
}
|
|
5
|
+
isPersisted = () => true;
|
|
8
6
|
getLocalStorageKey(key, global) {
|
|
9
7
|
return global ? ["common", key].join(".") : getLocalKey(key);
|
|
10
8
|
}
|
package/es/parseFunc.js
CHANGED
|
@@ -3,5 +3,5 @@ const funcRegex = /^function(?:\s+([a-zA-Z$_][a-zA-Z0-9$_]*))?\s*\(((?:(?:\s*[a-
|
|
|
3
3
|
export function parseFunc(func) {
|
|
4
4
|
var match = func.toString().match(funcRegex);
|
|
5
5
|
invariant(match, "Unable to parse function!");
|
|
6
|
-
return { name: match[1], args: match[2].split(","), body: match[3] };
|
|
6
|
+
return { name: func.name ?? match[1], args: match[2].split(",").map((a) => a.trim()), body: match[3] };
|
|
7
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { parseFunc } from "./parseFunc";
|
|
3
|
+
function testFunction(arg1, arg2) {
|
|
4
|
+
console.log(arg1, arg2);
|
|
5
|
+
}
|
|
6
|
+
describe("parseFunc", () => {
|
|
7
|
+
test("should parse function", () => {
|
|
8
|
+
expect(parseFunc(testFunction)).toEqual({
|
|
9
|
+
name: "testFunction",
|
|
10
|
+
args: ["arg1", "arg2"],
|
|
11
|
+
body: "\n console.log(arg1, arg2);\n",
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/appframe-core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.11.1",
|
|
4
4
|
"description": "Javascript libraries to utilize the Appframe framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": [
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"types": "./es/index.d.ts",
|
|
16
16
|
"scripts": {
|
|
17
|
-
"prepack": "pnpm run build",
|
|
17
|
+
"prepack": "bun test && pnpm run build",
|
|
18
18
|
"build": "pnpm run build:esbuild && pnpm run build:tsc",
|
|
19
19
|
"build:esbuild": "node --loader ts-node/esm.mjs ../../shared/esbuild.ts",
|
|
20
20
|
"build:tsc": " rm -rf ./es && tsc && node ./scripts/replace-version.js"
|
|
@@ -43,10 +43,12 @@
|
|
|
43
43
|
]
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
+
"@happy-dom/global-registrator": "^14.12.3",
|
|
46
47
|
"mitt": "^3.0.1"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
|
50
|
+
"@types/bun": "^1.1.5",
|
|
49
51
|
"chalk": "^5.3.0"
|
|
50
52
|
},
|
|
51
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "14a3bb472e2ad0a25554eed531a4da53a76b428b"
|
|
52
54
|
}
|
package/src/date.test.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { toLocalDateTime, toLocalISODate, toLocalISODateTime } from "./date.js";
|
|
4
|
+
|
|
5
|
+
describe("date", () => {
|
|
6
|
+
test("toLocalISODate", () => {
|
|
7
|
+
expect(toLocalISODate(new Date("2020-01-01T12:00:00Z"))).toBe("2020-01-01");
|
|
8
|
+
expect(toLocalISODate(new Date("2020-01-01T23:00:00Z"))).toBe("2020-01-01");
|
|
9
|
+
expect(toLocalISODate(new Date("2020-01-01T01:00:00Z"))).toBe("2020-01-01");
|
|
10
|
+
expect(toLocalISODate(null)).toBe(null);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("toLocalISODateTime", () => {
|
|
14
|
+
expect(toLocalISODateTime(new Date("2020-01-01T12:00:00.001Z"))).toBe("2020-01-01T12:00:00.001");
|
|
15
|
+
expect(toLocalISODateTime(new Date("2020-01-01T23:00:00.001Z"))).toBe("2020-01-01T23:00:00.001");
|
|
16
|
+
expect(toLocalISODateTime(new Date("2020-01-01T01:00:00.001Z"))).toBe("2020-01-01T01:00:00.001");
|
|
17
|
+
expect(toLocalISODateTime(null)).toBe(null);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("toLocalDateTime", () => {
|
|
21
|
+
expect(toLocalDateTime(new Date("2020-01-01T12:00:00.001Z"))).toBe("2020-01-01T12:00");
|
|
22
|
+
expect(toLocalDateTime(new Date("2020-01-01T23:00:00.001Z"))).toBe("2020-01-01T23:00");
|
|
23
|
+
expect(toLocalDateTime(new Date("2020-01-01T01:00:00.001Z"))).toBe("2020-01-01T01:00");
|
|
24
|
+
expect(toLocalDateTime(new Date("2020-01-01T12:00:00Z"))).toBe("2020-01-01T12:00");
|
|
25
|
+
expect(toLocalDateTime(null)).toBe("");
|
|
26
|
+
});
|
|
27
|
+
});
|
package/src/date.ts
CHANGED
|
@@ -4,16 +4,35 @@ export function toLocalISODate(date: Date | null): string | null {
|
|
|
4
4
|
return date ? toLocalISODateTime(date).substring(0, 10) : null;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
function pad(num: number) {
|
|
8
|
-
return String(num).padStart(
|
|
7
|
+
function pad(num: number, digits: number = 2) {
|
|
8
|
+
return String(num).padStart(digits, "0");
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Returns an ISO string (yyyy-MM-ddTHH:mm:ss.SSS) representing the local time
|
|
13
|
+
* of the date object.
|
|
14
|
+
*
|
|
15
|
+
* @param date
|
|
16
|
+
*/
|
|
11
17
|
export function toLocalISODateTime(date: Date): string;
|
|
12
18
|
export function toLocalISODateTime(date: Date | null): string | null;
|
|
13
19
|
export function toLocalISODateTime(date: Date | null): string | null {
|
|
14
20
|
return date
|
|
15
21
|
? `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(
|
|
16
22
|
date.getMinutes(),
|
|
17
|
-
|
|
23
|
+
)}:${pad(date.getSeconds())}.${pad(date.getMilliseconds(), 3)}`
|
|
18
24
|
: null;
|
|
19
25
|
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Converts a date object to a string compatible with input type datetime-local.
|
|
29
|
+
* Will handle the UTC value of the date as if it was local, to be compatible with
|
|
30
|
+
* data objects.
|
|
31
|
+
*
|
|
32
|
+
* If null or undefined is passed, returns an empty string.
|
|
33
|
+
*
|
|
34
|
+
* @param date
|
|
35
|
+
*/
|
|
36
|
+
export function toLocalDateTime(date?: Date | null): string {
|
|
37
|
+
return date ? date.toISOString().substring(0, 16) : "";
|
|
38
|
+
}
|
package/src/downloadRequest.ts
CHANGED
|
@@ -14,15 +14,9 @@ export type DownloadParameters = {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
export async function downloadRequest(request: Request, { fileName, signal, onProgress }: DownloadParameters) {
|
|
17
|
-
function reportProgress(progress: DownloadItem) {
|
|
18
|
-
if (typeof onProgress === "function") {
|
|
19
|
-
onProgress({ ...progress });
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
17
|
let bytesReceived = 0;
|
|
24
18
|
|
|
25
|
-
|
|
19
|
+
onProgress?.({ status: "requested" });
|
|
26
20
|
|
|
27
21
|
try {
|
|
28
22
|
let response = await fetch(request);
|
|
@@ -47,7 +41,7 @@ export async function downloadRequest(request: Request, { fileName, signal, onPr
|
|
|
47
41
|
}
|
|
48
42
|
|
|
49
43
|
bytesReceived += read.value.length;
|
|
50
|
-
|
|
44
|
+
onProgress?.({ bytesReceived, status: "downloading" });
|
|
51
45
|
controller.enqueue(read.value);
|
|
52
46
|
|
|
53
47
|
return pump();
|
|
@@ -63,16 +57,16 @@ export async function downloadRequest(request: Request, { fileName, signal, onPr
|
|
|
63
57
|
|
|
64
58
|
const blob = await response.blob();
|
|
65
59
|
|
|
66
|
-
|
|
60
|
+
onProgress?.({ bytesReceived, status: "downloaded" });
|
|
67
61
|
|
|
68
62
|
await downloadBlob(blob, fileName);
|
|
69
63
|
|
|
70
|
-
|
|
64
|
+
onProgress?.({ bytesReceived, status: "completed" });
|
|
71
65
|
} catch (err) {
|
|
72
66
|
if (signal?.aborted) {
|
|
73
|
-
|
|
67
|
+
onProgress?.({ status: "cancelled" });
|
|
74
68
|
} else {
|
|
75
|
-
|
|
69
|
+
onProgress?.({ error: (err as Error).message, status: "error" });
|
|
76
70
|
}
|
|
77
71
|
}
|
|
78
72
|
}
|
package/src/errorHandler.ts
CHANGED
|
@@ -1,26 +1,11 @@
|
|
|
1
|
+
import { logError } from "./log.js";
|
|
2
|
+
|
|
1
3
|
if (process.env.NODE_ENV === "production") {
|
|
2
4
|
globalThis.window?.addEventListener("error", (event) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
file: event.filename,
|
|
8
|
-
line: event.lineno ?? 0,
|
|
9
|
-
column: event.colno ?? 0,
|
|
10
|
-
stack: event.error?.stack ?? null,
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
fetch("/data/Logger/LogError", {
|
|
14
|
-
body: JSON.stringify(data),
|
|
15
|
-
headers: {
|
|
16
|
-
Accept: "application/json",
|
|
17
|
-
"Content-Type": "application/json; charset=utf-8",
|
|
18
|
-
"X-Requested-With": "XMLHttpRequest",
|
|
19
|
-
},
|
|
20
|
-
method: "POST",
|
|
21
|
-
});
|
|
22
|
-
} catch (ex) {
|
|
23
|
-
// ignore
|
|
5
|
+
if (typeof event === "object" && "error" in event) {
|
|
6
|
+
logError(event.error);
|
|
7
|
+
} else if (typeof event === "object" && "message" in event) {
|
|
8
|
+
logError(event);
|
|
24
9
|
}
|
|
25
10
|
});
|
|
26
11
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
|
|
2
|
+
|
|
3
|
+
import { describe, expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { expose } from "./expose.js";
|
|
6
|
+
|
|
7
|
+
GlobalRegistrator.register();
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
var test: {
|
|
11
|
+
value?: string;
|
|
12
|
+
func: () => string;
|
|
13
|
+
nested: {
|
|
14
|
+
func: () => string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("expose", () => {
|
|
20
|
+
test("should expose function to global scope", () => {
|
|
21
|
+
function func() {
|
|
22
|
+
return "test";
|
|
23
|
+
}
|
|
24
|
+
expose("test", func);
|
|
25
|
+
|
|
26
|
+
expect(globalThis.test.func).toBe(func);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("should expose function to global scope with nested name", () => {
|
|
30
|
+
function func() {
|
|
31
|
+
return "test";
|
|
32
|
+
}
|
|
33
|
+
expose("test.nested", func);
|
|
34
|
+
|
|
35
|
+
expect(globalThis.test.nested.func).toBe(func);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("should not overwrite existing without overwrite set to true", () => {
|
|
39
|
+
let value1 = "test1";
|
|
40
|
+
let value2 = "test2";
|
|
41
|
+
|
|
42
|
+
expose("test.value", value1);
|
|
43
|
+
expect(globalThis.test.value).toBe(value1);
|
|
44
|
+
|
|
45
|
+
expose("test.value", value2);
|
|
46
|
+
expect(globalThis.test.value).not.toBe(value2);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("should overwrite existing with overwrite set to true", () => {
|
|
50
|
+
let value1 = "test1";
|
|
51
|
+
let value2 = "test2";
|
|
52
|
+
|
|
53
|
+
expose("test.value", value1);
|
|
54
|
+
expect(globalThis.test.value).toBe(value1);
|
|
55
|
+
|
|
56
|
+
expose("test.value", value2, { overwrite: true });
|
|
57
|
+
expect(globalThis.test.value).toBe(value2);
|
|
58
|
+
});
|
|
59
|
+
});
|
package/src/expose.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type ExposeOptions = {
|
|
|
17
17
|
*/
|
|
18
18
|
export function expose(
|
|
19
19
|
name: object | string,
|
|
20
|
-
value: object | Function | Function[],
|
|
20
|
+
value: object | Function | Function[] | string | number,
|
|
21
21
|
options: ExposeOptions = {},
|
|
22
22
|
): void {
|
|
23
23
|
if (Array.isArray(value)) {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { getUserImage } from "./getUserImage";
|
|
4
|
+
|
|
5
|
+
describe("getUserImage", () => {
|
|
6
|
+
test("should return empty string when primKey is null", () => {
|
|
7
|
+
expect(getUserImage(null, "login", "hash")).toEqual("");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test("should return empty string when hash is null", () => {
|
|
11
|
+
expect(getUserImage("primKey", "login", null)).toEqual("");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("should return empty string when primKey and hash are null", () => {
|
|
15
|
+
expect(getUserImage(null, "login", null)).toEqual("");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("should return empty string when primKey and hash are not provided", () => {
|
|
19
|
+
expect(getUserImage(null, "login", null)).toEqual("");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("should return empty string when primKey and hash are not provided", () => {
|
|
23
|
+
expect(getUserImage(null, "login", null)).toEqual("");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("should return empty string when primKey and hash are not provided", () => {
|
|
27
|
+
expect(getUserImage(null, "login", null)).toEqual("");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("should return valid image path when all values are provided", () => {
|
|
31
|
+
expect(getUserImage("primKey", "login", "hash")).toEqual(
|
|
32
|
+
"/api/data/blob/EmployeesLookup/primKey/Image/login.hash.jpg?width=64",
|
|
33
|
+
);
|
|
34
|
+
expect(getUserImage("primKey", "login", "hash", "xs")).toEqual(
|
|
35
|
+
"/api/data/blob/EmployeesLookup/primKey/Image/login.hash.jpg?width=64",
|
|
36
|
+
);
|
|
37
|
+
expect(getUserImage("primKey", "login", "hash", "sm")).toEqual(
|
|
38
|
+
"/api/data/blob/EmployeesLookup/primKey/Image/login.hash.jpg?width=128",
|
|
39
|
+
);
|
|
40
|
+
expect(getUserImage("primKey", "login", "hash", "md")).toEqual(
|
|
41
|
+
"/api/data/blob/EmployeesLookup/primKey/Image/login.hash.jpg?width=256",
|
|
42
|
+
);
|
|
43
|
+
expect(getUserImage("primKey", "login", "hash", "lg")).toEqual(
|
|
44
|
+
"/api/data/blob/EmployeesLookup/primKey/Image/login.hash.jpg?width=512",
|
|
45
|
+
);
|
|
46
|
+
expect(getUserImage("primKey", "login", "hash", "xl")).toEqual(
|
|
47
|
+
"/api/data/blob/EmployeesLookup/primKey/Image/login.hash.jpg",
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { localize } from "./localize.js";
|
|
|
8
8
|
import { getSessionData, logActivity, logError, startActivity, time } from "./log.js";
|
|
9
9
|
|
|
10
10
|
export { default as mitt } from "mitt";
|
|
11
|
-
export { toLocalISODate, toLocalISODateTime } from "./date.js";
|
|
11
|
+
export { toLocalISODate, toLocalISODateTime, toLocalDateTime } from "./date.js";
|
|
12
12
|
export { downloadBlob } from "./downloadBlob.js";
|
|
13
13
|
export { downloadRequest } from "./downloadRequest.js";
|
|
14
14
|
export type { DownloadParameters, DownloadItem } from "./downloadRequest.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { invariant } from "./invariant";
|
|
4
|
+
|
|
5
|
+
describe("invariant", () => {
|
|
6
|
+
test("should throw error when condition is false", () => {
|
|
7
|
+
expect(() => {
|
|
8
|
+
invariant(false, "Test message");
|
|
9
|
+
}).toThrowError("Test message");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("should throw error when condition is false and message is a function", () => {
|
|
13
|
+
expect(() => {
|
|
14
|
+
invariant(false, () => "Test message");
|
|
15
|
+
}).toThrowError("Test message");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("should not throw error when condition is true", () => {
|
|
19
|
+
expect(() => {
|
|
20
|
+
invariant(true, "Test message");
|
|
21
|
+
}).not.toThrowError();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { parseFunc } from "./parseFunc";
|
|
4
|
+
|
|
5
|
+
function testFunction(arg1: string, arg2: string) {
|
|
6
|
+
console.log(arg1, arg2);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe("parseFunc", () => {
|
|
10
|
+
test("should parse function", () => {
|
|
11
|
+
expect(parseFunc(testFunction)).toEqual({
|
|
12
|
+
name: "testFunction",
|
|
13
|
+
args: ["arg1", "arg2"],
|
|
14
|
+
body: "\n console.log(arg1, arg2);\n",
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
});
|
package/src/parseFunc.ts
CHANGED
|
@@ -6,5 +6,5 @@ const funcRegex =
|
|
|
6
6
|
export function parseFunc(func: Function) {
|
|
7
7
|
var match = func.toString().match(funcRegex);
|
|
8
8
|
invariant(match, "Unable to parse function!");
|
|
9
|
-
return { name: match[1], args: match[2].split(","), body: match[3] };
|
|
9
|
+
return { name: func.name ?? match[1], args: match[2].split(",").map((a) => a.trim()), body: match[3] };
|
|
10
10
|
}
|