@ellipticltd/aml-utils 0.15.45 → 0.15.46

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