@churchapps/helpers 1.2.3 → 1.2.5

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 (53) hide show
  1. package/.eslintrc.json +28 -28
  2. package/.github/FUNDING.yml +1 -1
  3. package/.prettierrc +11 -11
  4. package/.yarnrc.yml +5 -5
  5. package/LICENSE +21 -21
  6. package/README.md +15 -15
  7. package/dist/ApiHelper.d.ts.map +1 -1
  8. package/dist/ApiHelper.js +4 -10
  9. package/dist/ApiHelper.js.map +1 -1
  10. package/dist/ArrayHelper.js +2 -1
  11. package/dist/ArrayHelper.js.map +1 -1
  12. package/dist/CommonEnvironmentHelper.d.ts +1 -1
  13. package/dist/CommonEnvironmentHelper.d.ts.map +1 -1
  14. package/dist/CommonEnvironmentHelper.js +5 -5
  15. package/dist/CommonEnvironmentHelper.js.map +1 -1
  16. package/dist/DateHelper.d.ts.map +1 -1
  17. package/dist/DateHelper.js +2 -0
  18. package/dist/DateHelper.js.map +1 -1
  19. package/dist/PersonHelper.js +2 -2
  20. package/dist/PersonHelper.js.map +1 -1
  21. package/dist/UserHelper.d.ts.map +1 -1
  22. package/dist/UserHelper.js +6 -2
  23. package/dist/UserHelper.js.map +1 -1
  24. package/package.json +50 -49
  25. package/scripts/build-cjs.js +32 -32
  26. package/src/ApiHelper.ts +169 -176
  27. package/src/AppearanceHelper.ts +69 -69
  28. package/src/ArrayHelper.ts +157 -157
  29. package/src/CommonEnvironmentHelper.ts +104 -104
  30. package/src/CurrencyHelper.ts +10 -10
  31. package/src/DateHelper.ts +154 -153
  32. package/src/DonationHelper.ts +26 -26
  33. package/src/ErrorHelper.ts +39 -39
  34. package/src/EventHelper.ts +49 -49
  35. package/src/FileHelper.ts +55 -55
  36. package/src/PersonHelper.ts +82 -82
  37. package/src/UniqueIdHelper.ts +36 -36
  38. package/src/UserHelper.ts +62 -59
  39. package/src/index.ts +15 -15
  40. package/src/interfaces/Access.ts +138 -138
  41. package/src/interfaces/Attendance.ts +45 -45
  42. package/src/interfaces/Content.ts +84 -84
  43. package/src/interfaces/Doing.ts +93 -93
  44. package/src/interfaces/Donation.ts +190 -190
  45. package/src/interfaces/Error.ts +17 -17
  46. package/src/interfaces/Membership.ts +184 -184
  47. package/src/interfaces/Messaging.ts +96 -96
  48. package/src/interfaces/Permissions.ts +92 -92
  49. package/src/interfaces/Reporting.ts +41 -41
  50. package/src/interfaces/UserContextInterface.ts +13 -13
  51. package/src/interfaces/index.ts +13 -13
  52. package/tsconfig.json +36 -36
  53. package/CLAUDE.md +0 -98
