-
Notifications
You must be signed in to change notification settings - Fork 32
/
generate_checksum.js
270 lines (236 loc) · 8.5 KB
/
generate_checksum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const crypto = require('crypto');
const { program } = require('commander');
function genericSort(list) {
function compare(item) {
if (typeof item === 'number' || typeof item === 'string' || typeof item === 'boolean') {
return item;
}
return JSON.stringify(item); // Convert objects to their string representation
}
return list.sort((a, b) => {
const aValue = compare(a);
const bValue = compare(b);
if (aValue < bValue) return -1;
if (aValue > bValue) return 1;
return 0;
});
}
function jsonStringifyWithSpaces(obj) {
const jsonString = JSON.stringify(obj, null, 2);
return jsonString.replace(/,\n\s+/g, ', ');
}
function customStringify(obj) {
return JSON.stringify(obj, (key, value) => {
if (Array.isArray(value)) {
return value.join(', ');
}
return value;
}, 2).replace(/", /g, '", ');
}
function formatArrayWithSpaces(array) {
return `[${array.map(item => `"${item}"`).join(', ')}]`;
}
function unicodeEscape(str) {
return str.replace(/[^\0-~]/g, ch => {
return '\\u' + ('000' + ch.charCodeAt().toString(16)).slice(-4);
});
}
function serialize(obj) {
if (Array.isArray(obj) && obj.length === 1) {
obj = obj[0];
}
if (Array.isArray(obj)) {
return `[${genericSort(obj).map(serialize).join(',')}]`;
}
if (typeof obj === 'object' && obj !== null) {
const keys = genericSort(Object.keys(obj));
let acc = `{${formatArrayWithSpaces(keys)}`;
keys.forEach(key => {
acc += `${serialize(obj[key])},`;
});
return `${acc}}`;
}
if (typeof obj === 'string' && obj.match(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/)) {
return JSON.stringify(obj, (key, value) => typeof value === 'string' ? unicodeEscape(value) : value).replace(/\\\\u/g, '\\u');
}
return JSON.stringify(obj, (key, value) => typeof value === 'string' ? unicodeEscape(value) : value).replace(/\\\\u/g, '\\u');
}
function cleanup(fields) {
let result = fields;
if (typeof fields === 'object' && fields !== null) {
result = {};
for (const key in fields) {
let value = fields[key];
if (value === null) continue;
if (
[
'retired', 'parent_concept_urls', 'child_concept_urls', 'descriptions', 'extras',
'names', 'locale_preferred', 'name_type', 'description_type'
].includes(key) && (!value || (Array.isArray(value) && value.length === 0))
)
continue;
if(['names', 'descriptions'].includes(key)) {
for (const val in value)
value = value.map(cleanup);
}
if (key === 'is_active' && value) continue;
if(typeof(value) === 'number' && parseInt(value) === parseFloat(value))
value = parseInt(value);
if (key === 'extras') {
if (typeof value === 'object' && Object.keys(value).some(k => k.startsWith('__'))) {
const valueCopied = { ...value };
for (const extraKey in value) {
if (extraKey.startsWith('__')) {
delete valueCopied[extraKey];
}
}
value = valueCopied;
}
}
result[key] = value;
}
}
return result;
}
function toHexString(byteArray) {
return Array.from(byteArray, byte => {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
}
function localesForChecksums(data, relation, fields, predicateFunc) {
const locales = data[relation] || [];
return locales.map(locale => {
const result = {};
fields.forEach(field => {
result[field] = locale[field];
});
return predicateFunc(locale) ? result : null;
}).filter(locale => locale !== null);
}
function generate(obj, hashAlgorithm = 'md5') {
let serializedObj = serialize(obj);
serializedObj = new TextEncoder().encode(serializedObj);
console.log()
console.log("After Serialization:")
console.log(new TextDecoder().decode(serializedObj))
const hash = crypto.createHash(hashAlgorithm);
hash.update(serializedObj);
return hash.digest('hex');
}
function isFullySpecifiedType(type) {
if (!type) return false;
if (type === 'FULLY_SPECIFIED' || type === 'Fully Specified') return true;
type = type.replace(/\s|[-_]/g, '').toLowerCase();
return type === 'fullyspecified';
}
function convertEmptyObjectToNull(obj) {
if (obj && Object.keys(obj).length === 0 && obj.constructor === Object) {
return null;
}
return obj;
}
function getConceptFields(data, checksumType) {
const nameFields = ['locale', 'locale_preferred', 'name', 'name_type', 'external_id'];
const descriptionFields = ['locale', 'locale_preferred', 'description', 'description_type', 'external_id'];
if (checksumType === 'standard') {
return {
concept_class: data.concept_class || null,
datatype: data.datatype || null,
retired: data.retired || false,
external_id: data.external_id || null,
extras: convertEmptyObjectToNull(data.extras || null),
names: localesForChecksums(data, 'names', nameFields, () => true),
descriptions: localesForChecksums(data, 'descriptions', descriptionFields, () => true),
parent_concept_urls: data.parent_concept_urls || [],
child_concept_urls: data.child_concept_urls || []
};
}
return {
concept_class: data.concept_class || null,
datatype: data.datatype || null,
retired: data.retired || false,
names: localesForChecksums(data, 'names', nameFields, locale => isFullySpecifiedType(locale.name_type))
};
}
function getMappingFields(data, checksumType) {
const fields = {
map_type: data.map_type || null,
from_concept_code: data.from_concept_code || null,
to_concept_code: data.to_concept_code || null,
from_concept_name: data.from_concept_name || null,
to_concept_name: data.to_concept_name || null,
retired: data.retired || false
};
if (checksumType === 'standard') {
return {
...fields,
sort_weight: parseFloat(data.sort_weight || 0) || null,
extras: convertEmptyObjectToNull(data.extras || null),
external_id: data.external_id || null,
from_source_url: data.from_source_url || null,
from_source_version: data.from_source_version || null,
to_source_url: data.to_source_url || null,
to_source_version: data.to_source_version || null
};
}
return fields;
}
function flatten(inputList, depth = 1) {
return inputList.reduce((acc, item) => {
if (Array.isArray(item) && depth > 0) {
acc.push(...flatten(item, depth - 1));
} else {
acc.push(item);
}
return acc;
}, []);
}
function generateChecksum(resource, data, checksumType = 'standard', verbosity=0) {
if (!resource || !['concept', 'mapping'].includes(resource.toLowerCase())) {
throw new Error(`Invalid resource: ${resource}`);
}
if (!['standard', 'smart'].includes(checksumType)) {
throw new Error(`Invalid checksum type: ${checksumType}`);
}
if (resource === 'concept') {
data = flatten([data]).map(d => getConceptFields(d, checksumType));
} else if (resource === 'mapping') {
data = flatten([data]).map(d => getMappingFields(d, checksumType));
}
if(verbosity > 0) {
console.log()
console.log("Fields for Checksum:")
console.log(JSON.stringify(data, null, 2))
console.log()
console.log("After Cleanup:")
console.log(JSON.stringify(data.map(d => cleanup(d)), null, 2))
}
const checksums = Array.isArray(data)
? data.map(d => generate(cleanup(d)))
: [generate(cleanup(data))];
if (checksums.length === 1) {
return checksums[0];
}
return generate(checksums);
}
// CLI usage with commander
program
.requiredOption('-r, --resource <type>', 'The type of resource (concept, mapping)')
.requiredOption('-c, --checksum_type <type>', 'The type of checksum to generate (default: standard)', 'standard')
.requiredOption('-d, --data <json>', 'The data for which checksum needs to be generated')
.requiredOption('-v, --verbosity <int>', 'Verbosity level (default: 0)', 0)
program.parse(process.argv);
const options = program.opts();
try {
const data = JSON.parse(options.data);
const checksum = generateChecksum(options.resource, data, options.checksum_type, options.verbosity);
console.log()
console.log('\x1b[6;30;42m' + `${options.checksum_type.charAt(0).toUpperCase() + options.checksum_type.slice(1)} Checksum: ${checksum}` + '\x1b[0m');
console.log()
} catch (error) {
console.error('Error:', error.message);
}
function usage() {
console.log("Use this as:")
console.log("node generate_checksum.js -r <concept|mapping> -c <standard|smart> -d '{...json...}'")
}