coer-elements 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/Tools/DateTime.class.ts +27 -0
- package/Tools/Files.class.ts +102 -0
- package/Tools/index.ts +134 -0
- package/index.ts +3 -127
- package/interfaces/index.ts +5 -0
- package/package.json +11 -3
- package/tsconfig.json +23 -0
@@ -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,102 @@
|
|
1
|
+
import { Tools } from ".";
|
2
|
+
import * as XLSX from 'xlsx';
|
3
|
+
|
4
|
+
export class Files {
|
5
|
+
private 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
|
+
}
|
package/Tools/index.ts
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
import { Guid } from "guid-typescript";
|
2
|
+
|
3
|
+
export const Tools = {
|
4
|
+
/** Generate a Guid */
|
5
|
+
GetGuid: (seed: string = 'coer-system') => {
|
6
|
+
return `${seed}-${Guid.create().toString()}`;
|
7
|
+
},
|
8
|
+
|
9
|
+
/** Returns true if the value is null or undefined, false otherwise */
|
10
|
+
IsNull: (value: any): boolean => {
|
11
|
+
if (value === undefined) return true;
|
12
|
+
if (value === null) return true;
|
13
|
+
return false;
|
14
|
+
},
|
15
|
+
|
16
|
+
|
17
|
+
/** Returns true if the value is not null or undefined, false otherwise */
|
18
|
+
IsNotNull: (value: any): boolean => {
|
19
|
+
if (value === undefined) return false;
|
20
|
+
if (value === null) return false;
|
21
|
+
return true;
|
22
|
+
},
|
23
|
+
|
24
|
+
|
25
|
+
/** Break reference of a object or array */
|
26
|
+
BreakReference: <T>(object: T): T => {
|
27
|
+
if (object === undefined) return undefined as T;
|
28
|
+
if (object === null) return null as T;
|
29
|
+
const OBJECT = JSON.parse(JSON.stringify(object))
|
30
|
+
return (Array.isArray(OBJECT)) ? [...OBJECT] : { ...OBJECT }
|
31
|
+
},
|
32
|
+
|
33
|
+
|
34
|
+
/** Clean extra whitespaces */
|
35
|
+
CleanUpBlanks: (text: string | number): string => {
|
36
|
+
if(Tools.IsNull(text)) return '';
|
37
|
+
let worlds: string[] = String(text).split(' ');
|
38
|
+
worlds = worlds.filter(x => x.length > 0);
|
39
|
+
return worlds.join(' ');
|
40
|
+
},
|
41
|
+
|
42
|
+
|
43
|
+
/** Get properties of an object */
|
44
|
+
GetObjectProperties: <T>(obj: T | null | undefined): string[] => {
|
45
|
+
const properties: string[] = [];
|
46
|
+
if(Tools.IsNull(obj)) return properties;
|
47
|
+
for(const property in obj) properties.push(String(property));
|
48
|
+
return properties;
|
49
|
+
},
|
50
|
+
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Set an index and merge more arrays of the same type
|
54
|
+
* @returns A new array
|
55
|
+
* */
|
56
|
+
SetIndex: <T>(array: T[], ...args: T[][]): T[] => {
|
57
|
+
let index = 0;
|
58
|
+
for (const arg of args) {
|
59
|
+
array = Tools.BreakReference(array).concat(Tools.BreakReference(arg));
|
60
|
+
}
|
61
|
+
|
62
|
+
return Tools.BreakReference(array).map(item => Object.assign({ index: index++ }, item));
|
63
|
+
},
|
64
|
+
|
65
|
+
|
66
|
+
/** Set First Char To Lower */
|
67
|
+
FirstCharToLower: (text: string | null | undefined): string => {
|
68
|
+
if (Tools.IsNull(text)) return '';
|
69
|
+
|
70
|
+
const textArray: string[] = [];
|
71
|
+
for(let i = 0; i < text!.length; i++) {
|
72
|
+
if(i === 0) textArray.push(text![i].toLowerCase());
|
73
|
+
else textArray.push(text![i]);
|
74
|
+
}
|
75
|
+
|
76
|
+
return textArray.join('');
|
77
|
+
},
|
78
|
+
|
79
|
+
|
80
|
+
/** Set First Char To Upper */
|
81
|
+
FirstCharToUpper: (text: string | null | undefined): string => {
|
82
|
+
if (Tools.IsNull(text)) return '';
|
83
|
+
|
84
|
+
const textArray: string[] = [];
|
85
|
+
for(let i = 0; i < text!.length; i++) {
|
86
|
+
if(i === 0) textArray.push(text![i].toUpperCase());
|
87
|
+
else textArray.push(text![i]);
|
88
|
+
}
|
89
|
+
|
90
|
+
return textArray.join('');
|
91
|
+
},
|
92
|
+
|
93
|
+
|
94
|
+
/**
|
95
|
+
* Sort an array in ascending order by property
|
96
|
+
* @returns A new array
|
97
|
+
* */
|
98
|
+
SortBy: <T>(array: T[], property: string, propertyType: 'string' | 'number' = 'string'): T[] => {
|
99
|
+
switch (propertyType) {
|
100
|
+
case 'string': {
|
101
|
+
return [...array].sort((x: any, y: any) => {
|
102
|
+
if (String(x[property]).toUpperCase().trim() < String(y[property]).toUpperCase().trim()) return -1;
|
103
|
+
else if (String(x[property]).toUpperCase().trim() > String(y[property]).toUpperCase().trim()) return 1;
|
104
|
+
else return 0;
|
105
|
+
});
|
106
|
+
}
|
107
|
+
|
108
|
+
case 'number': {
|
109
|
+
return array.sort((x: any, y: any) => Number(x[property] - Number(y[property])));
|
110
|
+
}
|
111
|
+
}
|
112
|
+
},
|
113
|
+
|
114
|
+
|
115
|
+
/**
|
116
|
+
* Sort an array in descending order by property
|
117
|
+
* @returns A new array
|
118
|
+
* */
|
119
|
+
SortByDesc: <T>(array: T[], property: string, propertyType: 'string' | 'number' = 'string'): T[] => {
|
120
|
+
switch (propertyType) {
|
121
|
+
case 'string': {
|
122
|
+
return array.sort((x: any, y: any) => {
|
123
|
+
if (String(x[property]).toUpperCase().trim() < String(y[property]).toUpperCase().trim()) return 1;
|
124
|
+
else if (String(x[property]).toUpperCase().trim() > String(y[property]).toUpperCase().trim()) return -1;
|
125
|
+
else return 0;
|
126
|
+
});
|
127
|
+
}
|
128
|
+
|
129
|
+
case 'number': {
|
130
|
+
return array.sort((x: any, y: any) => Number(Number(y[property])) - x[property]);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
};
|
package/index.ts
CHANGED
@@ -1,127 +1,3 @@
|
|
1
|
-
export
|
2
|
-
|
3
|
-
|
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/Files.class';
|
3
|
+
export * from './Tools/DateTime.class';
|
package/package.json
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "coer-elements",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.2",
|
4
4
|
"author": "Christian Omar Escamilla Rodríguez",
|
5
|
-
"keywords": [
|
5
|
+
"keywords": [
|
6
|
+
"coer",
|
7
|
+
"tools"
|
8
|
+
],
|
6
9
|
"description": "",
|
7
10
|
"license": "MIT",
|
8
11
|
"main": "dist_node/index.js",
|
@@ -18,7 +21,12 @@
|
|
18
21
|
"build": "npm run build-browser & npm run build-node",
|
19
22
|
"publish": "npm publish --access public"
|
20
23
|
},
|
24
|
+
"dependencies": {
|
25
|
+
"guid-typescript": "^1.0.9",
|
26
|
+
"moment": "^2.30.1",
|
27
|
+
"xlsx": "^0.18.5"
|
28
|
+
},
|
21
29
|
"devDependencies": {
|
22
30
|
"typescript": "^5.5.4"
|
23
31
|
}
|
24
|
-
}
|
32
|
+
}
|
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
|
+
}
|