@ellipticltd/aml-utils 0.16.5 → 0.16.6

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 (47) hide show
  1. package/README.md +19 -0
  2. package/lib/errors/errors.d.ts +9 -0
  3. package/lib/errors/errors.js +42 -0
  4. package/lib/errors/errors.js.map +1 -0
  5. package/lib/file-parser/errors.d.ts +3 -0
  6. package/lib/file-parser/errors.js +11 -0
  7. package/lib/file-parser/errors.js.map +1 -0
  8. package/lib/file-parser/file-parser.d.ts +6 -0
  9. package/lib/file-parser/file-parser.js +59 -0
  10. package/lib/file-parser/file-parser.js.map +1 -0
  11. package/lib/file-parser/parse-row.d.ts +2 -0
  12. package/lib/file-parser/parse-row.js +40 -0
  13. package/lib/file-parser/parse-row.js.map +1 -0
  14. package/lib/file-parser/sanitizeRows.d.ts +5 -0
  15. package/lib/file-parser/sanitizeRows.js +15 -0
  16. package/lib/file-parser/sanitizeRows.js.map +1 -0
  17. package/lib/formatting/formatting.d.ts +2 -0
  18. package/lib/formatting/formatting.js +17 -0
  19. package/lib/formatting/formatting.js.map +1 -0
  20. package/lib/index.d.ts +9 -0
  21. package/lib/index.js +21 -0
  22. package/lib/index.js.map +1 -0
  23. package/lib/middleware/middleware.d.ts +4 -0
  24. package/lib/middleware/middleware.js +22 -0
  25. package/lib/middleware/middleware.js.map +1 -0
  26. package/lib/orm-helpers/ormHelpers.d.ts +1 -0
  27. package/lib/orm-helpers/ormHelpers.js +17 -0
  28. package/lib/orm-helpers/ormHelpers.js.map +1 -0
  29. package/lib/structured-file-parser/errors.d.ts +12 -0
  30. package/lib/structured-file-parser/errors.js +33 -0
  31. package/lib/structured-file-parser/errors.js.map +1 -0
  32. package/lib/structured-file-parser/parse-row.d.ts +2 -0
  33. package/lib/structured-file-parser/parse-row.js +40 -0
  34. package/lib/structured-file-parser/parse-row.js.map +1 -0
  35. package/lib/structured-file-parser/sanitize-rows.d.ts +2 -0
  36. package/lib/structured-file-parser/sanitize-rows.js +14 -0
  37. package/lib/structured-file-parser/sanitize-rows.js.map +1 -0
  38. package/lib/structured-file-parser/structured-file-parser.d.ts +10 -0
  39. package/lib/structured-file-parser/structured-file-parser.js +95 -0
  40. package/lib/structured-file-parser/structured-file-parser.js.map +1 -0
  41. package/lib/types/types.d.ts +11 -0
  42. package/lib/types/types.js +201 -0
  43. package/lib/types/types.js.map +1 -0
  44. package/lib/validations/validations.d.ts +215 -0
  45. package/lib/validations/validations.js +551 -0
  46. package/lib/validations/validations.js.map +1 -0
  47. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # aml-utils
