coer-elements 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ import { signal } from "@angular/core";
2
+ import { Screen } from './../Tools/Screen.class';
3
+
4
+ export const breakpoint_signal = signal<'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'>(Screen?.BREAKPOINT || 'xs');
5
+ export const isReady_signal = signal<boolean>(false);
6
+ export const isLoading_signal = signal<boolean>(false);
7
+ export const isModalOpen_signal = signal<boolean>(false);
@@ -0,0 +1,27 @@
1
+ import moment from "moment";
2
+
3
+ export class DateTime {
4
+ /** Get UTC Offset */
5
+ public static GetUTCOffset(): number {
6
+ return moment().utcOffset();
7
+ }
8
+
9
+
10
+ /** Convert UTC Date to Local Zone */
11
+ public static ToLocalZone(utcDate: string | Date | moment.Moment): string {
12
+ return moment(utcDate).add(DateTime.GetUTCOffset(), 'minutes').format('YYYY-MM-DD HH:mm:ss');
13
+ }
14
+
15
+
16
+ /** Convert Local Zone Date to UTC */
17
+ public static ToUTC(utcDate: string | Date | moment.Moment): string {
18
+ return moment(utcDate).subtract(DateTime.GetUTCOffset(), 'minutes').format('YYYY-MM-DD HH:mm:ss');
19
+ }
20
+
21
+
22
+ /** DD MMM YYYY */
23
+ public static GetDateFormat(date: string | Date | moment.Moment): string {
24
+ if ((typeof date === 'string')) date = date.replaceAll('/', '-');
25
+ return moment(date).parseZone().local(true).format('DD MMM YYYY');
26
+ }
27
+ }
@@ -0,0 +1,119 @@
1
+ import { Tools } from ".";
2
+ import * as XLSX from 'xlsx';
3
+
4
+ export class Files {
5
+ public static readonly EXCEL_EXTENSIONS: string[] = ['xls', 'xlsx', 'csv'];
6
+
7
+ /** Get Extension File */
8
+ public static GetExtension(file: File): string | null {
9
+ const fileName = file.name;
10
+
11
+ if (fileName.includes('.')) {
12
+ let worlds = fileName.split('.') as string[];
13
+
14
+ if (worlds.length > 0) {
15
+ let extension = worlds.pop()!;
16
+ extension = extension.trim().toLowerCase();
17
+ if (extension.length > 0) return extension;
18
+ }
19
+ }
20
+
21
+ return null;
22
+ }
23
+
24
+
25
+ /** Is Excel File */
26
+ public static IsExcel(file: File): boolean {
27
+ const EXTENSION = Files.GetExtension(file);
28
+
29
+ return Tools.IsNotNull(EXTENSION)
30
+ ? this.EXCEL_EXTENSIONS.includes(EXTENSION!)
31
+ : false;
32
+ }
33
+
34
+
35
+ /** Read excel file */
36
+ public static ReadExcel<T>(file: File) {
37
+ return new Promise<{ columns: string[]; rows: T[]; }>(Resolve => {
38
+ let columns: string[] = [];
39
+ let rows: T[] = [];
40
+
41
+ const reader = new FileReader();
42
+ reader.readAsArrayBuffer(file);
43
+
44
+ reader.onload = () => {
45
+ const dataBytes = new Uint8Array(reader.result as any);
46
+
47
+ if (dataBytes) {
48
+ const workbook = XLSX.read(dataBytes, {});
49
+ const sheet = workbook.Sheets[workbook.SheetNames[0]];
50
+ let dataSheet: any[] = XLSX.utils.sheet_to_json(sheet, {
51
+ header: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
52
+ });
53
+
54
+ //Get Headers
55
+ for(const column in dataSheet[0]) {
56
+ columns.push(Tools.FirstCharToLower(String(dataSheet[0][column]).replaceAll(' ', '')));
57
+ }
58
+
59
+ //Get Rows
60
+ rows = XLSX.utils.sheet_to_json(sheet, { header: columns });
61
+ rows.shift();
62
+
63
+ rows = rows.map(row => {
64
+ const item = Tools.BreakReference<any>(row);
65
+ delete item['__rowNum__'];
66
+ return item;
67
+ });
68
+ }
69
+
70
+ Resolve({ columns, rows });
71
+ }
72
+
73
+ reader.onerror = () => { Resolve({ columns, rows }) }
74
+ });
75
+ }
76
+
77
+
78
+ /** Export to excel file */
79
+ public static ExportExcel<T>(data: T[], fileName: string = 'coer_report', sheetName: string = 'Sheet1') {
80
+ sheetName = Tools.CleanUpBlanks(sheetName);
81
+ fileName = Tools.CleanUpBlanks(fileName);
82
+
83
+ if(fileName.endsWith('.xls') || fileName.endsWith('.xlsx') || fileName.endsWith('.csv')) {
84
+ if (fileName.includes('.xls')) {
85
+ fileName = fileName.replaceAll('.xls', '.xlsx');
86
+ }
87
+
88
+ if (fileName.includes('.csv')) {
89
+ fileName = fileName.replaceAll('.csv', '.xlsx');
90
+ }
91
+ }
92
+
93
+ else {
94
+ fileName += '.xlsx';
95
+ }
96
+
97
+ const WORK_SHEET = XLSX.utils.json_to_sheet(data);
98
+ const WORK_BOOK = XLSX.utils.book_new();
99
+ XLSX.utils.book_append_sheet(WORK_BOOK, WORK_SHEET, 'Sheet1');
100
+ XLSX.writeFile(WORK_BOOK, fileName);
101
+ }
102
+
103
+
104
+ /** Convert file to string base64 */
105
+ public static ConvertToBase64(file: File) {
106
+ return new Promise<string>(Resolve => {
107
+ const reader = new FileReader();
108
+ reader.readAsDataURL(file);
109
+
110
+ reader.onload = () => {
111
+ Resolve(reader.result?.toString() || '');
112
+ }
113
+
114
+ reader.onerror = () => {
115
+ Resolve('');
116
+ }
117
+ });
118
+ }
119
+ }
@@ -0,0 +1,55 @@
1
+ import { Observable } from "rxjs";
2
+
3
+ export interface IScreenSize {
4
+ width: number;
5
+ height: number;
6
+ breakpoin: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
7
+ }
8
+
9
+ export class Screen {
10
+
11
+ public static get WINDOW_WIDTH(): number {
12
+ return window.innerWidth;
13
+ }
14
+
15
+
16
+ public static get WINDOW_HEIGHT(): number {
17
+ return window.innerHeight;
18
+ }
19
+
20
+
21
+ public static get DEVICE_WIDTH(): number {
22
+ return window.screen.width;
23
+ }
24
+
25
+
26
+ public static get DEVICE_HEIGHT(): number {
27
+ return window.screen.height;
28
+ }
29
+
30
+
31
+ public static get BREAKPOINT(): 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl' {
32
+ if (window.innerWidth < 576) return 'xs';
33
+ else if (window.innerWidth >= 576 && window.innerWidth < 768) return 'sm';
34
+ else if (window.innerWidth >= 768 && window.innerWidth < 992) return 'md';
35
+ else if (window.innerWidth >= 992 && window.innerWidth < 1200) return 'lg';
36
+ else if (window.innerWidth >= 1200 && window.innerWidth < 1400) return 'xl';
37
+ else return 'xxl';
38
+ }
39
+
40
+
41
+ /** */
42
+ public static Resize = new Observable<IScreenSize>(subscriber => {
43
+ window.addEventListener("load", () => {
44
+ window.dispatchEvent(new Event('resize'));
45
+ });
46
+
47
+ window.onresize = () => {
48
+ subscriber.next({
49
+ width: window.innerWidth,
50
+ height: window.innerHeight,
51
+ breakpoin: this.BREAKPOINT
52
+ });
53
+ }
54
+ });
55
+ }
package/Tools/index.ts ADDED
@@ -0,0 +1,212 @@
1
+ import { Guid } from "guid-typescript";
2
+ import { signal } from "@angular/core";
3
+ const reference_signal = signal<any>({});
4
+
5
+ export const Tools = {
6
+ /** Generate a Guid */
7
+ GetGuid: (seed: string = 'coer-system') => {
8
+ return `${seed}-${Guid.create().toString()}`;
9
+ },
10
+
11
+
12
+ /** Returns true if the value is null or undefined, false otherwise */
13
+ IsNull: (value: any): boolean => {
14
+ if (value === undefined) return true;
15
+ if (value === null) return true;
16
+ return false;
17
+ },
18
+
19
+
20
+ /** Returns true if the value is not null or undefined, false otherwise */
21
+ IsNotNull: (value: any): boolean => {
22
+ if (value === undefined) return false;
23
+ if (value === null) return false;
24
+ return true;
25
+ },
26
+
27
+
28
+ /** Returns true if the value is null or undefined or contains only whitespace, false otherwise */
29
+ IsOnlyWhiteSpace: (value: any): boolean => {
30
+ if (value === undefined) return true;
31
+ if (value === null) return true;
32
+ if((value as string).toString().trim() === '') return true;
33
+ return false;
34
+ },
35
+
36
+
37
+ /** Break reference of a object or array */
38
+ BreakReference: <T>(object: T): T => {
39
+ if (object === undefined) return undefined as T;
40
+ if (object === null) return null as T;
41
+ const OBJECT = JSON.parse(JSON.stringify(object))
42
+ return (Array.isArray(OBJECT)) ? [...OBJECT] : { ...OBJECT }
43
+ },
44
+
45
+
46
+ /** Clean extra whitespaces */
47
+ CleanUpBlanks: (text: string | number): string => {
48
+ if(Tools.IsNull(text)) return '';
49
+ let worlds: string[] = String(text).split(' ');
50
+ worlds = worlds.filter(x => x.length > 0);
51
+ return worlds.join(' ');
52
+ },
53
+
54
+
55
+ /** Get properties of an object */
56
+ GetObjectProperties: <T>(obj: T | null | undefined): string[] => {
57
+ const properties: string[] = [];
58
+ if(Tools.IsNull(obj)) return properties;
59
+ for(const property in obj) properties.push(String(property));
60
+ return properties;
61
+ },
62
+
63
+
64
+ /**
65
+ * Set an index and merge more arrays of the same type
66
+ * @returns A new array
67
+ * */
68
+ SetIndex: <T>(array: T[], ...args: T[][]): T[] => {
69
+ let index = 0;
70
+ for (const arg of args) {
71
+ array = Tools.BreakReference(array).concat(Tools.BreakReference(arg));
72
+ }
73
+
74
+ return Tools.BreakReference(array).map(item => Object.assign({ index: index++ }, item));
75
+ },
76
+
77
+
78
+ /** Set First Char To Lower */
79
+ FirstCharToLower: (text: string | null | undefined): string => {
80
+ if (Tools.IsNull(text)) return '';
81
+
82
+ const textArray: string[] = [];
83
+ for(let i = 0; i < text!.length; i++) {
84
+ if(i === 0) textArray.push(text![i].toLowerCase());
85
+ else textArray.push(text![i]);
86
+ }
87
+
88
+ return textArray.join('');
89
+ },
90
+
91
+
92
+ /** Set First Char To Upper */
93
+ FirstCharToUpper: (text: string | null | undefined): string => {
94
+ if (Tools.IsNull(text)) return '';
95
+
96
+ const textArray: string[] = [];
97
+ for(let i = 0; i < text!.length; i++) {
98
+ if(i === 0) textArray.push(text![i].toUpperCase());
99
+ else textArray.push(text![i]);
100
+ }
101
+
102
+ return textArray.join('');
103
+ },
104
+
105
+
106
+ /** Sort an array in ascending order by property */
107
+ SortBy: <T>(array: T[], property: string, propertyType: 'string' | 'number' = 'string'): T[] => {
108
+ switch (propertyType) {
109
+ case 'string': {
110
+ return array.sort((x: any, y: any) => {
111
+ if (String(x[property]).toUpperCase().trim() < String(y[property]).toUpperCase().trim()) return -1;
112
+ else if (String(x[property]).toUpperCase().trim() > String(y[property]).toUpperCase().trim()) return 1;
113
+ else return 0;
114
+ });
115
+ }
116
+
117
+ case 'number': {
118
+ return array.sort((x: any, y: any) => Number(x[property] - Number(y[property])));
119
+ }
120
+ }
121
+ },
122
+
123
+
124
+ /** Sort an array in descending order by property */
125
+ SortByDesc: <T>(array: T[], property: string, propertyType: 'string' | 'number' = 'string'): T[] => {
126
+ switch (propertyType) {
127
+ case 'string': {
128
+ return array.sort((x: any, y: any) => {
129
+ if (String(x[property]).toUpperCase().trim() < String(y[property]).toUpperCase().trim()) return 1;
130
+ else if (String(x[property]).toUpperCase().trim() > String(y[property]).toUpperCase().trim()) return -1;
131
+ else return 0;
132
+ });
133
+ }
134
+
135
+ case 'number': {
136
+ return array.sort((x: any, y: any) => Number(Number(y[property])) - x[property]);
137
+ }
138
+ }
139
+ },
140
+
141
+
142
+ /** Return a string with forman numeric */
143
+ GetNumericFormat: (value: string | number | null | undefined, decimals: number = 0): string => {
144
+ if (value == undefined
145
+ || value == null
146
+ || value.toString().trim() == ''
147
+ || isNaN(Number(value))) {
148
+ return '0';
149
+ }
150
+
151
+ let valueInteger = '';
152
+ let valueDecimal = '';
153
+ value = value.toString().replaceAll(' ', '');
154
+
155
+ if (value.includes('.') || (decimals > 0)) {
156
+ valueInteger = value.includes('.') ? value.split('.')[0] : value;
157
+
158
+ if (decimals > 0) {
159
+ const PADDING = decimals - valueDecimal.length;
160
+ valueDecimal = value.includes('.') ? value.split('.')[1] : '';
161
+ for(let i = 0; i < PADDING; i++) valueDecimal += '0';
162
+ valueDecimal = valueDecimal.substring(0, decimals);
163
+ valueDecimal = `.${valueDecimal}`;
164
+ }
165
+ }
166
+
167
+ else {
168
+ valueInteger = value;
169
+ }
170
+
171
+ let counter = 0;
172
+ const VALUE_INTEGER_ARRAY: string[] = [];
173
+ for(const char of valueInteger.split('').reverse()) {
174
+ if (counter == 3) {
175
+ VALUE_INTEGER_ARRAY.push(',');
176
+ counter = 0;
177
+ }
178
+
179
+ VALUE_INTEGER_ARRAY.push(char);
180
+ ++counter;
181
+ }
182
+
183
+ valueInteger = VALUE_INTEGER_ARRAY.reverse().join('');
184
+ return `${valueInteger}${valueDecimal}`;
185
+ },
186
+
187
+
188
+ /** Wait the time indicated */
189
+ Sleep: (milliseconds: number = 0, reference: string | null = null) => {
190
+ if (Tools.IsNull(reference)) {
191
+ return new Promise(Resolve => setTimeout(Resolve, milliseconds));
192
+ }
193
+
194
+ else return new Promise<void>(Resolve => {
195
+ reference = reference!.replaceAll(' ', '_').toLowerCase();
196
+
197
+ if (reference_signal().hasOwnProperty(reference)) {
198
+ clearInterval(reference_signal()[reference!]);
199
+ }
200
+
201
+ reference_signal.set(Object.assign(reference_signal(), {
202
+ [reference!]: setTimeout(() => {
203
+ Resolve();
204
+ clearInterval(reference_signal()[reference!]);
205
+ const _reference = { ...reference_signal() };
206
+ delete _reference[reference!];
207
+ reference_signal.set({ ..._reference });
208
+ }, milliseconds)
209
+ }));
210
+ });
211
+ }
212
+ };
package/index.ts CHANGED
@@ -1,127 +1,4 @@
1
- export const Tools = {
2
- /** Returns true if the value is null or undefined, false otherwise */
3
- IsNull: (value: any): boolean => {
4
- if (value === undefined) return true;
5
- if (value === null) return true;
6
- return false;
7
- },
8
-
9
-
10
- /** Returns true if the value is not null or undefined, false otherwise */
11
- IsNotNull: (value: any): boolean => {
12
- if (value === undefined) return false;
13
- if (value === null) return false;
14
- return true;
15
- },
16
-
17
-
18
- /** Break reference of a object or array */
19
- BreakReference: <T>(object: T): T => {
20
- if (object === undefined) return undefined as T;
21
- if (object === null) return null as T;
22
- const OBJECT = JSON.parse(JSON.stringify(object))
23
- return (Array.isArray(OBJECT)) ? [...OBJECT] : { ...OBJECT }
24
- },
25
-
26
-
27
- /** Clean extra whitespaces */
28
- CleanUpBlanks: (text: string | number): string => {
29
- if(Tools.IsNull(text)) return '';
30
- let worlds: string[] = String(text).split(' ');
31
- worlds = worlds.filter(x => x.length > 0);
32
- return worlds.join(' ');
33
- },
34
-
35
-
36
- /** Get properties of an object */
37
- GetObjectProperties: <T>(obj: T | null | undefined): string[] => {
38
- const properties: string[] = [];
39
- if(Tools.IsNull(obj)) return properties;
40
- for(const property in obj) properties.push(String(property));
41
- return properties;
42
- },
43
-
44
-
45
- /**
46
- * Set an index and merge more arrays of the same type
47
- * @returns A new array
48
- * */
49
- SetIndex: <T>(array: T[], ...args: T[][]): T[] => {
50
- let index = 0;
51
- for (const arg of args) {
52
- array = Tools.BreakReference(array).concat(Tools.BreakReference(arg));
53
- }
54
-
55
- return Tools.BreakReference(array).map(item => Object.assign({ index: index++ }, item));
56
- },
57
-
58
-
59
- /** Set First Char To Lower */
60
- FirstCharToLower: (text: string | null | undefined): string => {
61
- if (Tools.IsNull(text)) return '';
62
-
63
- const textArray: string[] = [];
64
- for(let i = 0; i < text!.length; i++) {
65
- if(i === 0) textArray.push(text![i].toLowerCase());
66
- else textArray.push(text![i]);
67
- }
68
-
69
- return textArray.join('');
70
- },
71
-
72
-
73
- /** Set First Char To Upper */
74
- FirstCharToUpper: (text: string | null | undefined): string => {
75
- if (Tools.IsNull(text)) return '';
76
-
77
- const textArray: string[] = [];
78
- for(let i = 0; i < text!.length; i++) {
79
- if(i === 0) textArray.push(text![i].toUpperCase());
80
- else textArray.push(text![i]);
81
- }
82
-
83
- return textArray.join('');
84
- },
85
-
86
-
87
- /**
88
- * Sort an array in ascending order by property
89
- * @returns A new array
90
- * */
91
- SortBy: <T>(array: T[], property: string, propertyType: 'string' | 'number' = 'string'): T[] => {
92
- switch (propertyType) {
93
- case 'string': {
94
- return [...array].sort((x: any, y: any) => {
95
- if (String(x[property]).toUpperCase().trim() < String(y[property]).toUpperCase().trim()) return -1;
96
- else if (String(x[property]).toUpperCase().trim() > String(y[property]).toUpperCase().trim()) return 1;
97
- else return 0;
98
- });
99
- }
100
-
101
- case 'number': {
102
- return array.sort((x: any, y: any) => Number(x[property] - Number(y[property])));
103
- }
104
- }
105
- },
106
-
107
-
108
- /**
109
- * Sort an array in descending order by property
110
- * @returns A new array
111
- * */
112
- SortByDesc: <T>(array: T[], property: string, propertyType: 'string' | 'number' = 'string'): T[] => {
113
- switch (propertyType) {
114
- case 'string': {
115
- return array.sort((x: any, y: any) => {
116
- if (String(x[property]).toUpperCase().trim() < String(y[property]).toUpperCase().trim()) return 1;
117
- else if (String(x[property]).toUpperCase().trim() > String(y[property]).toUpperCase().trim()) return -1;
118
- else return 0;
119
- });
120
- }
121
-
122
- case 'number': {
123
- return array.sort((x: any, y: any) => Number(Number(y[property])) - x[property]);
124
- }
125
- }
126
- }
127
- };
1
+ export * from './Tools'
2
+ export * from './Tools/DateTime.class';
3
+ export * from './Tools/Files.class';
4
+ export * from './Tools/Screen.class';
@@ -0,0 +1,5 @@
1
+ export interface IPatch {
2
+ op: 'remove' | 'add' | 'replace';
3
+ path: string;
4
+ value: any;
5
+ }
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "coer-elements",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "author": "Christian Omar Escamilla Rodríguez",
5
- "keywords": ["coer", "tools"],
5
+ "keywords": [
6
+ "coer",
7
+ "tools"
8
+ ],
6
9
  "description": "",
