@olenbetong/appframe-core 2.4.4 → 2.5.0

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.
@@ -1,35 +1,35 @@
1
1
  export type ImageSize = "xs" | "sm" | "md" | "lg" | "xl";
2
2
 
3
3
  export interface getUserImageOptions {
4
- /**
5
- * Prim key of the record in atbl_Personnel_Employees
6
- */
7
- primKey: string | null;
8
- /**
9
- * Person ID of the employee
10
- */
11
- login: string | null;
12
- /**
13
- * The value of the ImageHash column in atbl_Personnel_Employees
14
- */
15
- hash: string | number | null;
16
- /**
17
- * The image will be resized according to this value. Default is 'xs':
18
- * - xs: 64px
19
- * - sm: 128px
20
- * - md: 256px
21
- * - lg: 512px
22
- * - xl: image is not resized
23
- */
24
- size?: ImageSize;
4
+ /**
5
+ * Prim key of the record in atbl_Personnel_Employees
6
+ */
7
+ primKey: string | null;
8
+ /**
9
+ * Person ID of the employee
10
+ */
11
+ login: string | null;
12
+ /**
13
+ * The value of the ImageHash column in atbl_Personnel_Employees
14
+ */
15
+ hash: string | number | null;
16
+ /**
17
+ * The image will be resized according to this value. Default is 'xs':
18
+ * - xs: 64px
19
+ * - sm: 128px
20
+ * - md: 256px
21
+ * - lg: 512px
22
+ * - xl: image is not resized
23
+ */
24
+ size?: ImageSize;
25
25
  }
26
26
 
27
27
  const sizeMap: Record<ImageSize, number | undefined> = {
28
- xs: 64,
29
- sm: 128,
30
- md: 256,
31
- lg: 512,
32
- xl: undefined,
28
+ xs: 64,
29
+ sm: 128,
30
+ md: 256,
31
+ lg: 512,
32
+ xl: undefined,
33
33
  };
34
34
 
35
35
  /**
@@ -39,28 +39,28 @@ const sizeMap: Record<ImageSize, number | undefined> = {
39
39
  * If one of primKey or hash are not provided, or are null, an empty string will be returned.
40
40
  */
41
41
  export function getUserImage(
42
- primKey: string | null,
43
- login: string | null,
44
- hash: string | number | null,
45
- size?: ImageSize
42
+ primKey: string | null,
43
+ login: string | null,
44
+ hash: string | number | null,
45
+ size?: ImageSize
46
46
  ): string;
47
47
  export function getUserImage(options: getUserImageOptions): string;
48
48
  export function getUserImage(
49
- primKeyOrOptions: string | null | getUserImageOptions,
50
- login?: string | null,
51
- hash?: string | number | null,
52
- size: ImageSize = "xs"
49
+ primKeyOrOptions: string | null | getUserImageOptions,
50
+ login?: string | null,
51
+ hash?: string | number | null,
52
+ size: ImageSize = "xs"
53
53
  ) {
54
- if (primKeyOrOptions && typeof primKeyOrOptions === "object") {
55
- return getUserImage(primKeyOrOptions.primKey, primKeyOrOptions.login, primKeyOrOptions.hash, primKeyOrOptions.size);
56
- }
54
+ if (primKeyOrOptions && typeof primKeyOrOptions === "object") {
55
+ return getUserImage(primKeyOrOptions.primKey, primKeyOrOptions.login, primKeyOrOptions.hash, primKeyOrOptions.size);
56
+ }
57
57
 
58
- if (!primKeyOrOptions || !hash) {
59
- return "";
60
- }
58
+ if (!primKeyOrOptions || !hash) {
59
+ return "";
60
+ }
61
61
 
62
- let sizePx = sizeMap[size];
63
- return `/api/data/blob/EmployeesLookup/${primKeyOrOptions}/Image/${login}${login ? "." : ""}${hash}.jpg${
64
- sizePx ? `?width=${sizePx}` : ""
65
- }`;
62
+ let sizePx = sizeMap[size];
63
+ return `/api/data/blob/EmployeesLookup/${primKeyOrOptions}/Image/${login}${login ? "." : ""}${hash}.jpg${
64
+ sizePx ? `?width=${sizePx}` : ""
65
+ }`;
66
66
  }
