@olenbetong/appframe-core 2.11.0 → 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.
Files changed (41) hide show
  1. package/dist/esm/af.common.js +121 -135
  2. package/dist/esm/af.common.legacy.js +127 -141
  3. package/dist/esm/af.common.legacy.min.js +1 -1
  4. package/dist/esm/af.common.legacy.min.js.map +4 -4
  5. package/dist/esm/af.common.min.js +2 -2
  6. package/dist/esm/af.common.min.js.map +4 -4
  7. package/dist/iife/af.common.js +121 -135
  8. package/dist/iife/af.common.legacy.js +127 -141
  9. package/dist/iife/af.common.legacy.min.js +1 -1
  10. package/dist/iife/af.common.legacy.min.js.map +4 -4
  11. package/dist/iife/af.common.min.js +2 -2
  12. package/dist/iife/af.common.min.js.map +4 -4
  13. package/es/SessionHandler.js +1 -3
  14. package/es/UserSession.js +33 -29
  15. package/es/date.js +3 -3
  16. package/es/date.test.d.ts +1 -0
  17. package/es/date.test.js +23 -0
  18. package/es/errorHandler.d.ts +1 -0
  19. package/es/errorHandler.js +5 -21
  20. package/es/expose.d.ts +1 -1
  21. package/es/expose.test.d.ts +10 -0
  22. package/es/expose.test.js +36 -0
  23. package/es/getUserImage.test.d.ts +1 -0
  24. package/es/getUserImage.test.js +30 -0
  25. package/es/index.js +1 -1
  26. package/es/invariant.test.d.ts +1 -0
  27. package/es/invariant.test.js +19 -0
  28. package/es/localStorage.js +1 -3
  29. package/es/parseFunc.js +1 -1
  30. package/es/parseFunc.test.d.ts +1 -0
  31. package/es/parseFunc.test.js +14 -0
  32. package/package.json +5 -3
  33. package/src/date.test.ts +27 -0
  34. package/src/date.ts +3 -3
  35. package/src/errorHandler.ts +6 -21
  36. package/src/expose.test.ts +59 -0
  37. package/src/expose.ts +1 -1
  38. package/src/getUserImage.test.ts +50 -0
  39. package/src/invariant.test.ts +23 -0
  40. package/src/parseFunc.test.ts +17 -0
  41. package/src/parseFunc.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-core",
3
- "version": "2.11.0",
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": "8b9f374849381e92b3cde78cbba9a96fde0816ca"
53
+ "gitHead": "14a3bb472e2ad0a25554eed531a4da53a76b428b"
52
54
  }
@@ -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,8 +4,8 @@ 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(2, "0");
7
+ function pad(num: number, digits: number = 2) {
8
+ return String(num).padStart(digits, "0");
9
9
  }
10
10
 
11
11
  /**
@@ -20,7 +20,7 @@ export function toLocalISODateTime(date: Date | null): string | null {
20
20
  return date
21
21
  ? `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(
22
22
  date.getMinutes(),
23
- )}:${pad(date.getSeconds())}.${pad(date.getMilliseconds())}`
23
+ )}:${pad(date.getSeconds())}.${pad(date.getMilliseconds(), 3)}`
24
24
  : null;
25
25
  }
26
26
 
@@ -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
- try {
4
- // Try to avoid error-logging-loop
5
- const data = {
6
- error: event.error?.message ?? event.message ?? null,
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
+ });
@@ -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
  }