@friggframework/core 0.1.2 → 0.2.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.
@@ -1,118 +0,0 @@
1
- const md5 = require('md5');
2
- const ModuleManager = require('../../managers/ModuleManager');
3
- const { debug } = require('@friggframework/logs');
4
- const { get } = require('@friggframework/assertions');
5
-
6
- class Sync {
7
- static Config = {
8
- name: 'Sync',
9
-
10
- // an array of keys we will use to form an object and then hash it. Order matters here
11
- // because it will effect how the hash results
12
- keys: [],
13
-
14
- // matchOn is an array of keys that make the variable unique when combined together
15
- // and is used to sync with the other objects
16
- // matchOn keys _have_ to have a value, otherwise the object is not considered a match
17
- matchOn: [],
18
-
19
- // a key value mapping of module to then a list of keys that will map to
20
- // an a function that takes in the module object and return the value from it
21
- // format as follows:
22
- // {
23
- // ModuleName:{
24
- // firstName:(moduleObject)=>{moduleObject['name'][0]},
25
- // lastName:(moduleObject)=>{moduleObject['name'][1]},
26
- // },
27
- // ....
28
- // }
29
- moduleMap: {},
30
- reverseModuleMap: {},
31
- };
32
- constructor(params) {
33
- this.data = {};
34
-
35
- let data = get(params, 'data');
36
- this.moduleName = get(params, 'moduleName');
37
- this.dataIdentifier = get(params, 'dataIdentifier');
38
- this.useMapping = get(params, 'useMapping', true); // Use with caution...
39
-
40
- this.dataIdentifierHash = this.constructor.hashJSON(
41
- this.dataIdentifier
42
- );
43
-
44
- if (this.useMapping) {
45
- for (let key of this.constructor.Config.keys) {
46
- this.data[key] =
47
- this.constructor.Config.moduleMap[this.moduleName][key](
48
- data
49
- );
50
- }
51
- } else {
52
- this.data = data;
53
- }
54
-
55
- // matchHash is used to find matches between two sync objects
56
- // Match data _has_ to have a value
57
- const matchHashData = [];
58
- this.missingMatchData = false;
59
- for (const key of this.constructor.Config.matchOn) {
60
- if (!this.data[key]) {
61
- this.missingMatchData = true;
62
- debug(`Data key of ${key} was missing from MatchOn`);
63
- }
64
-
65
- matchHashData.push(this.data[key]);
66
- }
67
- this.matchHash = this.constructor.hashJSON(matchHashData);
68
-
69
- this.syncId = null;
70
- }
71
-
72
- equals(syncObj) {
73
- return this.matchHash === syncObj.matchHash;
74
- }
75
- dataKeyIsReplaceable(key) {
76
- return this.data[key] === null || this.data[key] === '';
77
- }
78
-
79
- isModuleInMap(moduleName) {
80
- return this.constructor.Config.moduleMap[name];
81
- }
82
-
83
- getName() {
84
- return this.constructor.Config.name;
85
- }
86
-
87
- getHashData(params) {
88
- let omitEmptyStringsFromData = get(
89
- params,
90
- 'omitEmptyStringsFromData',
91
- false
92
- );
93
- let orderedData = [];
94
- for (let key of this.constructor.Config.keys) {
95
- if (omitEmptyStringsFromData && this.data[key] === '') {
96
- this.data[key] = undefined;
97
- }
98
- orderedData.push(this.data[key]);
99
- }
100
-
101
- return this.constructor.hashJSON(orderedData);
102
- }
103
-
104
- setSyncId(syncId) {
105
- this.syncId = syncId;
106
- }
107
-
108
- reverseModuleMap(moduleName) {
109
- return this.constructor.Config.reverseModuleMap[moduleName](this.data);
110
- }
111
-
112
- static hashJSON(data) {
113
- let dataString = JSON.stringify(data, null, 2);
114
- return md5(dataString);
115
- }
116
- }
117
-
118
- module.exports = Sync;