7
10
  "license": "MIT",
8
11
  "main": "dist_node/index.js",
@@ -16,9 +19,16 @@
16
19
  "build-browser": "tsc index.ts --outDir dist_browser --module ES6",
17
20
  "build-node": "tsc index.ts --outDir dist_node",
18
21
  "build": "npm run build-browser & npm run build-node",
19
- "publish": "npm publish --access public"
22
+ "push": "npm publish --access public"
23
+ },
24
+ "dependencies": {
25
+ "@angular/core": "^18.2.3",
26
+ "guid-typescript": "^1.0.9",
27
+ "moment": "^2.30.1",
28
+ "rxjs": "^7.8.1",
29
+ "xlsx": "^0.18.5"
20
30
  },
21
31
  "devDependencies": {
22
32
  "typescript": "^5.5.4"
23
33
  }
24
- }
34
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig to read more about this file */
4
+ "outDir": "./dist/out-tsc",
5
+ "strict": true,
6
+ "noImplicitOverride": true,
7
+ "noPropertyAccessFromIndexSignature": true,
8
+ "noImplicitReturns": true,
9
+ "noFallthroughCasesInSwitch": true,
10
+ "skipLibCheck": true,
11
+ "esModuleInterop": true,
12
+ "sourceMap": true,
13
+ "declaration": false,
14
+ "experimentalDecorators": true,
15
+ "moduleResolution": "node",
16
+ "importHelpers": true,
17
+ "target": "ES2022",
18
+ "module": "ES2022",
19
+ "useDefineForClassFields": false,
20
+ "lib": ["ES2022", "dom"],
21
+ "baseUrl": "./"
22
+ }
23
+ }