-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhash.ts
More file actions
64 lines (59 loc) · 1.21 KB
/
hash.ts
File metadata and controls
64 lines (59 loc) · 1.21 KB
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
import hashObject from 'object-hash';
const PRESERVE_PROPS = [
'$ref',
'name',
'in',
'type',
'required',
'schema',
'enum',
'nullable',
'minimum',
'maximum',
'allOf',
'anyOf',
'oneOf',
'not',
'items',
'minItems',
'maxItems',
'minLength',
'maxLength',
'pattern',
'format',
'properties',
'propertyNames',
'additionalProperties',
'minProperties',
'maxProperties',
'requestBody',
'get',
'post',
'put',
'delete',
'patch',
'operationId',
'parameters',
'responses',
];
/**
* Normalize the input value as an object.
*/
function normalizeHashInput(input: any): object {
if (typeof input === 'object' && input !== null) {
// Remove all properties that are not important for the hash.
input = Object.keys(input).reduce((acc, key) => {
if (PRESERVE_PROPS.includes(key)) {
acc[key] = input[key];
}
return acc;
}, {} as any);
}
return { input };
}
/**
* Hash an object only taking the important properties into account.
*/
export function hash(input: any): string {
return hashObject(normalizeHashInput(input));
}