ismx-nexo-node-app 0.4.17 → 0.4.19

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,10 +1,54 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class ColorUtils {
4
+ static hexToRgb(hex) {
5
+ hex = hex.replace(/^#/, '');
6
+ return {
7
+ r: parseInt(hex.slice(0, 2), 16),
8
+ g: parseInt(hex.slice(2, 4), 16),
9
+ b: parseInt(hex.slice(4, 6), 16),
10
+ };
11
+ }
12
+ static rgbToHex(r, g, b) {
13
+ return `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}`;
14
+ }
15
+ static interpolate(color1, color2, ratio) {
16
+ const color1Rgb = ColorUtils.hexToRgb(color1);
17
+ const color2Rgb = ColorUtils.hexToRgb(color2);
18
+ // Interpolate each color component (r, g, b)
19
+ const r = Math.round(color1Rgb.r + (color2Rgb.r - color1Rgb.r) * ratio);
20
+ const g = Math.round(color1Rgb.g + (color2Rgb.g - color1Rgb.g) * ratio);
21
+ const b = Math.round(color1Rgb.b + (color2Rgb.b - color1Rgb.b) * ratio);
22
+ // Convert the resulting RGB back to hex
23
+ return ColorUtils.rgbToHex(r, g, b);
24
+ }
25
+ static contrast(color) {
26
+ // Ensure hex is in the correct format
27
+ color = color.replace(/^#/, '');
28
+ // Convert 3-digit hex to 6-digit
29
+ if (color.length === 3) {
30
+ color = color.split('').map(x => x + x).join('');
31
+ }
32
+ // Convert hex to RGB
33
+ let r = parseInt(color.substring(0, 2), 16) / 255;
34
+ let g = parseInt(color.substring(2, 4), 16) / 255;
35
+ let b = parseInt(color.substring(4, 6), 16) / 255;
36
+ // Apply gamma correction (sRGB)
37
+ r = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
38
+ g = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4);
39
+ b = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
40
+ // Calculate luminance
41
+ let luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
42
+ // WCAG threshold for contrast readability (4.5:1)
43
+ return luminance > 0.4 ? '#111111' : '#FFFFFF';
44
+ }
4
45
  static transparency(color, transparency) {
5
- if (!color || !transparency)
6
- return undefined;
7
- return color.substring(0, 7) + Math.ceil(transparency * 255).toString(16);
46
+ if (!/^#([0-9A-F]{3}){1,2}$/i.test(color))
47
+ throw new Error('Invalid hex color format');
48
+ if (color.length === 4)
49
+ color = '#' + color.slice(1).split('').map(x => x + x).join('');
50
+ let alphaHex = Math.round(transparency * 255).toString(16).padStart(2, '0');
51
+ return color + alphaHex;
8
52
  }
9
53
  }
10
54
  exports.default = ColorUtils;
@@ -152,11 +152,15 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
152
152
  throw new Error(`table ${tableName} does not exist`);
153
153
  let table = this.tables[tableName];
154
154
  let schema = table[0].schema;
155
- let columns = PostgresUtils_1.default.columns(table);
156
- let params = columns.map((column, index) => `\$${index + 2}`).join(",");
157
- let values = [id, ...columns.map((column) => object[PostgresUtils_1.default.stringToCamel(column)])];
158
- let query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `;
159
- query += `RETURNING *`;
155
+ const columns = PostgresUtils_1.default.columns(table).filter(column => {
156
+ const camelKey = PostgresUtils_1.default.stringToCamel(column);
157
+ return Object.prototype.hasOwnProperty.call(object, camelKey);
158
+ });
159
+ if (columns.length === 0)
160
+ throw new Error(`No valid columns provided to update for table ${tableName}`);
161
+ const params = columns.map((_, index) => `\$${index + 2}`).join(",");
162
+ const values = [id, ...columns.map((column) => object[PostgresUtils_1.default.stringToCamel(column)])];
163
+ const query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 RETURNING *`;
160
164
  return this.query(query, values).then((result) => result[0]);
161
165
  });
162
166
  }
@@ -1,3 +1,11 @@
1
1
  export default abstract class ColorUtils {
2
- static transparency(color?: string, transparency?: number): string | undefined;
2
+ static hexToRgb(hex: string): {
3
+ r: number;
4
+ g: number;
5
+ b: number;
6
+ };
7
+ static rgbToHex(r: number, g: number, b: number): string;
8
+ static interpolate(color1: string, color2: string, ratio: number): string;
9
+ static contrast(color: string): string;
10
+ static transparency(color: string, transparency: number): string;
3
11
  }
