@contrail/transform-data 1.0.4 → 1.0.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.
package/lib/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
1
  export * from './conditional';
2
2
  export * from './map-file';
3
+ export * from './morph';
4
+ export * from './processor';
3
5
  export * from './rekey';
6
+ export * from './remove';
7
+ export * from './value-function';
package/lib/index.js CHANGED
@@ -16,4 +16,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./conditional"), exports);
18
18
  __exportStar(require("./map-file"), exports);
19
+ __exportStar(require("./morph"), exports);
20
+ __exportStar(require("./processor"), exports);
19
21
  __exportStar(require("./rekey"), exports);
22
+ __exportStar(require("./remove"), exports);
23
+ __exportStar(require("./value-function"), exports);
@@ -1,8 +1,10 @@
1
1
  export declare class MapFileUtil {
2
2
  private entities;
3
3
  private cache;
4
+ static FILE_NOT_FOUND: string;
5
+ static ERROR_RETRIEVING: string;
4
6
  constructor(entities: any);
5
7
  getMapFile(fileId: string): Promise<any>;
6
- getMappingSectionFromMap(mapFile: any, objectClass: string, direction: string): any;
7
- getMappingSection(fileId: string, objectClass: string, direction: string): Promise<any>;
8
+ getMappingSectionFromMap(mapFile: any, objectClass: string, direction: string): {};
9
+ getMappingSection(fileId: string, objectClass: string, direction: string): Promise<{}>;
8
10
  }
@@ -7,6 +7,9 @@ class MapFileUtil {
7
7
  this.cache = {};
8
8
  }
9
9
  async getMapFile(fileId) {
10
+ if (!fileId) {
11
+ return {};
12
+ }
10
13
  if (this.cache[fileId]) {
11
14
  return this.cache[fileId];
12
15
  }
@@ -15,14 +18,35 @@ class MapFileUtil {
15
18
  id: fileId,
16
19
  };
17
20
  const file = await this.entities.get(options);
18
- const downloadUrl = file['downloadUrl'];
19
- const response = await fetch(downloadUrl);
20
- const mappingFile = await response.json();
21
+ if (!file) {
22
+ throw new Error(MapFileUtil.FILE_NOT_FOUND + fileId);
23
+ }
24
+ let mappingFile = {};
25
+ try {
26
+ const downloadUrl = file['downloadUrl'];
27
+ const contentType = '' + file['contentType'];
28
+ if (contentType && contentType.toLowerCase().indexOf('json') > -1) {
29
+ console.log('json file');
30
+ const response = await fetch(downloadUrl);
31
+ mappingFile = await response.json();
32
+ console.log(JSON.stringify(mappingFile));
33
+ }
34
+ else {
35
+ console.log('javascript file');
36
+ const requireFromUrl = require('require-from-url/sync');
37
+ const maps = requireFromUrl(downloadUrl);
38
+ mappingFile = maps['mapping'];
39
+ console.log(JSON.stringify(mappingFile));
40
+ }
41
+ }
42
+ catch (e) {
43
+ throw new Error(MapFileUtil.ERROR_RETRIEVING + e);
44
+ }
21
45
  this.cache[fileId] = mappingFile;
22
46
  return mappingFile;
23
47
  }
