-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCOMPARE.js
More file actions
213 lines (183 loc) · 7.37 KB
/
Copy pathCOMPARE.js
File metadata and controls
213 lines (183 loc) · 7.37 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
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
var fs = require("fs");
var path = require("path");
var ztxt = fs.readFileSync("schema.json");
var jtxt = fs.readFileSync("schema-jurism.json");
var ptxt = fs.readFileSync("schema-jurism-patch.json");
var zobj = JSON.parse(ztxt);
var jobj = JSON.parse(jtxt);
var patch = JSON.parse(ptxt);
/*
So ... what output do I need from this?
Sources
schema-jurism.patch
TYPES: preload this data, and remove where
a match is found, since native Zotero and native
Jurism can be compared.
CREATORS: extended creator types for specific Jurism
item types. These can be derived from z and j schema.json.
FIELDS: extended fields for specific item types. These
can be derived from z and j schema.json.
SEQUENCE: display order of item type fields. This can
be derived from the j schema.json, with warnings listed
to console for fields added by z schema.json.
CSL_FIELDS: these can just be copied across to the
new patch file.
CSL_DATES: these extensions can just be copied across
to the new patch file.
CSL_NAMES: this empty object can just be copied across
to the new patch file.
*/
function KeyCheck () {
this.itemTypeNames = (zobj, jobj) => {
var ret = {};
for (let idx=0; idx<zobj.itemTypes.length; idx++) {
ret[zobj.itemTypes[idx].itemType] = {zotero: idx};
}
for (let idx=0; idx<jobj.itemTypes.length; idx++) {
if (!ret[jobj.itemTypes[idx].itemType]) {
ret[jobj.itemTypes[idx].itemType] = {};
}
ret[jobj.itemTypes[idx].itemType].jurism = idx;
}
return ret;
};
this.creatorNames = (zIT, jIT) => {
var ret = {};
for (let idx=0; idx<zIT.creatorTypes.length; idx++) {
ret[zIT.creatorTypes[idx].creatorType] = {zotero: idx};
}
for (let idx=0; idx<jIT.creatorTypes.length; idx++) {
if (!ret[jIT.creatorTypes[idx].creatorType]) {
ret[jIT.creatorTypes[idx].creatorType] = {};
}
ret[jIT.creatorTypes[idx].creatorType].jurism = idx;
}
return ret;
};
this.fieldNames = (zIT, jIT) => {
var ret = {};
for (let idx=0; idx<zIT.fields.length; idx++) {
ret[zIT.fields[idx].field] = {zotero: idx};
}
for (let idx=0; idx<jIT.fields.length; idx++) {
if (!ret[jIT.fields[idx].field]) {
ret[jIT.fields[idx].field] = {};
}
ret[jIT.fields[idx].field].jurism = idx;
}
return ret;
};
this.getItemPairs = (key) => {
var ret = {};
var zITidx = this.typeIdx[key].zotero;
var jITidx = this.typeIdx[key].jurism;
// If a type is completely new in Zotero (like dataset),
// there won't be any Jurism extensions.
if (jITidx) {
ret.zotero = zobj.itemTypes[zITidx];
ret.jurism = jobj.itemTypes[jITidx];
} else {
ret = false;
}
return ret;
};
this.walkish = (zobj, jobj, patch) => {
this.typeIdx = this.itemTypeNames(zobj, jobj);
this.forTYPES = (zobj, jobj, patch) => {
var adoptedByZotero = {};
// For item types adopted by Zotero, remove them
// from the TYPES patch, but make a note of the
// base type of each, so that we can be sure all
// fields are included when we later process FIELDS.
for (let k in patch.TYPES) {
if (this.typeIdx[k].zotero !== undefined) {
adoptedByZotero[k] = patch[k];
}
}
for (let k in adoptedByZotero) {
delete patch.TYPES[k];
}
// For each of our remaining Jurism extended types,
// add them to zobj with a copy of their base type,
// for use in creator and field comparisons.
for (let k in patch.TYPES) {
var baseType = patch.TYPES[k].zotero;
let idx = this.typeIdx[baseType].zotero;
let typeCopy = JSON.parse(JSON.stringify(zobj.itemTypes[idx]));
typeCopy.itemType = k;
zobj.itemTypes.push(typeCopy);
this.typeIdx[k].zotero = (zobj.itemTypes.length - 1);
}
};
this.forCREATORS = (zobj, jobj) => {
var ret = {};
for (let k in this.typeIdx) {
var itemPair = this.getItemPairs(k);
if (itemPair) {
var creatorIdx = this.creatorNames(itemPair.zotero, itemPair.jurism);
for (let j in creatorIdx) {
if (creatorIdx[j].zotero === undefined) {
if (!ret[k]) {
ret[k] = [];
}
ret[k].push(j);
}
}
}
}
patch.CREATORS = ret;
};
this.forFIELDS = (zobj, jobj, patch) => {
var ret = {};
for (let k in this.typeIdx) {
var itemPair = this.getItemPairs(k);
if (itemPair) {
var fieldIdx = this.fieldNames(itemPair.zotero, itemPair.jurism);
for (let j in fieldIdx) {
if (fieldIdx[j].zotero === undefined) {
if (patch.DATES[k] && patch.DATES[k].indexOf(j) > -1) continue;
// I think we just copy the field spec into ret,
// as the new patch spec? If fields are found to
// be present in new Zotero, they'll be dropped,
// which is the only adjustment needed for this one.
if (!ret[k]) {
ret[k] = [];
}
ret[k].push(JSON.parse(JSON.stringify(itemPair.jurism.fields[fieldIdx[j].jurism])));
}
}
}
}
patch.FIELDS = ret;
};
this.forSEQUENCE = (zobj, jobj, patch) => {
var ret = {};
for (let k in this.typeIdx) {
var itemType = zobj.itemTypes[this.typeIdx[k].zotero];
var fields = itemType.fields;
for (let fieldInfo of fields) {
var field = fieldInfo.field;
if (!patch.SEQUENCE[k] || patch.SEQUENCE[k].indexOf(field) == -1) {
if (!ret[k]) {
ret[k] = [];
}
ret[k].push(field);
}
}
}
console.log(`Fields missing from SEQUENCE spec:\n${JSON.stringify(ret, null, 2)}`);
};
this.forTYPES(zobj, jobj, patch);
this.forCREATORS(zobj, jobj, patch);
this.forFIELDS(zobj, jobj, patch);
// DATES is fine.
this.forSEQUENCE(zobj, jobj, patch);
// CSL_FIELDS is fine.
// CSL_DATES is fine.
// CSL_NAMES is fine.
return patch;
};
}
var keys = new KeyCheck();
var ret = keys.walkish(zobj, jobj, patch);
fs.writeFileSync("schema-jurism-patch-new.json", JSON.stringify(ret, null, 2))