package/src/has.ts CHANGED
@@ -1,6 +1,3 @@
1
- export function has(
2
- obj: any,
3
- key: string | number | symbol
4
- ): boolean {
5
- return Object.prototype.hasOwnProperty.call(obj, key);
1
+ export function has(obj: any, key: string | number | symbol): boolean {
2
+ return Object.prototype.hasOwnProperty.call(obj, key);
6
3
  }
package/src/index.ts CHANGED
@@ -1,10 +1,9 @@
1
+ import { UserSession } from "./UserSession.js";
1
2
  import "./errorHandler.js";
2
3
  import { expose } from "./expose.js";
3
4
  import { getLocalizedString } from "./getLocalizedString.js";
4
5
  import { localize } from "./localize.js";
5
6
 
6
- import { UserSession } from "./UserSession.js";
7
-
8
7
  export { default as mitt } from "mitt";
9
8
  export * from "./date.js";
10
9
  export { downloadBlob } from "./downloadBlob.js";
@@ -23,36 +22,36 @@ export * from "./settings.js";
23
22
  export const version = __VERSION__;
24
23
 
25
24
  declare global {
26
- var __VERSION__: string;
27
- var __DEV__: number;
28
- var af: AfCore;
25
+ var __VERSION__: string;
26
+ var __DEV__: number;
27
+ var af: AfCore;
29
28
 
30
- interface Window {
31
- af: AfCore;
32
- }
29
+ interface Window {
30
+ af: AfCore;
31
+ }
33
32
  }
34
33
 
35
34
  export interface AfArticle {
36
- hostName: string;
37
- i18n: Record<string, string>;
38
- id: string;
35
+ hostName: string;
36
+ i18n: Record<string, string>;
37
+ id: string;
39
38
  }
40
39
 
41
40
  export interface AfCore {
42
- article: AfArticle;
43
- common: {
44
- getLocalizedString: typeof getLocalizedString;
45
- expose: typeof expose;
46
- localize: typeof localize;
47
- };
48
- controls?: {
49
- alert?: (message: string, callback?: () => void) => void;
50
- confirm?: (options: {
51
- title: string;
52
- message: string;
53
- buttons: string[];
54
- callback: (button: number) => void;
55
- }) => void;
56
- };
57
- userSession: UserSession;
41
+ article: AfArticle;
42
+ common: {
43
+ getLocalizedString: typeof getLocalizedString;
44
+ expose: typeof expose;
45
+ localize: typeof localize;
46
+ };
47
+ controls?: {
48
+ alert?: (message: string, callback?: () => void) => void;
49
+ confirm?: (options: {
50
+ title: string;
51
+ message: string;
52
+ buttons: string[];
53
+ callback: (button: number) => void;
54
+ }) => void;
55
+ };
56
+ userSession: UserSession;
58
57
  }
package/src/invariant.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export function invariant(condition: any, message?: string | (() => string)): asserts condition {
2
- if (condition) {
3
- return;
4
- }
5
- const provided: string | undefined = typeof message === "function" ? message() : message;
2
+ if (condition) {
3
+ return;
4
+ }
5
+ const provided: string | undefined = typeof message === "function" ? message() : message;
6
6
 
7
- throw new Error(provided ?? "Invariant failed");
7
+ throw new Error(provided ?? "Invariant failed");
8
8
  }
@@ -1,49 +1,46 @@
1
1
  function getLocalKey(key: string): string {
2
- return [af?.article?.id || "", "common", key].join(".");
2
+ return [af?.article?.id || "", "common", key].join(".");
3
3
  }
4
4
 
5
5
  class LocalStorage {
6
- isPersisted() {
7
- return true;
8
- }
9
-
10
- getLocalStorageKey(key: string, global?: boolean) {
11
- return global ? ["common", key].join(".") : getLocalKey(key);
12
- }
13
-
14
- get(key: string, defaultValue: unknown, global?: boolean) {
15
- const lsKey = this.getLocalStorageKey(key, global);
16
- const value = localStorage.getItem(lsKey);
17
-
18
- return value !== null ? JSON.parse(value) : defaultValue;
19
- }
20
-
21
- set(key: string, value: unknown, global?: boolean) {
22
- localStorage.setItem(
23
- this.getLocalStorageKey(key, global),
24
- JSON.stringify(value)
25
- );
26
- }
27
-
28
- remove(key: string, global?: boolean) {
29
- const lsKey = this.getLocalStorageKey(key, global);
30
- if (localStorage.getItem(lsKey) !== null) {
31
- localStorage.removeItem(lsKey);
32
- return true;
33
- } else {
34
- return false;
35
- }
36
- }
37
-
38
- clear() {
39
- const baseKey = getLocalKey("");
40
-
41
- for (let key in localStorage) {
42
- if (key.slice(0, baseKey.length) === baseKey) {
43
- localStorage.removeItem(key);
44
- }
45
- }
46
- }
6
+ isPersisted() {
7
+ return true;
8
+ }
9
+
10
+ getLocalStorageKey(key: string, global?: boolean) {
11
+ return global ? ["common", key].join(".") : getLocalKey(key);
12
+ }
13
+
14
+ get(key: string, defaultValue: unknown, global?: boolean) {
15
+ const lsKey = this.getLocalStorageKey(key, global);
16
+ const value = localStorage.getItem(lsKey);
17
+
18
+ return value !== null ? JSON.parse(value) : defaultValue;
19
+ }
20
+
21
+ set(key: string, value: unknown, global?: boolean) {
22
+ localStorage.setItem(this.getLocalStorageKey(key, global), JSON.stringify(value));
23
+ }
24
+
25
+ remove(key: string, global?: boolean) {
26
+ const lsKey = this.getLocalStorageKey(key, global);
27
+ if (localStorage.getItem(lsKey) !== null) {
28
+ localStorage.removeItem(lsKey);
29
+ return true;
30
+ } else {
31
+ return false;
32
+ }
33
+ }
34
+
35
+ clear() {
36
+ const baseKey = getLocalKey("");
37
+
38
+ for (let key in localStorage) {
39
+ if (key.slice(0, baseKey.length) === baseKey) {
40
+ localStorage.removeItem(key);
41
+ }
42
+ }
43
+ }
47
44
  }
48
45
 
49
46
  export default new LocalStorage();
package/src/localize.ts CHANGED
@@ -15,17 +15,17 @@ import { invariant } from "./invariant.js";
15
15
  * @returns Translated string
16
16
  */
17
17
  export function localize(stringParts: TemplateStringsArray, ...args: any[]): string {
18
- invariant(
19
- Array.isArray(stringParts) && stringParts.length === args.length + 1,
20
- "Method 'localize' must be used as a tag function"
21
- );
18
+ invariant(
19
+ Array.isArray(stringParts) && stringParts.length === args.length + 1,
20
+ "Method 'localize' must be used as a tag function"
21
+ );
22
22
 
23
- let stringToLocalize = stringParts.map((s, i) => (i === stringParts.length - 1 ? s : `${s}\$${i}`)).join("");
24
- let localized = getLocalizedString(stringToLocalize);
23
+ let stringToLocalize = stringParts.map((s, i) => (i === stringParts.length - 1 ? s : `${s}\$${i}`)).join("");
24
+ let localized = getLocalizedString(stringToLocalize);
25
25
 
26
- args.forEach((arg, index) => {
27
- localized = localized.replace("$" + index, arg);
28
- });
26
+ args.forEach((arg, index) => {
27
+ localized = localized.replace("$" + index, arg);
28
+ });
29
29
 
30
- return localized;
30
+ return localized;
31
31
  }
package/src/parseFunc.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { invariant } from "./invariant.js";
2
2
 
3
3
  const funcRegex =
4
- /^function(?:\s+([a-zA-Z$_][a-zA-Z0-9$_]*))?\s*\(((?:(?:\s*[a-zA-Z$_][a-zA-Z0-9$_]*)(?:\s*,\s*[a-zA-Z$_][a-zA-Z0-9$_]*)*)?)\s*\)\s*\{((?:.|\n|\r)*)\}$/;
4
+ /^function(?:\s+([a-zA-Z$_][a-zA-Z0-9$_]*))?\s*\(((?:(?:\s*[a-zA-Z$_][a-zA-Z0-9$_]*)(?:\s*,\s*[a-zA-Z$_][a-zA-Z0-9$_]*)*)?)\s*\)\s*\{((?:.|\n|\r)*)\}$/;
5
5
 
6
6
  export function parseFunc(func: Function) {
7
- var match = func.toString().match(funcRegex);
8
- invariant(match, "Unable to parse function!");
9
- return { name: match[1], args: match[2].split(","), body: match[3] };
7
+ var match = func.toString().match(funcRegex);
8
+ invariant(match, "Unable to parse function!");
9
+ return { name: match[1], args: match[2].split(","), body: match[3] };
10
10
  }
@@ -1,22 +1,18 @@
1
1
  import Namespace from "./Namespace.js";
2
2
 
3
- export function requireNamespace(
4
- root: any,
5
- namespace: string | Array<string>
6
- ): any {
7
- if (typeof root === "object") {
8
- if (typeof namespace === "string") {
9
- let path = namespace.split(".");
3
+ export function requireNamespace(root: any, namespace: string | Array<string>): any {
4
+ if (typeof root === "object") {
5
+ if (typeof namespace === "string") {
6
+ let path = namespace.split(".");
10
7
 
11
- return requireNamespace(root, path);
12
- } else if (Array.isArray(namespace)) {
13
- let current: any = root;
14
- for (let i = 0; i < namespace.length; i++) {
15
- current = current[namespace[i]] =
16
- current[namespace[i]] || new (Namespace as any)();
17
- }
8
+ return requireNamespace(root, path);
9
+ } else if (Array.isArray(namespace)) {
10
+ let current: any = root;
11
+ for (let i = 0; i < namespace.length; i++) {
12
+ current = current[namespace[i]] = current[namespace[i]] || new (Namespace as any)();
13
+ }
18
14
 
19
- return current;
20
- }
21
- }
15
+ return current;
16
+ }
17
+ }
22
18
  }
package/src/settings.ts CHANGED
@@ -1,36 +1,32 @@
1
1
  function getKey(article?: string) {
2
- const articleKey = article || af.article.id;
3
- return `${articleKey}.${af.userSession.login}`;
2
+ const articleKey = article || af.article.id;
3
+ return `${articleKey}.${af.userSession.login}`;
4
4
  }
5
5
 
6
6
  export function getSettings<T>(setting: string, article?: string): T | null {
7
- if (!af?.userSession) return null;
7
+ if (!af?.userSession) return null;
8
8
 
9
- const key = getKey(article);
10
- const settings = JSON.parse(localStorage.getItem(key) || "{}");
9
+ const key = getKey(article);
10
+ const settings = JSON.parse(localStorage.getItem(key) || "{}");
11
11
 
12
- if (setting) {
13
- return settings[setting] || null;
14
- }
12
+ if (setting) {
13
+ return settings[setting] || null;
14
+ }
15
15
 
16
- return settings;
16
+ return settings;
17
17
  }
18
18
 
19
- export function saveSettings<T>(
20
- setting: string,
21
- value: T | null,
22
- article?: string
23
- ): void {
24
- if (!af?.userSession) return;
19
+ export function saveSettings<T>(setting: string, value: T | null, article?: string): void {
20
+ if (!af?.userSession) return;
25
21
 
26
- const key = getKey(article);
27
- let userSettings = JSON.parse(localStorage.getItem(key) || "{}");
22
+ const key = getKey(article);
23
+ let userSettings = JSON.parse(localStorage.getItem(key) || "{}");
28
24
 
29
- if (typeof value !== "undefined") {
30
- userSettings[setting] = value;
31
- } else {
32
- userSettings = setting;
33
- }
25
+ if (typeof value !== "undefined") {
26
+ userSettings[setting] = value;
27
+ } else {
28
+ userSettings = setting;
29
+ }
34
30
 
35
- localStorage.setItem(key, JSON.stringify(userSettings));
31
+ localStorage.setItem(key, JSON.stringify(userSettings));
36
32
  }