coer-elements 1.0.0 → 1.0.2

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.
@@ -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
+ }
@@ -1,4 +1,11 @@
1
+ import { Guid } from "guid-typescript";
2
+
1
3
  export const Tools = {
4
+ /** Generate a Guid */
5
+ GetGuid: (seed: string = 'coer-system') => {
6
+ return `${seed}-${Guid.create().toString()}`;
7
+ },
8
+
2
9
  /** Returns true if the value is null or undefined, false otherwise */
3
10
  IsNull: (value: any): boolean => {
4
11
  if (value === undefined) return true;
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './Tools'
2
+ export * from './Tools/Files.class';
3
+ export * from './Tools/DateTime.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.0",
3
+ "version": "1.0.2",
4
4
  "author": "Christian Omar Escamilla Rodríguez",
5
- "keywords": ["coer", "coer-system", "coer system"],
5
+ "keywords": [
6
+ "coer",
7
+ "tools"
8
+ ],
6
9
  "description": "",
7
10
  "license": "MIT",
8
11
  "main": "dist_node/index.js",
@@ -13,12 +16,17 @@
13
16
  "url": "https://github.com/Chris19910804/coer-elements"
14
17
  },
15
18
  "scripts": {
16
- "build-browser": "tsc src/index.ts --outDir dist-browser --module ES6",
17
- "build-node": "tsc src/index.ts --outDir dist_node",
18
- "build": "npm run build-browser & npm run build_node",
19
+ "build-browser": "tsc index.ts --outDir dist_browser --module ES6",
20
+ "build-node": "tsc index.ts --outDir dist_node",
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
+ }