package/package.json CHANGED
@@ -1,49 +1,50 @@
1
- {
2
- "name": "@churchapps/helpers",
3
- "version": "1.2.3",
4
- "description": "Library of helper functions not specific to any one ChurchApps project or framework.",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "scripts": {
8
- "test": "echo \"Error: no test specified\" && exit 1",
9
- "clean": "rimraf dist",
10
- "build": "yarn clean && tsc",
11
- "lint": "eslint src/**/*.ts",
12
- "lint:fix": "eslint src/**/*.ts --fix",
13
- "format": "prettier --write src/**/*.ts"
14
- },
15
- "repository": {
16
- "type": "git",
17
- "url": "git+https://github.com/LiveChurchSolutions/Helpers.git"
18
- },
19
- "keywords": [
20
- "ChurchApps"
21
- ],
22
- "author": "ChurchApps.org",
23
- "license": "MIT",
24
- "bugs": {
25
- "url": "https://github.com/LiveChurchSolutions/Helpers/issues"
26
- },
27
- "homepage": "https://github.com/LiveChurchSolutions/Helpers#readme",
28
- "engines": {
29
- "node": ">=20.0.0",
30
- "yarn": ">=1.22.0",
31
- "npm": ">=8.0.0"
32
- },
33
- "devDependencies": {
34
- "@types/node": "^24.5.0",
35
- "@typescript-eslint/eslint-plugin": "^7.0.0",
36
- "@typescript-eslint/parser": "^7.0.0",
37
- "eslint": "^8.57.0",
38
- "eslint-config-prettier": "^10.1.8",
39
- "eslint-plugin-prettier": "^5.5.4",
40
- "npm-run-all2": "^8.0.4",
41
- "prettier": "^3.6.2",
42
- "rimraf": "^6.0.1",
43
- "typescript": "^5.9.2"
44
- },
45
- "dependencies": {
46
- "dayjs": "^1.11.18",
47
- "rrule": "^2.8.1"
48
- }
49
- }
1
+ {
2
+ "name": "@churchapps/helpers",
3
+ "version": "1.2.5",
4
+ "description": "Library of helper functions not specific to any one ChurchApps project or framework.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1",
9
+ "clean": "rimraf dist",
10
+ "build": "yarn clean && tsc",
11
+ "lint": "eslint src/**/*.ts",
12
+ "lint:fix": "eslint src/**/*.ts --fix",
13
+ "format": "prettier --write src/**/*.ts",
14
+ "publish": "npm run build && npm publish --access=public"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/LiveChurchSolutions/Helpers.git"
19
+ },
20
+ "keywords": [
21
+ "ChurchApps"
22
+ ],
23
+ "author": "ChurchApps.org",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/LiveChurchSolutions/Helpers/issues"
27
+ },
28
+ "homepage": "https://github.com/LiveChurchSolutions/Helpers#readme",
29
+ "engines": {
30
+ "node": ">=20.0.0",
31
+ "yarn": ">=1.22.0",
32
+ "npm": ">=8.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^24.5.0",
36
+ "@typescript-eslint/eslint-plugin": "^7.0.0",
37
+ "@typescript-eslint/parser": "^7.0.0",
38
+ "eslint": "^8.57.0",
39
+ "eslint-config-prettier": "^10.1.8",
40
+ "eslint-plugin-prettier": "^5.5.4",
41
+ "npm-run-all2": "^8.0.4",
42
+ "prettier": "^3.6.2",
43
+ "rimraf": "^6.0.1",
44
+ "typescript": "^5.9.2"
45
+ },
46
+ "dependencies": {
47
+ "dayjs": "^1.11.18",
48
+ "rrule": "^2.8.1"
49
+ }
50
+ }
@@ -1,33 +1,33 @@
1
- import fs from 'fs-extra';
2
- import path from 'path';
3
- import { fileURLToPath } from 'url';
4
-
5
- const __filename = fileURLToPath(import.meta.url);
6
- const __dirname = path.dirname(__filename);
7
-
8
- async function buildCjs() {
9
- try {
10
- const distDir = path.join(__dirname, '..', 'dist');
11
-
12
- // Read the ES module index
13
- const esmContent = await fs.readFile(path.join(distDir, 'index.js'), 'utf8');
14
-
15
- // Convert ES module syntax to CommonJS
16
- let cjsContent = esmContent
17
- .replace(/export \* from "(.+?)";/g, 'Object.assign(module.exports, require("$1"));')
18
- .replace(/export \{ (.+?) \} from "(.+?)";/g, (match, exports, from) => {
19
- const exportList = exports.split(',').map(e => e.trim());
20
- return exportList.map(exp => `module.exports.${exp} = require("${from}").${exp};`).join('\n');
21
- });
22
-
23
- // Write CommonJS version
24
- await fs.writeFile(path.join(distDir, 'index.cjs'), cjsContent);
25
-
26
- console.log('CommonJS build created successfully');
27
- } catch (error) {
28
- console.error('Error building CommonJS version:', error);
29
- process.exit(1);
30
- }
31
- }
32
-
1
+ import fs from 'fs-extra';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = path.dirname(__filename);
7
+
8
+ async function buildCjs() {
9
+ try {
10
+ const distDir = path.join(__dirname, '..', 'dist');
11
+
12
+ // Read the ES module index
13
+ const esmContent = await fs.readFile(path.join(distDir, 'index.js'), 'utf8');
14
+
15
+ // Convert ES module syntax to CommonJS
16
+ let cjsContent = esmContent
17
+ .replace(/export \* from "(.+?)";/g, 'Object.assign(module.exports, require("$1"));')
18
+ .replace(/export \{ (.+?) \} from "(.+?)";/g, (match, exports, from) => {
19
+ const exportList = exports.split(',').map(e => e.trim());
20
+ return exportList.map(exp => `module.exports.${exp} = require("${from}").${exp};`).join('\n');
21
+ });
22
+
23
+ // Write CommonJS version
24
+ await fs.writeFile(path.join(distDir, 'index.cjs'), cjsContent);
25
+
26
+ console.log('CommonJS build created successfully');
27
+ } catch (error) {
28
+ console.error('Error building CommonJS version:', error);
29
+ process.exit(1);
30
+ }
31
+ }
32
+
33
33
  buildCjs();
