Skip to content

Commit 34ada59

Browse files
committed
wip: show identity strings
1 parent cd25a54 commit 34ada59

File tree

4 files changed

+72
-29
lines changed

4 files changed

+72
-29
lines changed

demo/index.html

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
const scl1 = document.getElementById('scl1');
1717
const result = document.getElementById('result');
1818

19-
const { hash, db } = newHasher();
19+
const { hash, db, idDb } = newHasher();
2020

2121
function display(description) {
2222
console.log('displaying', description);
@@ -29,18 +29,21 @@
2929
const sum = document.createElement('summary');
3030
det.append(sum);
3131
const d = db[tag][hash];
32-
sum.textContent = tag + ' ' + hash;
32+
sum.textContent = tag + ' ' + (idDb[hash] || hash);
3333
det.addEventListener('toggle', () => {if (det.children.length < 2) det.append(...display(db[tag][hash]))})
3434
elements.push(det);
3535
}
3636
} else if ( key === 'eNS') {
37+
console.log('ens!', value);
3738
const det = document.createElement('details');
3839
const sum = document.createElement('summary');
40+
det.setAttribute('open', '');
3941
det.append(sum);
4042
sum.textContent = 'extension NS';
4143
Object.entries(value).forEach(([ns, attrs]) => {
4244
const de = document.createElement('details');
4345
const su = document.createElement('summary');
46+
de.setAttribute('open', '');
4447
de.append(su);
4548
su.textContent = ns;
4649
Object.entries(attrs).forEach(([a,v]) => {
@@ -53,7 +56,9 @@
5356
val.textContent = v
5457
de.append(val);
5558
})
59+
det.append(de)
5660
})
61+
elements.push(det);
5762
} else {
5863
const attr = document.createElement('div');
5964
attr.setAttribute('class','column');
@@ -85,31 +90,36 @@
8590
const det = document.createElement('details');
8691
const sum = document.createElement('summary');
8792
det.append(sum);
88-
sum.textContent = tag + ' ' + hash;
93+
sum.textContent = tag + ' ' + (idDb[hash] || hash);
8994
det.addEventListener('toggle', () => {console.log('toggled!'); if (det.children.length < 2) det.append(...display(db[tag][hash]))})
9095
details.append(det);
9196
}
9297
} else if ( key === 'eNS') {
93-
const det = document.createElement('details');
94-
const sum = document.createElement('summary');
95-
det.append(sum);
96-
sum.textContent = 'extension NS';
97-
Object.entries(value).forEach(([ns, attrs]) => {
98-
const de = document.createElement('details');
99-
const su = document.createElement('summary');
100-
de.append(su);
101-
su.textContent = ns;
102-
Object.entries(attrs).forEach(([a,v]) => {
103-
const attr = document.createElement('div');
104-
attr.setAttribute('class','column');
105-
attr.textContent = a
106-
de.append(attr);
107-
const val = document.createElement('div');
108-
val.setAttribute('class','column');
109-
val.textContent = v
110-
de.append(val);
111-
})
98+
console.log('ens!', value);
99+
const det = document.createElement('details');
100+
const sum = document.createElement('summary');
101+
det.setAttribute('open', '');
102+
det.append(sum);
103+
sum.textContent = 'extension NS';
104+
Object.entries(value).forEach(([ns, attrs]) => {
105+
const de = document.createElement('details');
106+
const su = document.createElement('summary');
107+
de.setAttribute('open', '');
108+
de.append(su);
109+
su.textContent = ns;
110+
Object.entries(attrs).forEach(([a,v]) => {
111+
const attr = document.createElement('div');
112+
attr.setAttribute('class','column');
113+
attr.textContent = a
114+
de.append(attr);
115+
const val = document.createElement('div');
116+
val.setAttribute('class','column');
117+
val.textContent = v
118+
de.append(val);
112119
})
120+
det.append(de)
121+
})
122+
details.append(det);
113123
} else {
114124
const attr = document.createElement('div');
115125
attr.setAttribute('class','column');
@@ -130,8 +140,8 @@
130140

131141
<style>
132142
body {
133-
background: #ccc;
134-
color: #222;
143+
background: #edc;
144+
color: #014;
135145
font-family: Roboto;
136146
font-weight: 300;
137147
}
@@ -142,7 +152,7 @@
142152
padding: 4px;
143153
opacity: 0.8;
144154
vertical-align: middle;
145-
border: 1px #ccc solid;
155+
border: 1px #edc solid;
146156
border-radius: 6px;
147157
}
148158
.column:nth-of-type(2n+1) {

hash.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import xxhash from "xxhash-wasm";
2+
import { identity } from "@openenergytools/scl-lib";
23

34
const xxh = await xxhash();
45

56
export type HashDB = Record<string, Record<string, object>>;
7+
export type IdentityDB = Record<string, string | number>;
68
export type Hasher = (e: Element) => string;
79

810
const xmlTruths = new Set(["true", "1"]);
@@ -56,8 +58,18 @@ const attributes: {
5658

5759
export function hasher(
5860
db: HashDB,
61+
idDb: IdentityDB,
5962
{
60-
ignoreAttrs = new Set(["desc", "id", "name", "type", "inst", "lnType"]),
63+
ignoreAttrs = new Set([
64+
"desc",
65+
"id",
66+
"name",
67+
"DO.type",
68+
"DA.type",
69+
"BDA.type",
70+
"inst",
71+
"lnType",
72+
]),
6173
hashENS,
6274
}: { ignoreAttrs?: Set<string>; hashENS?: string[] } = {},
6375
): (e: Element) => string {
@@ -69,6 +81,7 @@ export function hasher(
6981
Array.from(e.attributes)
7082
.map((a) => a.localName)
7183
.filter((a) => !ignoreAttrs.has(a))
84+
.filter((a) => !ignoreAttrs.has(e.tagName + "." + a))
7285
.sort()
7386
.forEach((name) => {
7487
if (name in defaults) description[name] = defaults[name];
@@ -100,8 +113,8 @@ export function hasher(
100113
.map((c) => c.tagName)
101114
.filter((c, i, arr) => arr.indexOf(c) === i);
102115
const description: Record<string, unknown> = {
103-
...describeChildren(e, ...children),
104116
...describeAttributes(e),
117+
...describeChildren(e, ...children),
105118
};
106119
const eNSAttrs = Array.from(e.attributes).filter((a) => a.namespaceURI);
107120
if (eNSAttrs.length) {
@@ -247,13 +260,21 @@ export function hasher(
247260
const digest = xxh.h64ToString(JSON.stringify(description));
248261
if (!(tag in db)) db[tag] = {};
249262
if (!(digest in db[tag])) db[tag][digest] = description;
263+
if (!(digest in idDb)) {
264+
idDb[digest] = identity(e);
265+
}
250266
return digest;
251267
}
252268

253269
return hash;
254270
}
255271

256-
export function newHasher(options = {}): { hash: Hasher; db: HashDB } {
272+
export function newHasher(options = {}): {
273+
hash: Hasher;
274+
db: HashDB;
275+
idDb: IdentityDB;
276+
} {
257277
const db: HashDB = {};
258-
return { hash: hasher(db, options), db };
278+
const idDb: IdentityDB = {};
279+
return { hash: hasher(db, idDb, options), db, idDb };
259280
}

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"root": true
5656
},
5757
"dependencies": {
58+
"@openenergytools/scl-lib": "^0.13.3",
5859
"xxhash-wasm": "^1.0.2"
5960
}
6061
}

0 commit comments

Comments
 (0)