@ellipticltd/aml-utils 0.15.20 → 0.15.22

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 (67) hide show
  1. package/lib/errors/errors.d.ts +9 -0
  2. package/lib/errors/errors.js +19 -30
  3. package/lib/errors/errors.js.map +1 -0
  4. package/lib/file-parser/errors.d.ts +3 -0
  5. package/lib/file-parser/errors.js +11 -0
  6. package/lib/file-parser/errors.js.map +1 -0
  7. package/lib/file-parser/file-parser.d.ts +6 -0
  8. package/lib/file-parser/file-parser.js +60 -0
  9. package/lib/file-parser/file-parser.js.map +1 -0
  10. package/lib/file-parser/parse-row.d.ts +2 -0
  11. package/lib/file-parser/parse-row.js +40 -0
  12. package/lib/file-parser/parse-row.js.map +1 -0
  13. package/lib/file-parser/sanitizeRows.d.ts +5 -0
  14. package/lib/file-parser/sanitizeRows.js +15 -0
  15. package/lib/file-parser/sanitizeRows.js.map +1 -0
  16. package/lib/formatting/formatting.d.ts +2 -0
  17. package/lib/formatting/formatting.js +11 -12
  18. package/lib/formatting/formatting.js.map +1 -0
  19. package/lib/{index.ts → index.d.ts} +0 -1
  20. package/lib/index.js +21 -0
  21. package/lib/index.js.map +1 -0
  22. package/lib/middleware/middleware.d.ts +4 -0
  23. package/lib/middleware/middleware.js +14 -17
  24. package/lib/middleware/middleware.js.map +1 -0
  25. package/lib/orm-helpers/ormHelpers.d.ts +1 -0
  26. package/lib/orm-helpers/ormHelpers.js +7 -9
  27. package/lib/orm-helpers/ormHelpers.js.map +1 -0
  28. package/lib/structured-file-parser/errors.d.ts +12 -0
  29. package/lib/structured-file-parser/errors.js +33 -0
  30. package/lib/structured-file-parser/errors.js.map +1 -0
  31. package/lib/structured-file-parser/parse-row.d.ts +2 -0
  32. package/lib/structured-file-parser/parse-row.js +40 -0
  33. package/lib/structured-file-parser/parse-row.js.map +1 -0
  34. package/lib/structured-file-parser/sanitize-rows.d.ts +2 -0
  35. package/lib/structured-file-parser/sanitize-rows.js +14 -0
  36. package/lib/structured-file-parser/sanitize-rows.js.map +1 -0
  37. package/lib/structured-file-parser/structured-file-parser.d.ts +10 -0
  38. package/lib/structured-file-parser/structured-file-parser.js +96 -0
  39. package/lib/structured-file-parser/structured-file-parser.js.map +1 -0
  40. package/lib/types/types.d.ts +11 -0
  41. package/lib/types/types.js +190 -200
  42. package/lib/types/types.js.map +1 -0
  43. package/lib/validations/validations.d.ts +163 -0
  44. package/lib/validations/validations.js +468 -465
  45. package/lib/validations/validations.js.map +1 -0
  46. package/package.json +15 -1
  47. package/.eslintrc.js +0 -22
  48. package/dist/libs/aml-utils/libs/aml-utils/README.md +0 -19
  49. package/jest.config.ts +0 -13
  50. package/lib/errors/errors.spec.js +0 -49
  51. package/lib/file-parser/__tests/parse-row.spec.js +0 -34
  52. package/lib/file-parser/__tests/sanitize-rows.spec.js +0 -85
  53. package/lib/file-parser/errors.ts +0 -6
  54. package/lib/file-parser/file-parser.ts +0 -67
  55. package/lib/file-parser/parse-row.ts +0 -46
  56. package/lib/file-parser/sanitizeRows.ts +0 -22
  57. package/lib/formatting/formatting.spec.js +0 -47
  58. package/lib/orm-helpers/ormHelpers.spec.js +0 -47
  59. package/lib/structured-file-parser/errors.ts +0 -25
  60. package/lib/structured-file-parser/parse-row.ts +0 -46
  61. package/lib/structured-file-parser/sanitize-rows.ts +0 -17
  62. package/lib/structured-file-parser/structured-file-parser.ts +0 -129
  63. package/lib/validations/validations.spec.js +0 -869
  64. package/project.json +0 -55
  65. package/tsconfig.json +0 -13
  66. package/tsconfig.lib.json +0 -13
  67. package/tsconfig.spec.json +0 -10