package/src/ApiHelper.ts CHANGED
@@ -1,176 +1,169 @@
1
- import { ApiConfig, RolePermissionInterface, ApiListType } from "./interfaces";
2
- import { ErrorHelper } from "./ErrorHelper";
3
-
4
- // Global singleton pattern to ensure single instance across all packages
5
- declare global {
6
- interface Window {
7
- __CHURCHAPPS_API_HELPER__?: ApiHelperClass;
8
- }
9
- var __CHURCHAPPS_API_HELPER__: ApiHelperClass | undefined;
10
- }
11
-
12
- class ApiHelperClass {
13
-
14
- apiConfigs: ApiConfig[] = [];
15
- isAuthenticated = false;
16
- onRequest: (url:string, requestOptions:any) => void;
17
- onError: (url:string, requestOptions:any, error: any) => void;
18
-
19
- getConfig(keyName: string) {
20
- let result: ApiConfig = null;
21
- this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
22
- //if (result === null) throw new Error("Unconfigured API: " + keyName);
23
- return result;
24
- }
25
-
26
- setDefaultPermissions(jwt: string) {
27
- this.apiConfigs.forEach(config => {
28
- config.jwt = jwt;
29
- config.permissions = [];
30
- });
31
- this.isAuthenticated = true;
32
- }
33
-
34
- setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
35
- this.apiConfigs.forEach(config => {
36
- if (config.keyName === keyName) {
37
- config.jwt = jwt;
38
- config.permissions = permissions;
39
- }
40
- });
41
- this.isAuthenticated = true;
42
- }
43
-
44
- clearPermissions() {
45
- this.apiConfigs.forEach(config => { config.jwt = ""; config.permissions = []; });
46
- this.isAuthenticated = false;
47
- }
48
-
49
- async get(path: string, apiName: ApiListType) {
50
- const config = this.getConfig(apiName);
51
- if (!config) throw new Error(`API configuration not found: ${apiName}`);
52
- const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + config.jwt } };
53
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
54
- }
55
-
56
- async getAnonymous(path: string, apiName: ApiListType) {
57
- const config = this.getConfig(apiName);
58
- const requestOptions = { method: "GET" };
59
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
60
- }
61
-
62
- async post(path: string, data: any[] | {}, apiName: ApiListType) {
63
- const config = this.getConfig(apiName);
64
- if (!config) throw new Error(`API configuration not found: ${apiName}`);
65
- const requestOptions = {
66
- method: "POST",
67
- headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
68
- body: JSON.stringify(data)
69
- };
70
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
71
- }
72
-
73
- async patch(path: string, data: any[] | {}, apiName: ApiListType) {
74
- const config = this.getConfig(apiName);
75
- if (!config) throw new Error(`API configuration not found: ${apiName}`);
76
- const requestOptions = {
77
- method: "PATCH",
78
- headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
79
- body: JSON.stringify(data)
80
- };
81
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
82
- }
83
-
84
- async delete(path: string, apiName: ApiListType) {
85
- const config = this.getConfig(apiName);
86
- if (!config) throw new Error(`API configuration not found: ${apiName}`);
87
- const requestOptions = {
88
- method: "DELETE",
89
- headers: { Authorization: "Bearer " + config.jwt }
90
- };
91
- if (this.onRequest) this.onRequest(config.url + path, requestOptions);
92
- try {
93
- const response = await fetch(config.url + path, requestOptions);
94
- if (!response.ok) await this.throwApiError(response);
95
- } catch (e) {
96
- console.log(e);
97
- if (this.onError) this.onError(config.url + path, requestOptions, e);
98
- throw (e);
99
- }
100
- }
101
-
102
- async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
103
- const config = this.getConfig(apiName);
104
- const requestOptions = {
105
- method: "POST",
106
- headers: { "Content-Type": "application/json" },
107
- body: JSON.stringify(data)
108
- };
109
- return await this.fetchWithErrorHandling(config.url + path, requestOptions);
110
- }
111
-
112
- async fetchWithErrorHandling(url: string, requestOptions: any) {
113
- if (this.onRequest) this.onRequest(url, requestOptions);
114
- try {
115
- const response = await fetch(url, requestOptions);
116
- if (!response.ok) await this.throwApiError(response);
117
- else {
118
- if (response.status !== 204 ) {
119
- return response.json();
120
- }
121
- }
122
- } catch (e) {
123
- console.log("Error loading url: " + url);
124
- console.log(e)
125
- throw (e);
126
- }
127
- }
128
-
129
- private async throwApiError(response: Response) {
130
- let msg = response.statusText;
131
- try {
132
- msg = await response.text();
133
- } catch {}
134
- try {
135
- const json = await response.json();
136
- msg = json.errors[0];
137
- } catch { }
138
- console.log("RESPONSE", response)
139
- ErrorHelper.logError(response.status.toString(), response.url, msg);
140
- throw new Error(msg || "Error");
141
- }
142
-
143
- }
144
-
145
- // Force singleton with immediate global assignment
146
- const getGlobalObject = () => {
147
- if (typeof window !== 'undefined') return window;
148
- if (typeof global !== 'undefined') return global;
149
- if (typeof globalThis !== 'undefined') return globalThis;
150
- return {};
151
- };
152
-
153
- // Get or create singleton immediately - FORCE SINGLE INSTANCE
154
- const ensureSingleton = () => {
155
- const globalObj = getGlobalObject() as any;
156
-
157
- // Use a more unique key to avoid conflicts
158
- const SINGLETON_KEY = '__CHURCHAPPS_API_HELPER_SINGLETON__';
159
-
160
- // ALWAYS create a new instance and overwrite any existing one
161
- // This ensures the latest module load wins
162
- if (!globalObj[SINGLETON_KEY]) {
163
- globalObj[SINGLETON_KEY] = new ApiHelperClass();
164
- console.log('🔧 ApiHelper SINGLETON created (new instance)');
165
- } else {
166
- console.log('🔄 ApiHelper SINGLETON already exists - using existing (configs:', globalObj[SINGLETON_KEY].apiConfigs.length, ')');
167
- }
168
-
169
- return globalObj[SINGLETON_KEY];
170
- };
171
-
172
- // Export the singleton instance
173
- export const ApiHelper = ensureSingleton();
174
-
175
- // Also export the class for type usage
176
- export type { ApiHelperClass };
1
+ import { ApiConfig, RolePermissionInterface, ApiListType } from "./interfaces";
2
+ import { ErrorHelper } from "./ErrorHelper";
3
+
4
+ // Global singleton pattern to ensure single instance across all packages
5
+ declare global {
6
+ interface Window {
7
+ __CHURCHAPPS_API_HELPER__?: ApiHelperClass;
8
+ }
9
+ var __CHURCHAPPS_API_HELPER__: ApiHelperClass | undefined;
10
+ }
11
+
12
+ class ApiHelperClass {
13
+
14
+ apiConfigs: ApiConfig[] = [];
15
+ isAuthenticated = false;
16
+ onRequest: (url:string, requestOptions:any) => void;
17
+ onError: (url:string, requestOptions:any, error: any) => void;
18
+
19
+ getConfig(keyName: string) {
20
+ let result: ApiConfig = null;
21
+ this.apiConfigs.forEach(config => { if (config.keyName === keyName) result = config });
22
+ //if (result === null) throw new Error("Unconfigured API: " + keyName);
23
+ return result;
24
+ }
25
+
26
+ setDefaultPermissions(jwt: string) {
27
+ this.apiConfigs.forEach(config => {
28
+ config.jwt = jwt;
29
+ config.permissions = [];
30
+ });
31
+ this.isAuthenticated = true;
32
+ }
33
+
34
+ setPermissions(keyName: string, jwt: string, permissions: RolePermissionInterface[]) {
35
+ this.apiConfigs.forEach(config => {
36
+ if (config.keyName === keyName) {
37
+ config.jwt = jwt;
38
+ config.permissions = permissions;
39
+ }
40
+ });
41
+ this.isAuthenticated = true;
42
+ }
43
+
44
+ clearPermissions() {
45
+ this.apiConfigs.forEach(config => { config.jwt = ""; config.permissions = []; });
46
+ this.isAuthenticated = false;
47
+ }
48
+
49
+ async get(path: string, apiName: ApiListType) {
50
+ const config = this.getConfig(apiName);
51
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
52
+ const requestOptions = { method: "GET", headers: { Authorization: "Bearer " + config.jwt } };
53
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
54
+ }
55
+
56
+ async getAnonymous(path: string, apiName: ApiListType) {
57
+ const config = this.getConfig(apiName);
58
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
59
+ const requestOptions = { method: "GET" };
60
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
61
+ }
62
+
63
+ async post(path: string, data: any[] | {}, apiName: ApiListType) {
64
+ const config = this.getConfig(apiName);
65
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
66
+ const requestOptions = {
67
+ method: "POST",
68
+ headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
69
+ body: JSON.stringify(data)
70
+ };
71
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
72
+ }
73
+
74
+ async patch(path: string, data: any[] | {}, apiName: ApiListType) {
75
+ const config = this.getConfig(apiName);
76
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
77
+ const requestOptions = {
78
+ method: "PATCH",
79
+ headers: { Authorization: "Bearer " + config.jwt, "Content-Type": "application/json" },
80
+ body: JSON.stringify(data)
81
+ };
82
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
83
+ }
84
+
85
+ async delete(path: string, apiName: ApiListType) {
86
+ const config = this.getConfig(apiName);
87
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
88
+ const requestOptions = {
89
+ method: "DELETE",
90
+ headers: { Authorization: "Bearer " + config.jwt }
91
+ };
92
+ if (this.onRequest) this.onRequest(config.url + path, requestOptions);
93
+ try {
94
+ const response = await fetch(config.url + path, requestOptions);
95
+ if (!response.ok) await this.throwApiError(response);
96
+ } catch (e) {
97
+ if (this.onError) this.onError(config.url + path, requestOptions, e);
98
+ throw (e);
99
+ }
100
+ }
101
+
102
+ async postAnonymous(path: string, data: any[] | {}, apiName: ApiListType) {
103
+ const config = this.getConfig(apiName);
104
+ if (!config) throw new Error(`API configuration not found: ${apiName}`);
105
+ const requestOptions = {
106
+ method: "POST",
107
+ headers: { "Content-Type": "application/json" },
108
+ body: JSON.stringify(data)
109
+ };
110
+ return await this.fetchWithErrorHandling(config.url + path, requestOptions);
111
+ }
112
+
113
+ async fetchWithErrorHandling(url: string, requestOptions: any) {
114
+ if (this.onRequest) this.onRequest(url, requestOptions);
115
+ try {
116
+ const response = await fetch(url, requestOptions);
117
+ if (!response.ok) await this.throwApiError(response);
118
+ else {
119
+ if (response.status !== 204 ) {
120
+ return response.json();
121
+ }
122
+ }
123
+ } catch (e) {
124
+ throw (e);
125
+ }
126
+ }
127
+
128
+ private async throwApiError(response: Response) {
129
+ let msg = response.statusText;
130
+ try {
131
+ msg = await response.text();
132
+ } catch {}
133
+ try {
134
+ const json = await response.json();
135
+ msg = json.errors[0];
136
+ } catch { }
137
+ ErrorHelper.logError(response.status.toString(), response.url, msg);
138
+ throw new Error(msg || "Error");
139
+ }
140
+
141
+ }
142
+
143
+ // Force singleton with immediate global assignment
144
+ const getGlobalObject = () => {
145
+ if (typeof window !== 'undefined') return window;
146
+ if (typeof global !== 'undefined') return global;
147
+ if (typeof globalThis !== 'undefined') return globalThis;
148
+ return {};
149
+ };
150
+
151
+ // Get or create singleton immediately - FORCE SINGLE INSTANCE
152
+ const ensureSingleton = () => {
153
+ const globalObj = getGlobalObject() as any;
154
+
155
+ // Use a more unique key to avoid conflicts
156
+ const SINGLETON_KEY = '__CHURCHAPPS_API_HELPER_SINGLETON__';
157
+
158
+ if (!globalObj[SINGLETON_KEY]) {
159
+ globalObj[SINGLETON_KEY] = new ApiHelperClass();
160
+ }
161
+
162
+ return globalObj[SINGLETON_KEY];
163
+ };
164
+
165
+ // Export the singleton instance
166
+ export const ApiHelper = ensureSingleton();
167
+
168
+ // Also export the class for type usage
169
+ export type { ApiHelperClass };
@@ -1,69 +1,69 @@
1
-
2
- export interface AppearanceInterface { primaryColor?: string, primaryContrast?: string, secondaryColor?: string, secondaryContrast?: string, logoLight?: string, logoDark?: string, favicon_400x400?: string, favicon_16x16?: string }
3
-
4
- export class AppearanceHelper {
5
-
6
- public static getLogoDark(appearanceSettings: AppearanceInterface, defaultLogo: string) {
7
- return (appearanceSettings?.logoDark) ? appearanceSettings.logoDark : defaultLogo;
8
- }
9
-
10
- public static getLogoLight(appearanceSettings: AppearanceInterface, defaultLogo: string) {
11
- return (appearanceSettings?.logoLight) ? appearanceSettings.logoLight : defaultLogo;
12
- }
13
-
14
- public static getFavicon(appearanceSettings: AppearanceInterface, size: "400" | "16") {
15
- if (size === "400") {
16
- return appearanceSettings?.favicon_400x400;
17
- }
18
-
19
- if (size === "16") {
20
- return appearanceSettings?.favicon_16x16;
21
- }
22
-
23
- return null;
24
- }
25
-
26
- public static getLogo(appearanceSettings: AppearanceInterface, defaultLogoLight: string, defaultLogoDark: string, backgroundColor: string) {
27
- const isDark = (appearanceSettings.logoDark) ? this.isDark(backgroundColor) : false;
28
- if (isDark) return this.getLogoDark(appearanceSettings, defaultLogoDark);
29
- else return this.getLogoLight(appearanceSettings, defaultLogoLight);
30
- }
31
-
32
- private static isDark(backgroundColor: string) {
33
- let valid = false;
34
- let r = 0;
35
- let g = 0;
36
- let b = 0;
37
-
38
- if (backgroundColor.match(/#[0-9a-fA-F]{6}/)) {
39
- r = this.getHexValue(backgroundColor.substring(1, 3));
40
- g = this.getHexValue(backgroundColor.substring(3, 5));
41
- b = this.getHexValue(backgroundColor.substring(5, 7));
42
- valid = true;
43
- } else if (backgroundColor.match(/#[0-9a-fA-F]{3}/)) {
44
- r = this.getHexValue(backgroundColor.substring(1, 2));
45
- g = this.getHexValue(backgroundColor.substring(2, 3));
46
- b = this.getHexValue(backgroundColor.substring(3, 4));
47
- valid = true;
48
- }
49
-
50
- if (!valid) return false;
51
- else {
52
- //HSP brightness formula. Some colors have a bigger impact on our perceived brightness than others.
53
- const rWeight = .299 * Math.pow(r, 2);
54
- const gWeight = .587 * Math.pow(g, 2);
55
- const bWeight = .114 * Math.pow(b, 2);
56
- const brightness = Math.sqrt(rWeight + gWeight + bWeight);
57
- //return brightness < 128; //
58
- return brightness < 156;
59
- }
60
-
61
- }
62
-
63
- private static getHexValue(hex: string) {
64
- let result = parseInt(hex, 16);
65
- if (hex.length === 1) result = result * 16;
66
- return result;
67
- }
68
-
69
- }
1
+
2
+ export interface AppearanceInterface { primaryColor?: string, primaryContrast?: string, secondaryColor?: string, secondaryContrast?: string, logoLight?: string, logoDark?: string, favicon_400x400?: string, favicon_16x16?: string }
3
+
4
+ export class AppearanceHelper {
5
+
6
+ public static getLogoDark(appearanceSettings: AppearanceInterface, defaultLogo: string) {
7
+ return (appearanceSettings?.logoDark) ? appearanceSettings.logoDark : defaultLogo;
8
+ }
9
+
10
+ public static getLogoLight(appearanceSettings: AppearanceInterface, defaultLogo: string) {
11
+ return (appearanceSettings?.logoLight) ? appearanceSettings.logoLight : defaultLogo;
12
+ }
13
+
14
+ public static getFavicon(appearanceSettings: AppearanceInterface, size: "400" | "16") {
15
+ if (size === "400") {
16
+ return appearanceSettings?.favicon_400x400;
17
+ }
18
+
19
+ if (size === "16") {
20
+ return appearanceSettings?.favicon_16x16;
21
+ }
22
+
23
+ return null;
24
+ }
25
+
26
+ public static getLogo(appearanceSettings: AppearanceInterface, defaultLogoLight: string, defaultLogoDark: string, backgroundColor: string) {
27
+ const isDark = (appearanceSettings.logoDark) ? this.isDark(backgroundColor) : false;
28
+ if (isDark) return this.getLogoDark(appearanceSettings, defaultLogoDark);
29
+ else return this.getLogoLight(appearanceSettings, defaultLogoLight);
30
+ }
31
+
32
+ private static isDark(backgroundColor: string) {
33
+ let valid = false;
34
+ let r = 0;
35
+ let g = 0;
36
+ let b = 0;
37
+
38
+ if (backgroundColor.match(/#[0-9a-fA-F]{6}/)) {
39
+ r = this.getHexValue(backgroundColor.substring(1, 3));
40
+ g = this.getHexValue(backgroundColor.substring(3, 5));
41
+ b = this.getHexValue(backgroundColor.substring(5, 7));
42
+ valid = true;
43
+ } else if (backgroundColor.match(/#[0-9a-fA-F]{3}/)) {
44
+ r = this.getHexValue(backgroundColor.substring(1, 2));
45
+ g = this.getHexValue(backgroundColor.substring(2, 3));
46
+ b = this.getHexValue(backgroundColor.substring(3, 4));
47
+ valid = true;
48
+ }
49
+
50
+ if (!valid) return false;
51
+ else {
52
+ //HSP brightness formula. Some colors have a bigger impact on our perceived brightness than others.
53
+ const rWeight = .299 * Math.pow(r, 2);
54
+ const gWeight = .587 * Math.pow(g, 2);
55
+ const bWeight = .114 * Math.pow(b, 2);
56
+ const brightness = Math.sqrt(rWeight + gWeight + bWeight);
57
+ //return brightness < 128; //
58
+ return brightness < 156;
59
+ }
60
+
61
+ }
62
+
63
+ private static getHexValue(hex: string) {
64
+ let result = parseInt(hex, 16);
65
+ if (hex.length === 1) result = result * 16;
66
+ return result;
67
+ }
68
+
69
+ }