2
+
3
+ This lib contains some utility functions which originally lived in aml-utils repo.
4
+
5
+ ## Running unit tests
6
+
7
+ Run `npx nx test aml-utils` to execute the unit tests via [Jest](https://jestjs.io).
8
+
9
+ ## Running lint
10
+
11
+ Run `npx nx lint aml-utils` to execute the lint via [ESLint](https://eslint.org/).
12
+
13
+ ## Building
14
+
15
+ Run `npx nx build aml-utils` to build the package
16
+
17
+ ## Publishing
18
+ Run `npx nx publish aml-utils` to build and publish the package
19
+ You can also publish temp version of the package to npm with a tag by running `npx nx publish aml-utils -- --tag "your-tag-name"`
@@ -0,0 +1,9 @@
1
+ export const RequestError: any;
2
+ export const ServerError: any;
3
+ export const Forbidden: any;
4
+ export const Unauthorized: any;
5
+ export const BadRequest: any;
6
+ export const NotFound: any;
7
+ export const ConflictError: any;
8
+ export const InvalidArguments: any;
9
+ export const ServerTimeout: any;
@@ -0,0 +1,42 @@
1
+ const create = require('create-error');
2
+ const AppError = create('AppError');
3
+ const RequestError = create(AppError, 'RequestError');
4
+ RequestError.toJSON = () => ({
5
+ message: this.message,
6
+ });
7
+ const Forbidden = create(RequestError, 'ForbiddenError', {
8
+ status: 403,
9
+ });
10
+ const Unauthorized = create(RequestError, 'UnauthorizedError', {
11
+ status: 401,
12
+ });
13
+ const BadRequest = create(RequestError, 'BadRequestError', {
14
+ status: 400,
15
+ });
16
+ const NotFound = create(RequestError, 'NotFoundError', {
17
+ status: 404,
18
+ });
19
+ const ConflictError = create(RequestError, 'ConflictError', {
20
+ status: 409,
21
+ });
22
+ const ServerError = create(RequestError, 'ServerError', {
23
+ status: 500,
24
+ });
25
+ const InvalidArguments = create(RequestError, 'InvalidArgumentsError', {
26
+ status: 500,
27
+ });
28
+ const ServerTimeout = create(RequestError, 'ServerTimeoutError', {
29
+ status: 503,
30
+ });
31
+ module.exports = {
32
+ RequestError,
33
+ ServerError,
34
+ Forbidden,
35
+ Unauthorized,
36
+ BadRequest,
37
+ NotFound,
38
+ ConflictError,
39
+ InvalidArguments,
40
+ ServerTimeout,
41
+ };
42
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/errors/errors.js"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;AAEvC,MAAM,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;AAEpC,MAAM,YAAY,GAAG,MAAM,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;AAEtD,YAAY,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC,CAAC;IAC3B,OAAO,EAAE,IAAI,CAAC,OAAO;CACtB,CAAC,CAAC;AAEH,MAAM,SAAS,GAAG,MAAM,CAAC,YAAY,EAAE,gBAAgB,EAAE;IACvD,MAAM,EAAE,GAAG;CACZ,CAAC,CAAC;AAEH,MAAM,YAAY,GAAG,MAAM,CAAC,YAAY,EAAE,mBAAmB,EAAE;IAC7D,MAAM,EAAE,GAAG;CACZ,CAAC,CAAC;AAEH,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,EAAE,iBAAiB,EAAE;IACzD,MAAM,EAAE,GAAG;CACZ,CAAC,CAAC;AAEH,MAAM,QAAQ,GAAG,MAAM,CAAC,YAAY,EAAE,eAAe,EAAE;IACrD,MAAM,EAAE,GAAG;CACZ,CAAC,CAAC;AAEH,MAAM,aAAa,GAAG,MAAM,CAAC,YAAY,EAAE,eAAe,EAAE;IAC1D,MAAM,EAAE,GAAG;CACZ,CAAC,CAAC;AAEH,MAAM,WAAW,GAAG,MAAM,CAAC,YAAY,EAAE,aAAa,EAAE;IACtD,MAAM,EAAE,GAAG;CACZ,CAAC,CAAC;AAEH,MAAM,gBAAgB,GAAG,MAAM,CAAC,YAAY,EAAE,uBAAuB,EAAE;IACrE,MAAM,EAAE,GAAG;CACZ,CAAC,CAAC;AAEH,MAAM,aAAa,GAAG,MAAM,CAAC,YAAY,EAAE,oBAAoB,EAAE;IAC/D,MAAM,EAAE,GAAG;CACZ,CAAC,CAAC;AAEH,MAAM,CAAC,OAAO,GAAG;IACf,YAAY;IACZ,WAAW;IACX,SAAS;IACT,YAAY;IACZ,UAAU;IACV,QAAQ;IACR,aAAa;IACb,gBAAgB;IAChB,aAAa;CACd,CAAC","sourcesContent":["const create = require('create-error');\n\nconst AppError = create('AppError');\n\nconst RequestError = create(AppError, 'RequestError');\n\nRequestError.toJSON = () => ({\n message: this.message,\n});\n\nconst Forbidden = create(RequestError, 'ForbiddenError', {\n status: 403,\n});\n\nconst Unauthorized = create(RequestError, 'UnauthorizedError', {\n status: 401,\n});\n\nconst BadRequest = create(RequestError, 'BadRequestError', {\n status: 400,\n});\n\nconst NotFound = create(RequestError, 'NotFoundError', {\n status: 404,\n});\n\nconst ConflictError = create(RequestError, 'ConflictError', {\n status: 409,\n});\n\nconst ServerError = create(RequestError, 'ServerError', {\n status: 500,\n});\n\nconst InvalidArguments = create(RequestError, 'InvalidArgumentsError', {\n status: 500,\n});\n\nconst ServerTimeout = create(RequestError, 'ServerTimeoutError', {\n status: 503,\n});\n\nmodule.exports = {\n RequestError,\n ServerError,\n Forbidden,\n Unauthorized,\n BadRequest,\n NotFound,\n ConflictError,\n InvalidArguments,\n ServerTimeout,\n};\n"]}
@@ -0,0 +1,3 @@
1
+ export declare class TooManyRowsError extends Error {
2
+ constructor();
3
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TooManyRowsError = void 0;
4
+ class TooManyRowsError extends Error {
5
+ constructor() {
6
+ super('More than configured number of rows imported');
7
+ this.name = 'TooManyRowsError';
8
+ }
9
+ }
10
+ exports.TooManyRowsError = TooManyRowsError;
11
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/file-parser/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,gBAAiB,SAAQ,KAAK;IACzC;QACE,KAAK,CAAC,8CAA8C,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AALD,4CAKC","sourcesContent":["export class TooManyRowsError extends Error {\n constructor() {\n super('More than configured number of rows imported');\n this.name = 'TooManyRowsError';\n }\n}\n"]}
@@ -0,0 +1,6 @@
1
+ import { ProcessingEntry } from './sanitizeRows';
2
+ type Dependencies<T extends ProcessingEntry> = {
3
+ processRow: (cells: string[]) => T;
4
+ };
5
+ declare function parseFile<T extends ProcessingEntry>(fileContent: string, maxRows: number, dependencies: Dependencies<T>): Promise<T[]>;
6
+ export default parseFile;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const parse_row_1 = tslib_1.__importDefault(require("./parse-row"));
5
+ const errors_1 = require("./errors");
6
+ const sanitizeRows_1 = tslib_1.__importDefault(require("./sanitizeRows"));
7
+ function chunk(array, size) {
8
+ if (!array.length) {
9
+ return [];
10
+ }
11
+ const head = array.slice(0, size);
12
+ const tail = array.slice(size);
13
+ return [head, ...chunk(tail, size)];
14
+ }
15
+ function processChunk(parsedRows, dependencies) {
16
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
17
+ const { processRow } = dependencies;
18
+ return new Promise((resolve, reject) => {
19
+ setTimeout(() => {
20
+ try {
21
+ const processedRows = parsedRows.map(processRow);
22
+ resolve(processedRows);
23
+ }
24
+ catch (ex) {
25
+ if (ex instanceof Error) {
26
+ reject(ex);
27
+ }
28
+ }
29
+ }, 0);
30
+ });
31
+ });
32
+ }
33
+ function parseFile(fileContent, maxRows, dependencies) {
34
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
35
+ const { processRow } = dependencies;
36
+ if (fileContent === 'invalidFileType') {
37
+ throw Error('Invalid file type');
38
+ }
39
+ const rows = fileContent.split('\n');
40
+ const parsedRows = rows.map(parse_row_1.default);
41
+ const sanitizedRows = (0, sanitizeRows_1.default)(parsedRows, processRow);
42
+ if (sanitizedRows.length > maxRows) {
43
+ throw new errors_1.TooManyRowsError();
44
+ }
45
+ if (sanitizedRows.length === 0) {
46
+ throw new Error('No rows');
47
+ }
48
+ const parsedRowChunks = chunk(sanitizedRows, 30);
49
+ let results = [];
50
+ // eslint-disable-next-line no-restricted-syntax
51
+ for (const currentRowChunk of parsedRowChunks) {
52
+ const chunkResult = yield processChunk(currentRowChunk, dependencies);
53
+ results = results.concat(chunkResult);
54
+ }
55
+ return results;
56
+ });
57
+ }
58
+ exports.default = parseFile;
59
+ //# sourceMappingURL=file-parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-parser.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/file-parser/file-parser.ts"],"names":[],"mappings":";;;AAAA,oEAAmC;AACnC,qCAA4C;AAC5C,0EAA+D;AAM/D,SAAS,KAAK,CAAI,KAAU,EAAE,IAAY;IACxC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;QACjB,OAAO,EAAE,CAAC;KACX;IACD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAE/B,OAAO,CAAC,IAAI,EAAE,GAAG,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AACtC,CAAC;AAED,SAAe,YAAY,CAA4B,UAAsB,EAAE,YAA6B;;QAC1G,MAAM,EAAE,UAAU,EAAE,GAAG,YAAY,CAAC;QACpC,OAAO,IAAI,OAAO,CAAC,CAAC,OAA6B,EAAE,MAA8B,EAAQ,EAAE;YACzF,UAAU,CAAC,GAAG,EAAE;gBACd,IAAI;oBACF,MAAM,aAAa,GAAG,UAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;oBACjD,OAAO,CAAC,aAAa,CAAC,CAAC;iBACxB;gBAAC,OAAO,EAAE,EAAE;oBACX,IAAI,EAAE,YAAY,KAAK,EAAE;wBACvB,MAAM,CAAC,EAAE,CAAC,CAAC;qBACZ;iBACF;YACH,CAAC,EAAE,CAAC,CAAC,CAAC;QACR,CAAC,CAAC,CAAC;IACL,CAAC;CAAA;AAED,SAAe,SAAS,CAA4B,WAAmB,EAAE,OAAe,EAAE,YAA6B;;QACrH,MAAM,EAAE,UAAU,EAAE,GAAG,YAAY,CAAC;QAEpC,IAAI,WAAW,KAAK,iBAAiB,EAAE;YACrC,MAAM,KAAK,CAAC,mBAAmB,CAAC,CAAC;SAClC;QAED,MAAM,IAAI,GAAG,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACrC,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,mBAAQ,CAAC,CAAC;QAEtC,MAAM,aAAa,GAAG,IAAA,sBAAY,EAAC,UAAU,EAAE,UAAU,CAAC,CAAC;QAE3D,IAAI,aAAa,CAAC,MAAM,GAAG,OAAO,EAAE;YAClC,MAAM,IAAI,yBAAgB,EAAE,CAAC;SAC9B;QAED,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;SAC5B;QAED,MAAM,eAAe,GAAG,KAAK,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;QACjD,IAAI,OAAO,GAAQ,EAAE,CAAC;QACtB,gDAAgD;QAChD,KAAK,MAAM,eAAe,IAAI,eAAe,EAAE;YAC7C,MAAM,WAAW,GAAG,MAAM,YAAY,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;YACtE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;SACvC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;CAAA;AAED,kBAAe,SAAS,CAAC","sourcesContent":["import parseRow from './parse-row';\nimport { TooManyRowsError } from './errors';\nimport sanitizeRows, { ProcessingEntry } from './sanitizeRows';\n\ntype Dependencies<T extends ProcessingEntry> = {\n processRow: (cells: string[]) => T;\n};\n\nfunction chunk<T>(array: T[], size: number): T[][] {\n if (!array.length) {\n return [];\n }\n const head = array.slice(0, size);\n const tail = array.slice(size);\n\n return [head, ...chunk(tail, size)];\n}\n\nasync function processChunk<T extends ProcessingEntry>(parsedRows: string[][], dependencies: Dependencies<T>): Promise<T[]> {\n const { processRow } = dependencies;\n return new Promise((resolve: (value: T[]) => void, reject: (error: Error) => void): void => {\n setTimeout(() => {\n try {\n const processedRows = parsedRows.map(processRow);\n resolve(processedRows);\n } catch (ex) {\n if (ex instanceof Error) {\n reject(ex);\n }\n }\n }, 0);\n });\n}\n\nasync function parseFile<T extends ProcessingEntry>(fileContent: string, maxRows: number, dependencies: Dependencies<T>): Promise<T[]> {\n const { processRow } = dependencies;\n\n if (fileContent === 'invalidFileType') {\n throw Error('Invalid file type');\n }\n\n const rows = fileContent.split('\\n');\n const parsedRows = rows.map(parseRow);\n\n const sanitizedRows = sanitizeRows(parsedRows, processRow);\n\n if (sanitizedRows.length > maxRows) {\n throw new TooManyRowsError();\n }\n\n if (sanitizedRows.length === 0) {\n throw new Error('No rows');\n }\n\n const parsedRowChunks = chunk(sanitizedRows, 30);\n let results: T[] = [];\n // eslint-disable-next-line no-restricted-syntax\n for (const currentRowChunk of parsedRowChunks) {\n const chunkResult = await processChunk(currentRowChunk, dependencies);\n results = results.concat(chunkResult);\n }\n\n return results;\n}\n\nexport default parseFile;\n"]}
@@ -0,0 +1,2 @@
1
+ declare const parseRow: (row: string) => string[];
2
+ export default parseRow;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fp_1 = require("lodash/fp");
4
+ const parseRow = (row) => {
5
+ const trySplitByTab = (line) => {
6
+ if (typeof line === 'string' && line.match('\t')) {
7
+ return line.split('\t');
8
+ }
9
+ return line;
10
+ };
11
+ const trySplitByComa = (line) => {
12
+ if (typeof line === 'string' && line.match(',')) {
13
+ return line.split(',');
14
+ }
15
+ return line;
16
+ };
17
+ const trySplitByMultipleSpaces = (line) => {
18
+ if (typeof line === 'string' && line.match(/ {3,}/)) {
19
+ return line.split(/ {3,}/);
20
+ }
21
+ return line;
22
+ };
23
+ const trySplitBySpace = (line) => {
24
+ if (typeof line === 'string' && line.match(' ')) {
25
+ return line.split(' ');
26
+ }
27
+ return line;
28
+ };
29
+ const trySplitByNothing = (line) => {
30
+ if (typeof line === 'string') {
31
+ return [line];
32
+ }
33
+ return line;
34
+ };
35
+ const splitRow = (0, fp_1.pipe)(trySplitByTab, trySplitByComa, trySplitByMultipleSpaces, trySplitBySpace, trySplitByNothing);
36
+ const cells = splitRow(row);
37
+ return cells.map((c) => c.trim());
38
+ };
39
+ exports.default = parseRow;
40
+ //# sourceMappingURL=parse-row.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-row.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/file-parser/parse-row.ts"],"names":[],"mappings":";;AAAA,kCAAiC;AAEjC,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAY,EAAE;IACzC,MAAM,aAAa,GAAG,CAAC,IAAuB,EAAqB,EAAE;QACnE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;YAChD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;SACzB;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,cAAc,GAAG,CAAC,IAAuB,EAAqB,EAAE;QACpE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;YAC/C,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;SACxB;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,wBAAwB,GAAG,CAAC,IAAuB,EAAqB,EAAE;QAC9E,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE;YACnD,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;SAC5B;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,eAAe,GAAG,CAAC,IAAuB,EAAqB,EAAE;QACrE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;YAC/C,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;SACxB;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,iBAAiB,GAAG,CAAC,IAAuB,EAAY,EAAE;QAC9D,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;YAC5B,OAAO,CAAC,IAAI,CAAC,CAAC;SACf;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,QAAQ,GAAG,IAAA,SAAI,EAAC,aAAa,EAAE,cAAc,EAAE,wBAAwB,EAAE,eAAe,EAAE,iBAAiB,CAAC,CAAC;IAEnH,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;IAE5B,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,kBAAe,QAAQ,CAAC","sourcesContent":["import { pipe } from 'lodash/fp';\n\nconst parseRow = (row: string): string[] => {\n const trySplitByTab = (line: string | string[]): string | string[] => {\n if (typeof line === 'string' && line.match('\\t')) {\n return line.split('\\t');\n }\n return line;\n };\n\n const trySplitByComa = (line: string | string[]): string | string[] => {\n if (typeof line === 'string' && line.match(',')) {\n return line.split(',');\n }\n return line;\n };\n\n const trySplitByMultipleSpaces = (line: string | string[]): string | string[] => {\n if (typeof line === 'string' && line.match(/ {3,}/)) {\n return line.split(/ {3,}/);\n }\n return line;\n };\n\n const trySplitBySpace = (line: string | string[]): string | string[] => {\n if (typeof line === 'string' && line.match(' ')) {\n return line.split(' ');\n }\n return line;\n };\n\n const trySplitByNothing = (line: string | string[]): string[] => {\n if (typeof line === 'string') {\n return [line];\n }\n return line;\n };\n\n const splitRow = pipe(trySplitByTab, trySplitByComa, trySplitByMultipleSpaces, trySplitBySpace, trySplitByNothing);\n\n const cells = splitRow(row);\n\n return cells.map((c: string) => c.trim());\n};\n\nexport default parseRow;\n"]}
@@ -0,0 +1,5 @@
1
+ export type ProcessingEntry = {
2
+ isValid: boolean;
3
+ };
4
+ declare function sanitizeRows<T extends ProcessingEntry>(parsedRows: string[][], processRow: (row: string[]) => T): string[][];
5
+ export default sanitizeRows;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const sensicalRowCheck = (cells) => cells.some((cell) => !!cell);
4
+ function sanitizeRows(parsedRows, processRow) {
5
+ const firstSensicalRowIndex = parsedRows.findIndex(sensicalRowCheck);
6
+ const lastSensicalRowIndex = [...parsedRows].reverse().findIndex(sensicalRowCheck);
7
+ const sanitizedRows = parsedRows.splice(firstSensicalRowIndex, parsedRows.length - firstSensicalRowIndex - lastSensicalRowIndex);
8
+ const firstRow = processRow(sanitizedRows[0]);
9
+ if (firstRow.isValid) {
10
+ return sanitizedRows;
11
+ }
12
+ return sanitizedRows.splice(1);
13
+ }
14
+ exports.default = sanitizeRows;
15
+ //# sourceMappingURL=sanitizeRows.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitizeRows.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/file-parser/sanitizeRows.ts"],"names":[],"mappings":";;AAIA,MAAM,gBAAgB,GAAG,CAAC,KAAe,EAAW,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAY,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAE5F,SAAS,YAAY,CAA4B,UAAsB,EAAE,UAAgC;IACvG,MAAM,qBAAqB,GAAG,UAAU,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC;IAErE,MAAM,oBAAoB,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC;IAEnF,MAAM,aAAa,GAAG,UAAU,CAAC,MAAM,CAAC,qBAAqB,EAAE,UAAU,CAAC,MAAM,GAAG,qBAAqB,GAAG,oBAAoB,CAAC,CAAC;IAEjI,MAAM,QAAQ,GAAG,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;IAE9C,IAAI,QAAQ,CAAC,OAAO,EAAE;QACpB,OAAO,aAAa,CAAC;KACtB;IACD,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AACjC,CAAC;AAED,kBAAe,YAAY,CAAC","sourcesContent":["export type ProcessingEntry = {\n isValid: boolean;\n};\n\nconst sensicalRowCheck = (cells: string[]): boolean => cells.some((cell: string) => !!cell);\n\nfunction sanitizeRows<T extends ProcessingEntry>(parsedRows: string[][], processRow: (row: string[]) => T): string[][] {\n const firstSensicalRowIndex = parsedRows.findIndex(sensicalRowCheck);\n\n const lastSensicalRowIndex = [...parsedRows].reverse().findIndex(sensicalRowCheck);\n\n const sanitizedRows = parsedRows.splice(firstSensicalRowIndex, parsedRows.length - firstSensicalRowIndex - lastSensicalRowIndex);\n\n const firstRow = processRow(sanitizedRows[0]);\n\n if (firstRow.isValid) {\n return sanitizedRows;\n }\n return sanitizedRows.splice(1);\n}\n\nexport default sanitizeRows;\n"]}
@@ -0,0 +1,2 @@
1
+ export function rethrowError(fnName: any, e: any): string;
2
+ export function sqlEscapeWildcard(str: any): any;
@@ -0,0 +1,17 @@
1
+ const _ = require('lodash');
2
+ const rethrowError = (fnName, e) => {
3
+ let formattedError;
4
+ if (_.isPlainObject(e.error) || _.isArray(e.error)) {
5
+ formattedError = `\n${JSON.stringify(e.error, null, 2)}`;
6
+ }
7
+ else {
8
+ formattedError = e.error;
9
+ }
10
+ return `${fnName} - ${e.name}: ${e.statusCode} - ${formattedError}`;
11
+ };
12
+ const sqlEscapeWildcard = (str) => str.replace(/_|%|\\/g, (x) => `\\${x}`);
13
+ module.exports = {
14
+ rethrowError,
15
+ sqlEscapeWildcard,
16
+ };
17
+ //# sourceMappingURL=formatting.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatting.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/formatting/formatting.js"],"names":[],"mappings":"AAAA,MAAM,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAE5B,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;IACjC,IAAI,cAAc,CAAC;IACnB,IAAI,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE;QAClD,cAAc,GAAG,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;KAC1D;SAAM;QACL,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC;KAC1B;IACD,OAAO,GAAG,MAAM,MAAM,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,UAAU,MAAM,cAAc,EAAE,CAAC;AACtE,CAAC,CAAC;AAEF,MAAM,iBAAiB,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AAE3E,MAAM,CAAC,OAAO,GAAG;IACf,YAAY;IACZ,iBAAiB;CAClB,CAAC","sourcesContent":["const _ = require('lodash');\n\nconst rethrowError = (fnName, e) => {\n let formattedError;\n if (_.isPlainObject(e.error) || _.isArray(e.error)) {\n formattedError = `\\n${JSON.stringify(e.error, null, 2)}`;\n } else {\n formattedError = e.error;\n }\n return `${fnName} - ${e.name}: ${e.statusCode} - ${formattedError}`;\n};\n\nconst sqlEscapeWildcard = (str) => str.replace(/_|%|\\\\/g, (x) => `\\\\${x}`);\n\nmodule.exports = {\n rethrowError,\n sqlEscapeWildcard,\n};\n"]}
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import types from './types/types';
2
+ import validations from './validations/validations';
3
+ import errors from './errors/errors';
4
+ import formatting from './formatting/formatting';
5
+ import middleware from './middleware/middleware';
6
+ import ormHelpers from './orm-helpers/ormHelpers';
7
+ import fileParser from './file-parser/file-parser';
8
+ import structuredFileParser from './structured-file-parser/structured-file-parser';
9
+ export { types, validations, errors, formatting, fileParser, structuredFileParser, middleware, ormHelpers };
package/lib/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ormHelpers = exports.middleware = exports.structuredFileParser = exports.fileParser = exports.formatting = exports.errors = exports.validations = exports.types = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const types_1 = tslib_1.__importDefault(require("./types/types"));
6
+ exports.types = types_1.default;
7
+ const validations_1 = tslib_1.__importDefault(require("./validations/validations"));
8
+ exports.validations = validations_1.default;
9
+ const errors_1 = tslib_1.__importDefault(require("./errors/errors"));
10
+ exports.errors = errors_1.default;
11
+ const formatting_1 = tslib_1.__importDefault(require("./formatting/formatting"));
12
+ exports.formatting = formatting_1.default;
13
+ const middleware_1 = tslib_1.__importDefault(require("./middleware/middleware"));
14
+ exports.middleware = middleware_1.default;
15
+ const ormHelpers_1 = tslib_1.__importDefault(require("./orm-helpers/ormHelpers"));
16
+ exports.ormHelpers = ormHelpers_1.default;
17
+ const file_parser_1 = tslib_1.__importDefault(require("./file-parser/file-parser"));
18
+ exports.fileParser = file_parser_1.default;
19
+ const structured_file_parser_1 = tslib_1.__importDefault(require("./structured-file-parser/structured-file-parser"));
20
+ exports.structuredFileParser = structured_file_parser_1.default;
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/aml-utils/lib/index.ts"],"names":[],"mappings":";;;;AAAA,kEAAkC;AASzB,gBATF,eAAK,CASE;AARd,oFAAoD;AAQpC,sBART,qBAAW,CAQS;AAP3B,qEAAqC;AAOR,iBAPtB,gBAAM,CAOsB;AANnC,iFAAiD;AAMZ,qBAN9B,oBAAU,CAM8B;AAL/C,iFAAiD;AAKkC,qBAL5E,oBAAU,CAK4E;AAJ7F,kFAAkD;AAI6C,qBAJxF,oBAAU,CAIwF;AAHzG,oFAAmD;AAGF,qBAH1C,qBAAU,CAG0C;AAF3D,qHAAmF;AAEtB,+BAFtD,gCAAoB,CAEsD","sourcesContent":["import types from './types/types';\nimport validations from './validations/validations';\nimport errors from './errors/errors';\nimport formatting from './formatting/formatting';\nimport middleware from './middleware/middleware';\nimport ormHelpers from './orm-helpers/ormHelpers';\nimport fileParser from './file-parser/file-parser';\nimport structuredFileParser from './structured-file-parser/structured-file-parser';\n\nexport { types, validations, errors, formatting, fileParser, structuredFileParser, middleware, ormHelpers };\n"]}
@@ -0,0 +1,4 @@
1
+ export function ensureType(type: any): (req: any, res: any, next: any, id: any, name: any) => any;
2
+ export function ensureUUID(req: any, res: any, next: any, id: any, name: any): any;
3
+ export function ensureHex32(req: any, res: any, next: any, id: any, name: any): any;
4
+ export function ensureInteger(req: any, res: any, next: any, id: any, name: any): any;
@@ -0,0 +1,22 @@
1
+ const T = require('../types/types');
2
+ const ensureType = (type) => (req, res, next, id, name) => {
3
+ let error;
4
+ try {
5
+ T.ensureType(type, id, `Invalid ${name}`);
6
+ return next();
7
+ }
8
+ catch (error1) {
9
+ error = error1;
10
+ return next(error);
11
+ }
12
+ };
13
+ const ensureUUID = ensureType('UUID');
14
+ const ensureHex32 = ensureType('Hex32');
15
+ const ensureInteger = ensureType('IntString');
16
+ module.exports = {
17
+ ensureType,
18
+ ensureUUID,
19
+ ensureHex32,
20
+ ensureInteger,
21
+ };
22
+ //# sourceMappingURL=middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/middleware/middleware.js"],"names":[],"mappings":"AAAA,MAAM,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAEpC,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE;IACxD,IAAI,KAAK,CAAC;IACV,IAAI;QACF,CAAC,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,EAAE,WAAW,IAAI,EAAE,CAAC,CAAC;QAC1C,OAAO,IAAI,EAAE,CAAC;KACf;IAAC,OAAO,MAAM,EAAE;QACf,KAAK,GAAG,MAAM,CAAC;QACf,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC;KACpB;AACH,CAAC,CAAC;AAEF,MAAM,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;AAEtC,MAAM,WAAW,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;AAExC,MAAM,aAAa,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;AAE9C,MAAM,CAAC,OAAO,GAAG;IACf,UAAU;IACV,UAAU;IACV,WAAW;IACX,aAAa;CACd,CAAC","sourcesContent":["const T = require('../types/types');\n\nconst ensureType = (type) => (req, res, next, id, name) => {\n let error;\n try {\n T.ensureType(type, id, `Invalid ${name}`);\n return next();\n } catch (error1) {\n error = error1;\n return next(error);\n }\n};\n\nconst ensureUUID = ensureType('UUID');\n\nconst ensureHex32 = ensureType('Hex32');\n\nconst ensureInteger = ensureType('IntString');\n\nmodule.exports = {\n ensureType,\n ensureUUID,\n ensureHex32,\n ensureInteger,\n};\n"]}
@@ -0,0 +1 @@
1
+ export function includeNested(orm: any, models: any): any;
@@ -0,0 +1,17 @@
1
+ const _ = require('lodash');
2
+ const includeNested = (orm, models) => _.map(_.toPairs(models), (arg) => {
3
+ const [modelName, inclusion] = arg;
4
+ const include = includeNested(orm, inclusion);
5
+ const model = orm[modelName];
6
+ if (_.isEmpty(include)) {
7
+ return model;
8
+ }
9
+ return {
10
+ model,
11
+ include,
12
+ };
13
+ });
14
+ module.exports = {
15
+ includeNested,
16
+ };
17
+ //# sourceMappingURL=ormHelpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ormHelpers.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/orm-helpers/ormHelpers.js"],"names":[],"mappings":"AAAA,MAAM,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAE5B,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,CACpC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE;IAC/B,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,GAAG,GAAG,CAAC;IACnC,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IAC9C,MAAM,KAAK,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC;IAC7B,IAAI,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QACtB,OAAO,KAAK,CAAC;KACd;IACD,OAAO;QACL,KAAK;QACL,OAAO;KACR,CAAC;AACJ,CAAC,CAAC,CAAC;AAEL,MAAM,CAAC,OAAO,GAAG;IACf,aAAa;CACd,CAAC","sourcesContent":["const _ = require('lodash');\n\nconst includeNested = (orm, models) =>\n _.map(_.toPairs(models), (arg) => {\n const [modelName, inclusion] = arg;\n const include = includeNested(orm, inclusion);\n const model = orm[modelName];\n if (_.isEmpty(include)) {\n return model;\n }\n return {\n model,\n include,\n };\n });\n\nmodule.exports = {\n includeNested,\n};\n"]}
@@ -0,0 +1,12 @@
1
+ export declare class InvalidFileError extends Error {
2
+ constructor();
3
+ }
4
+ export declare class TooManyRowsError extends Error {
5
+ constructor();
6
+ }
7
+ export declare class NoRowsError extends Error {
8
+ constructor();
9
+ }
10
+ export declare class InvalidHeadersError extends Error {
11
+ constructor();
12
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidHeadersError = exports.NoRowsError = exports.TooManyRowsError = exports.InvalidFileError = void 0;
4
+ /* eslint-disable max-classes-per-file */
5
+ class InvalidFileError extends Error {
6
+ constructor() {
7
+ super('Invalid file type');
8
+ this.name = 'InvalidFileError';
9
+ }
10
+ }
11
+ exports.InvalidFileError = InvalidFileError;
12
+ class TooManyRowsError extends Error {
13
+ constructor() {
14
+ super('More than configured number of rows imported');
15
+ this.name = 'TooManyRowsError';
16
+ }
17
+ }
18
+ exports.TooManyRowsError = TooManyRowsError;
19
+ class NoRowsError extends Error {
20
+ constructor() {
21
+ super('No rows');
22
+ this.name = 'NoRowsError';
23
+ }
24
+ }
25
+ exports.NoRowsError = NoRowsError;
26
+ class InvalidHeadersError extends Error {
27
+ constructor() {
28
+ super('Invalid or incomplete headers list');
29
+ this.name = 'InvalidHeadersError';
30
+ }
31
+ }
32
+ exports.InvalidHeadersError = InvalidHeadersError;
33
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/structured-file-parser/errors.ts"],"names":[],"mappings":";;;AAAA,yCAAyC;AACzC,MAAa,gBAAiB,SAAQ,KAAK;IACzC;QACE,KAAK,CAAC,mBAAmB,CAAC,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AALD,4CAKC;AACD,MAAa,gBAAiB,SAAQ,KAAK;IACzC;QACE,KAAK,CAAC,8CAA8C,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACjC,CAAC;CACF;AALD,4CAKC;AACD,MAAa,WAAY,SAAQ,KAAK;IACpC;QACE,KAAK,CAAC,SAAS,CAAC,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AALD,kCAKC;AACD,MAAa,mBAAoB,SAAQ,KAAK;IAC5C;QACE,KAAK,CAAC,oCAAoC,CAAC,CAAC;QAC5C,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AALD,kDAKC","sourcesContent":["/* eslint-disable max-classes-per-file */\nexport class InvalidFileError extends Error {\n constructor() {\n super('Invalid file type');\n this.name = 'InvalidFileError';\n }\n}\nexport class TooManyRowsError extends Error {\n constructor() {\n super('More than configured number of rows imported');\n this.name = 'TooManyRowsError';\n }\n}\nexport class NoRowsError extends Error {\n constructor() {\n super('No rows');\n this.name = 'NoRowsError';\n }\n}\nexport class InvalidHeadersError extends Error {\n constructor() {\n super('Invalid or incomplete headers list');\n this.name = 'InvalidHeadersError';\n }\n}\n"]}
@@ -0,0 +1,2 @@
1
+ declare const parseRow: (row: string) => string[];
2
+ export default parseRow;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const fp_1 = require("lodash/fp");
4
+ const parseRow = (row) => {
5
+ const trySplitByTab = (line) => {
6
+ if (typeof line === 'string' && line.match('\t')) {
7
+ return line.split('\t');
8
+ }
9
+ return line;
10
+ };
11
+ const trySplitByComa = (line) => {
12
+ if (typeof line === 'string' && line.match(',')) {
13
+ return line.split(',');
14
+ }
15
+ return line;
16
+ };
17
+ const trySplitByMultipleSpaces = (line) => {
18
+ if (typeof line === 'string' && line.match(/ {3,}/)) {
19
+ return line.split(/ {3,}/);
20
+ }
21
+ return line;
22
+ };
23
+ const trySplitBySpace = (line) => {
24
+ if (typeof line === 'string' && line.match(' ')) {
25
+ return line.split(' ');
26
+ }
27
+ return line;
28
+ };
29
+ const trySplitByNothing = (line) => {
30
+ if (typeof line === 'string') {
31
+ return [line];
32
+ }
33
+ return line;
34
+ };
35
+ const splitRow = (0, fp_1.pipe)(trySplitByTab, trySplitByComa, trySplitByMultipleSpaces, trySplitBySpace, trySplitByNothing);
36
+ const cells = splitRow(row);
37
+ return cells.map((c) => c.trim());
38
+ };
39
+ exports.default = parseRow;
40
+ //# sourceMappingURL=parse-row.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-row.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/structured-file-parser/parse-row.ts"],"names":[],"mappings":";;AAAA,kCAAiC;AAEjC,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAY,EAAE;IACzC,MAAM,aAAa,GAAG,CAAC,IAAuB,EAAqB,EAAE;QACnE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;YAChD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;SACzB;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,cAAc,GAAG,CAAC,IAAuB,EAAqB,EAAE;QACpE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;YAC/C,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;SACxB;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,wBAAwB,GAAG,CAAC,IAAuB,EAAqB,EAAE;QAC9E,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE;YACnD,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;SAC5B;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,eAAe,GAAG,CAAC,IAAuB,EAAqB,EAAE;QACrE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;YAC/C,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;SACxB;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,iBAAiB,GAAG,CAAC,IAAuB,EAAY,EAAE;QAC9D,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;YAC5B,OAAO,CAAC,IAAI,CAAC,CAAC;SACf;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,QAAQ,GAAG,IAAA,SAAI,EAAC,aAAa,EAAE,cAAc,EAAE,wBAAwB,EAAE,eAAe,EAAE,iBAAiB,CAAC,CAAC;IAEnH,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;IAE5B,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,kBAAe,QAAQ,CAAC","sourcesContent":["import { pipe } from 'lodash/fp';\n\nconst parseRow = (row: string): string[] => {\n const trySplitByTab = (line: string | string[]): string | string[] => {\n if (typeof line === 'string' && line.match('\\t')) {\n return line.split('\\t');\n }\n return line;\n };\n\n const trySplitByComa = (line: string | string[]): string | string[] => {\n if (typeof line === 'string' && line.match(',')) {\n return line.split(',');\n }\n return line;\n };\n\n const trySplitByMultipleSpaces = (line: string | string[]): string | string[] => {\n if (typeof line === 'string' && line.match(/ {3,}/)) {\n return line.split(/ {3,}/);\n }\n return line;\n };\n\n const trySplitBySpace = (line: string | string[]): string | string[] => {\n if (typeof line === 'string' && line.match(' ')) {\n return line.split(' ');\n }\n return line;\n };\n\n const trySplitByNothing = (line: string | string[]): string[] => {\n if (typeof line === 'string') {\n return [line];\n }\n return line;\n };\n\n const splitRow = pipe(trySplitByTab, trySplitByComa, trySplitByMultipleSpaces, trySplitBySpace, trySplitByNothing);\n\n const cells = splitRow(row);\n\n return cells.map((c: string) => c.trim());\n};\n\nexport default parseRow;\n"]}
@@ -0,0 +1,2 @@
1
+ declare function sanitizeRows(parsedRows: string[][]): string[][];
2
+ export default sanitizeRows;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const sensicalRowCheck = (cells) => cells.some((cell) => !!cell);
4
+ function sanitizeRows(parsedRows) {
5
+ const firstSensicalRowIndex = parsedRows.findIndex(sensicalRowCheck);
6
+ const lastSensicalRowIndex = [...parsedRows].reverse().findIndex(sensicalRowCheck);
7
+ if (firstSensicalRowIndex < 0) {
8
+ return [];
9
+ }
10
+ const sanitizedRows = [...parsedRows].splice(firstSensicalRowIndex, parsedRows.length - firstSensicalRowIndex - lastSensicalRowIndex);
11
+ return sanitizedRows;
12
+ }
13
+ exports.default = sanitizeRows;
14
+ //# sourceMappingURL=sanitize-rows.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitize-rows.js","sourceRoot":"","sources":["../../../../../libs/aml-utils/lib/structured-file-parser/sanitize-rows.ts"],"names":[],"mappings":";;AAAA,MAAM,gBAAgB,GAAG,CAAC,KAAe,EAAW,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAY,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAE5F,SAAS,YAAY,CAAC,UAAsB;IAC1C,MAAM,qBAAqB,GAAG,UAAU,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC;IAErE,MAAM,oBAAoB,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC;IAEnF,IAAI,qBAAqB,GAAG,CAAC,EAAE;QAC7B,OAAO,EAAE,CAAC;KACX;IAED,MAAM,aAAa,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,MAAM,CAAC,qBAAqB,EAAE,UAAU,CAAC,MAAM,GAAG,qBAAqB,GAAG,oBAAoB,CAAC,CAAC;IAEtI,OAAO,aAAa,CAAC;AACvB,CAAC;AAED,kBAAe,YAAY,CAAC","sourcesContent":["const sensicalRowCheck = (cells: string[]): boolean => cells.some((cell: string) => !!cell);\n\nfunction sanitizeRows(parsedRows: string[][]): string[][] {\n const firstSensicalRowIndex = parsedRows.findIndex(sensicalRowCheck);\n\n const lastSensicalRowIndex = [...parsedRows].reverse().findIndex(sensicalRowCheck);\n\n if (firstSensicalRowIndex < 0) {\n return [];\n }\n\n const sanitizedRows = [...parsedRows].splice(firstSensicalRowIndex, parsedRows.length - firstSensicalRowIndex - lastSensicalRowIndex);\n\n return sanitizedRows;\n}\n\nexport default sanitizeRows;\n"]}
@@ -0,0 +1,10 @@
1
+ export type ProcessingEntry = {
2
+ isValid: boolean;
3
+ };
4
+ type Dependencies<T extends ProcessingEntry> = {
5
+ processRow: (cells: string[], headers: string[]) => T;
6
+ };
7
+ export declare const hasHeaderRow: (rows: string[][], requiredHeaderNames: string[]) => boolean;
8
+ export declare const isHeaderRowValid: (rows: string[][], requiredHeaderNames: string[]) => boolean;
9
+ declare function parseFile<T extends ProcessingEntry>(fileContent: string, maxRows: number, requiredHeaderNames: string[], dependencies: Dependencies<T>): Promise<T[]>;
10
+ export default parseFile;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isHeaderRowValid = exports.hasHeaderRow = void 0;
4
+ const tslib_1 = require("tslib");
5
+ /* eslint-disable no-restricted-syntax */
6
+ const parse_row_1 = tslib_1.__importDefault(require("./parse-row"));
7
+ const errors_1 = require("./errors");
8
+ const sanitize_rows_1 = tslib_1.__importDefault(require("./sanitize-rows"));
9
+ function chunk(array, size) {
10
+ if (!array.length) {
11
+ return [];
12
+ }
13
+ const head = array.slice(0, size);
14
+ const tail = array.slice(size);
15
+ return [head, ...chunk(tail, size)];
16
+ }
17
+ function processChunk(parsedRows, dependencies) {
18
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
19
+ const { processRow, headers } = dependencies;
20
+ return new Promise((resolve, reject) => {
21
+ setTimeout(() => {
22
+ try {
23
+ const processedRows = parsedRows.map((row) => processRow(row, headers));
24
+ resolve(processedRows);
25
+ }
26
+ catch (ex) {
27
+ reject(ex);
28
+ }
29
+ }, 0);
30
+ });
31
+ });
32
+ }
33
+ // a row is said to contain headers if two valid header names are present
34
+ const hasHeaderRow = (rows, requiredHeaderNames) => {
35
+ const firstRow = rows[0];
36
+ if (!firstRow) {
37
+ return false;
38
+ }
39
+ const foundIndex = firstRow.findIndex((value) => requiredHeaderNames.includes(value.toLowerCase()));
40
+ if (foundIndex < 0) {
41
+ return false;
42
+ }
43
+ const containsSecondHeader = firstRow
44
+ .filter((_val, i) => i !== foundIndex)
45
+ .some((value) => requiredHeaderNames.includes(value.toLowerCase()));
46
+ return containsSecondHeader;
47
+ };
48
+ exports.hasHeaderRow = hasHeaderRow;
49
+ const isHeaderRowValid = (rows, requiredHeaderNames) => {
50
+ const firstRow = rows[0];
51
+ if (!firstRow) {
52
+ return false;
53
+ }
54
+ return (requiredHeaderNames.every((header) => firstRow.map((value) => value.toLowerCase()).includes(header)) &&
55
+ requiredHeaderNames.length === firstRow.length);
56
+ };
57
+ exports.isHeaderRowValid = isHeaderRowValid;
58
+ function parseFile(fileContent, maxRows, requiredHeaderNames, dependencies) {
59
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
60
+ if (fileContent === 'invalidFileType') {
61
+ throw new errors_1.InvalidFileError();
62
+ }
63
+ const rows = fileContent.split('\n');
64
+ const parsedRows = rows.map(parse_row_1.default);
65
+ const sanitizedRows = (0, sanitize_rows_1.default)(parsedRows);
66
+ if (sanitizedRows.length > maxRows) {
67
+ throw new errors_1.TooManyRowsError();
68
+ }
69
+ if (sanitizedRows.length === 0) {
70
+ throw new errors_1.NoRowsError();
71
+ }
72
+ const containsHeaders = (0, exports.hasHeaderRow)(sanitizedRows, requiredHeaderNames);
73
+ if (containsHeaders && !(0, exports.isHeaderRowValid)(sanitizedRows, requiredHeaderNames)) {
74
+ throw new errors_1.InvalidHeadersError();
75
+ }
76
+ const firstSanitizedRow = sanitizedRows[0];
77
+ if (!firstSanitizedRow) {
78
+ throw new errors_1.NoRowsError();
79
+ }
80
+ const headers = containsHeaders ? firstSanitizedRow.map((value) => value.toLowerCase()) : requiredHeaderNames;
81
+ const dataRows = containsHeaders ? sanitizedRows.splice(1) : sanitizedRows;
82
+ if (dataRows.length === 0) {
83
+ throw new errors_1.NoRowsError();
84
+ }
85
+ const parsedRowChunks = chunk(dataRows, 30);
86
+ let results = [];
87
+ for (const currentRowChunk of parsedRowChunks) {
88
+ const chunkResult = yield processChunk(currentRowChunk, Object.assign(Object.assign({}, dependencies), { headers }));
89
+ results = results.concat(chunkResult);
90
+ }
91
+ return results;
92
+ });
93
+ }
94
+ exports.default = parseFile;
95
+ //# sourceMappingURL=structured-file-parser.js.map