@@ -36,9 +36,7 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase {
36
36
  addAll<T>(tableName: string, objects: ({
37
37
  [key: string]: Primitive;
38
38
  } | any)[]): Promise<T[]>;
39
- update<T>(tableName: string, id: string, object: {
40
- [key: string]: Primitive;
41
- } | any): Promise<T>;
39
+ update<T>(tableName: string, id: string, object: Partial<T>): Promise<T>;
42
40
  page<T>(tableName: string, sortKey: string, maxResults?: number, pageNumber?: number, filters?: {
43
41
  [key: string]: Primitive | Array<Primitive>;
44
42
  }): Promise<Pagination<T>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.17",
3
+ "version": "0.4.19",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -1,8 +1,63 @@
1
1
  export default abstract class ColorUtils {
2
2
 
3
- static transparency(color?: string, transparency?: number) {
4
- if (!color || !transparency) return undefined;
5
- return color.substring(0, 7) + Math.ceil(transparency*255).toString(16);
3
+ static hexToRgb(hex: string): { r: number; g: number; b: number }
4
+ {
5
+ hex = hex.replace(/^#/, '');
6
+ return {
7
+ r: parseInt(hex.slice(0, 2), 16),
8
+ g: parseInt(hex.slice(2, 4), 16),
9
+ b: parseInt(hex.slice(4, 6), 16),
10
+ };
6
11
  }
7
12
 
13
+ static rgbToHex(r:number, g: number, b: number): string
14
+ {
15
+ return `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}`;
16
+ }
17
+
18
+ static interpolate(color1: string, color2: string, ratio: number) {
19
+ const color1Rgb = ColorUtils.hexToRgb(color1);
20
+ const color2Rgb = ColorUtils.hexToRgb(color2);
21
+
22
+ // Interpolate each color component (r, g, b)
23
+ const r = Math.round(color1Rgb.r + (color2Rgb.r - color1Rgb.r) * ratio);
24
+ const g = Math.round(color1Rgb.g + (color2Rgb.g - color1Rgb.g) * ratio);
25
+ const b = Math.round(color1Rgb.b + (color2Rgb.b - color1Rgb.b) * ratio);
26
+
27
+ // Convert the resulting RGB back to hex
28
+ return ColorUtils.rgbToHex(r, g, b);
29
+ }
30
+
31
+ static contrast(color: string): string {
32
+ // Ensure hex is in the correct format
33
+ color = color.replace(/^#/, '');
34
+
35
+ // Convert 3-digit hex to 6-digit
36
+ if (color.length === 3) {
37
+ color = color.split('').map(x => x + x).join('');
38
+ }
39
+
40
+ // Convert hex to RGB
41
+ let r = parseInt(color.substring(0, 2), 16) / 255;
42
+ let g = parseInt(color.substring(2, 4), 16) / 255;
43
+ let b = parseInt(color.substring(4, 6), 16) / 255;
44
+
45
+ // Apply gamma correction (sRGB)
46
+ r = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
47
+ g = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4);
48
+ b = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
49
+
50
+ // Calculate luminance
51
+ let luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
52
+
53
+ // WCAG threshold for contrast readability (4.5:1)
54
+ return luminance > 0.4 ? '#111111' : '#FFFFFF';
55
+ }
56
+
57
+ static transparency(color: string, transparency: number) {
58
+ if (!/^#([0-9A-F]{3}){1,2}$/i.test(color)) throw new Error('Invalid hex color format');
59
+ if (color.length === 4) color = '#' + color.slice(1).split('').map(x => x + x).join('');
60
+ let alphaHex = Math.round(transparency * 255).toString(16).padStart(2, '0');
61
+ return color + alphaHex;
62
+ }
8
63
  }
@@ -120,18 +120,22 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
120
120
  return this.query(query, values);
121
121
  }
122
122
 
123
- async update<T>(tableName: string, id: string, object: {[key:string]:Primitive}|any): Promise<T>
123
+ async update<T>(tableName: string, id: string, object: Partial<T>): Promise<T>
124
124
  {
125
125
  if (!id) throw new Error(`field 'id' is mandatory when updating ${tableName}`);
126
126
  if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
127
127
  let table = this.tables[tableName];
128
128
  let schema = table[0].schema;
129
- let columns = PostgresUtils.columns(table);
129
+ const columns = PostgresUtils.columns(table).filter(column => {
130
+ const camelKey = PostgresUtils.stringToCamel(column);
131
+ return Object.prototype.hasOwnProperty.call(object, camelKey);
132
+ });
133
+
134
+ if (columns.length === 0) throw new Error(`No valid columns provided to update for table ${tableName}`);
130
135
 
131
- let params = columns.map((column, index) => `\$${index+2}`).join(",");
132
- let values = [ id, ...columns.map((column) => object[PostgresUtils.stringToCamel(column)]) ];
133
- let query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `
134
- query += `RETURNING *`;
136
+ const params = columns.map((_, index) => `\$${index + 2}`).join(",");
137
+ const values = [ id, ...columns.map((column) => object[PostgresUtils.stringToCamel(column) as keyof T]) ];
138
+ const query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 RETURNING *`;
135
139
 
136
140
  return this.query<T>(query, values).then((result) => result[0]);
137
141
  }