@@ -1,129 +0,0 @@
1
- /* eslint-disable no-await-in-loop */
2
- /* eslint-disable no-restricted-syntax */
3
- import parseRow from './parse-row';
4
- import { InvalidFileError, TooManyRowsError, NoRowsError, InvalidHeadersError } from './errors';
5
- import sanitizeRows from './sanitize-rows';
6
-
7
- export type ProcessingEntry = {
8
- isValid: boolean;
9
- };
10
-
11
- type Dependencies<T extends ProcessingEntry> = {
12
- processRow: (cells: string[], headers: string[]) => T;
13
- };
14
-
15
- type CompleteDependencies<T extends ProcessingEntry, U extends string[]> = {
16
- processRow: (cells: string[], headers: U) => T;
17
- headers: U;
18
- };
19
-
20
- function chunk<T>(array: T[], size: number): T[][] {
21
- if (!array.length) {
22
- return [];
23
- }
24
- const head = array.slice(0, size);
25
- const tail = array.slice(size);
26
-
27
- return [head, ...chunk(tail, size)];
28
- }
29
-
30
- async function processChunk<T extends ProcessingEntry, U extends string[]>(
31
- parsedRows: string[][],
32
- dependencies: CompleteDependencies<T, U>
33
- ): Promise<T[]> {
34
- const { processRow, headers } = dependencies;
35
- return new Promise((resolve: (value: T[] | undefined) => void, reject: (error: Error) => void): void => {
36
- setTimeout(() => {
37
- try {
38
- const processedRows = parsedRows.map((row) => processRow(row, headers));
39
- resolve(processedRows);
40
- } catch (ex) {
41
- reject(ex);
42
- }
43
- }, 0);
44
- });
45
- }
46
-
47
- // a row is said to contain headers if two valid header names are present
48
- export const hasHeaderRow = (rows: string[][], requiredHeaderNames: string[]): boolean => {
49
- const firstRow = rows[0];
50
- if (!firstRow) {
51
- return false;
52
- }
53
- const foundIndex = firstRow.findIndex((value) => requiredHeaderNames.includes(value.toLowerCase()));
54
- if (foundIndex < 0) {
55
- return false;
56
- }
57
- const containsSecondHeader = firstRow
58
- .filter((_val, i) => i !== foundIndex)
59
- .some((value) => requiredHeaderNames.includes(value.toLowerCase()));
60
- return containsSecondHeader;
61
- };
62
-
63
- export const isHeaderRowValid = (rows: string[][], requiredHeaderNames: string[]): boolean => {
64
- const firstRow = rows[0];
65
- if (!firstRow) {
66
- return false;
67
- }
68
- return (
69
- requiredHeaderNames.every((header) => firstRow.map((value) => value.toLowerCase()).includes(header)) &&
70
- requiredHeaderNames.length === firstRow.length
71
- );
72
- };
73
-
74
- async function parseFile<T extends ProcessingEntry>(
75
- fileContent: string,
76
- maxRows: number,
77
- requiredHeaderNames: string[],
78
- dependencies: Dependencies<T>
79
- ): Promise<T[]> {
80
- if (fileContent === 'invalidFileType') {
81
- throw new InvalidFileError();
82
- }
83
-
84
- const rows = fileContent.split('\n');
85
- const parsedRows = rows.map(parseRow);
86
-
87
- const sanitizedRows = sanitizeRows(parsedRows);
88
-
89
- if (sanitizedRows.length > maxRows) {
90
- throw new TooManyRowsError();
91
- }
92
-
93
- if (sanitizedRows.length === 0) {
94
- throw new NoRowsError();
95
- }
96
-
97
- const containsHeaders = hasHeaderRow(sanitizedRows, requiredHeaderNames);
98
- if (containsHeaders && !isHeaderRowValid(sanitizedRows, requiredHeaderNames)) {
99
- throw new InvalidHeadersError();
100
- }
101
-
102
- const firstSanitizedRow = sanitizedRows[0];
103
-
104
- if (!firstSanitizedRow) {
105
- throw new NoRowsError();
106
- }
107
-
108
- const headers = containsHeaders ? firstSanitizedRow.map((value) => value.toLowerCase()) : requiredHeaderNames;
109
-
110
- const dataRows = containsHeaders ? sanitizedRows.splice(1) : sanitizedRows;
111
-
112
- if (dataRows.length === 0) {
113
- throw new NoRowsError();
114
- }
115
-
116
- const parsedRowChunks = chunk(dataRows, 30);
117
- let results: T[] = [];
118
- for (const currentRowChunk of parsedRowChunks) {
119
- const chunkResult = await processChunk(currentRowChunk, {
120
- ...dependencies,
121
- headers,
122
- });
123
- results = results.concat(chunkResult);
124
- }
125
-
126
- return results;
127
- }
128
-
129
- export default parseFile;