24
48
  getMappingSectionFromMap(mapFile, objectClass, direction) {
25
- let mapping = undefined;
49
+ let mapping = {};
26
50
  if (mapFile[objectClass]) {
27
51
  const classMapping = mapFile[objectClass];
28
52
  if (classMapping[direction]) {
@@ -38,3 +62,5 @@ class MapFileUtil {
38
62
  }
39
63
  }
40
64
  exports.MapFileUtil = MapFileUtil;
65
+ MapFileUtil.FILE_NOT_FOUND = 'Mapping File not found for id: ';
66
+ MapFileUtil.ERROR_RETRIEVING = 'Error getting mapping file: ';
@@ -0,0 +1 @@
1
+ export * from './morph-transformer';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./morph-transformer"), exports);
@@ -0,0 +1,4 @@
1
+ export declare class MorphTransformer {
2
+ static transformData(rows: any[], transformFunctions: Record<string, Function>, dependencies: Record<string, any>): void;
3
+ static transformObject(row: any, transformFunctions: Record<string, Function>, dependencies: Record<string, any>): void;
4
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MorphTransformer = void 0;
4
+ class MorphTransformer {
5
+ static transformData(rows, transformFunctions, dependencies) {
6
+ for (const row of rows) {
7
+ MorphTransformer.transformObject(row, transformFunctions, dependencies);
8
+ }
9
+ }
10
+ static transformObject(row, transformFunctions, dependencies) {
11
+ for (const [key, fun] of Object.entries(transformFunctions)) {
12
+ fun(row, dependencies);
13
+ }
14
+ }
15
+ }
16
+ exports.MorphTransformer = MorphTransformer;
@@ -0,0 +1,2 @@
1
+ export * from './process-interfaces';
2
+ export * from './transform-processor';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./process-interfaces"), exports);
18
+ __exportStar(require("./transform-processor"), exports);
@@ -0,0 +1,10 @@
1
+ import { TransformDefinition } from "../conditional";
2
+ export interface TransformTask {
3
+ processor: 'CONDITIONAL' | 'MORPH' | 'REKEY' | 'REMOVE' | 'VALUE_TRANSFORM';
4
+ conditionalTransformDefinitions?: TransformDefinition[];
5
+ functionTransformers?: Record<string, Function>;
6
+ rekeyTransformers?: Record<string, string>;
7
+ rekeyDelete?: boolean;
8
+ removeKeys?: string[];
9
+ dependencies?: object;
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import { TransformTask } from "./process-interfaces";
2
+ export declare class TransformProcessor {
3
+ static transformData(rows: any[], transformTasks: TransformTask[]): any[];
4
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransformProcessor = void 0;
4
+ const conditional_1 = require("../conditional");
5
+ const morph_transformer_1 = require("../morph/morph-transformer");
6
+ const rekey_1 = require("../rekey");
7
+ const remove_1 = require("../remove");
8
+ const value_function_1 = require("../value-function");
9
+ class TransformProcessor {
10
+ static transformData(rows, transformTasks) {
11
+ for (const transformTask of transformTasks) {
12
+ switch (transformTask.processor) {
13
+ case 'CONDITIONAL':
14
+ conditional_1.ConditionalTransformer.evaluateTransformDefinitionsOnBatch(transformTask.conditionalTransformDefinitions, rows);
15
+ break;
16
+ case 'MORPH':
17
+ morph_transformer_1.MorphTransformer.transformData(rows, transformTask.functionTransformers, transformTask.dependencies);
18
+ break;
19
+ case 'REKEY':
20
+ rekey_1.RekeyTransformer.transformData(rows, transformTask.rekeyTransformers, transformTask.rekeyDelete);
21
+ break;
22
+ case 'REMOVE':
23
+ remove_1.RemoveTransformer.transformData(rows, transformTask.removeKeys);
24
+ break;
25
+ case 'VALUE_TRANSFORM':
26
+ value_function_1.ValueFunctionTransformer.transformData(rows, transformTask.functionTransformers, transformTask.dependencies);
27
+ break;
28
+ }
29
+ }
30
+ return rows;
31
+ }
32
+ }
33
+ exports.TransformProcessor = TransformProcessor;
@@ -1,4 +1,4 @@
1
1
  export declare class RekeyTransformer {
2
- static transformData(csvJSON: any[], federatedMappings: Record<string, string>, deleteOldKey?: boolean): any[];
3
- static transformObject(row: any, federatedMappings: Record<string, string>, deleteOldKey?: boolean): any;
2
+ static transformData(csvJSON: any[], federatedMappings: Record<string, string>, deleteOldKey?: boolean): void;
3
+ static transformObject(row: any, federatedMappings: Record<string, string>, deleteOldKey?: boolean): void;
4
4
  }
@@ -6,11 +6,10 @@ class RekeyTransformer {
6
6
  for (const row of csvJSON) {
7
7
  RekeyTransformer.transformObject(row, federatedMappings, deleteOldKey);
8
8
  }
9
- return csvJSON;
10
9
  }
11
10
  static transformObject(row, federatedMappings, deleteOldKey = false) {
12
11
  for (const [key, value] of Object.entries(federatedMappings)) {
13
- if (!row[key] && row[value]) {
12
+ if (!row[key] && value) {
14
13
  row[key] = row[value];
15
14
  }
16
15
  }
@@ -19,7 +18,6 @@ class RekeyTransformer {
19
18
  delete row[value];
20
19
  }
21
20
  }
22
- return row;
23
21
  }
24
22
  }
25
23
  exports.RekeyTransformer = RekeyTransformer;
@@ -0,0 +1 @@
1
+ export * from './remove-transformer';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./remove-transformer"), exports);
@@ -0,0 +1,4 @@
1
+ export declare class RemoveTransformer {
2
+ static transformData(rows: any[], keys: string[]): void;
3
+ static transformObject(row: any, keys: string[]): void;
4
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RemoveTransformer = void 0;
4
+ class RemoveTransformer {
5
+ static transformData(rows, keys) {
6
+ for (const row of rows) {
7
+ RemoveTransformer.transformObject(row, keys);
8
+ }
9
+ }
10
+ static transformObject(row, keys) {
11
+ for (const key of keys) {
12
+ delete row[key];
13
+ }
14
+ }
15
+ }
16
+ exports.RemoveTransformer = RemoveTransformer;
@@ -0,0 +1 @@
1
+ export * from './value-function-transformer';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./value-function-transformer"), exports);
@@ -0,0 +1,4 @@
1
+ export declare class ValueFunctionTransformer {
2
+ static transformData(rows: any[], transformFunctions: Record<string, Function>, dependencies: Record<string, any>): void;
3
+ static transformObject(row: any, transformFunctions: Record<string, Function>, dependencies: Record<string, any>): void;
4
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValueFunctionTransformer = void 0;
4
+ class ValueFunctionTransformer {
5
+ static transformData(rows, transformFunctions, dependencies) {
6
+ for (const row of rows) {
7
+ ValueFunctionTransformer.transformObject(row, transformFunctions, dependencies);
8
+ }
9
+ }
10
+ static transformObject(row, transformFunctions, dependencies) {
11
+ for (const [key, fun] of Object.entries(transformFunctions)) {
12
+ row[key] = fun(row, dependencies);
13
+ }
14
+ }
15
+ }
16
+ exports.ValueFunctionTransformer = ValueFunctionTransformer;
package/package.json CHANGED
@@ -1,27 +1,34 @@
1
1
  {
2
2
  "name": "@contrail/transform-data",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Libraries for transforming data",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
- "lint": "tslint -p tsconfig.json",
11
- "test": "jest"
10
+ "lint": "tslint --fix -p tsconfig.json",
11
+ "test": "jest",
12
+ "test-watch": "jest --watch"
12
13
  },
13
14
  "keywords": [],
14
15
  "author": "",
15
16
  "license": "ISC",
16
17
  "devDependencies": {
17
- "@types/jest": "^23.3.14",
18
- "@types/node": "^14.14.6",
19
- "jest": "^23.6.0",
18
+ "@types/jest": "^29.4.0",
19
+ "@types/node": "^18.14.6",
20
+ "@typescript-eslint/eslint-plugin": "^5.48.0",
21
+ "@typescript-eslint/parser": "^5.48.0",
22
+ "body-parser": "^1.20.2",
23
+ "eslint": "^8.31.0",
24
+ "eslint-plugin-unused-imports": "^2.0.0",
25
+ "jest": "^29.2.2",
20
26
  "prettier": "^1.19.1",
21
- "ts-jest": "^23.10.5",
22
27
  "tslint": "^5.11.0",
23
28
  "tslint-config-prettier": "^1.18.0",
24
- "typescript": "^4.0.0"
29
+ "ts-jest": "^29.0.3",
30
+ "ts-node": "^7.0.1",
31
+ "typescript": "^4.7.4"
25
32
  },
26
33
  "jest": {
27
34
  "moduleFileExtensions": [
@@ -38,9 +45,9 @@
38
45
  "testEnvironment": "node"
39
46
  },
40
47
  "dependencies": {
41
- "@contrail/core": "1.5.56",
42
48
  "@contrail/sdk": "^1.2.1",
43
49
  "@contrail/util": "^1.0.26",
44
- "node-fetch": "^2.6.0"
50
+ "node-fetch": "^3.2.10",
51
+ "require-from-url": "^3.1.3"
45
52
  }
46
53
  }