-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
86 lines (72 loc) · 1.87 KB
/
utils.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
const fs = require('fs-extra');
const options = {};
function getHash(item) {
'use strict';
let uri = item['@id'];
if (!uri) return null;
if (uri.includes('#'))
return uri.split('#').pop();
return options.ontPrefix ? uri.replace(options.ontPrefix + ":", "") : uri;
}
function isSignificative(prop) {
'use strict';
var notSignificatifProps = [
'@type', '@id', 'rdfs:isDefinedBy'
];
return !notSignificatifProps.includes(prop);
}
function print(value, single) {
'use strict';
if (typeof value === 'string')
return value;
else if (Array.isArray(value)) {
if (single)
return print(value.sort(sortByLang)[0], single);
else
return value.map((v) => print(v)).join('\n');
} else if (value['@id']) {
return `<a href="${regenerateLink(value['@id'])}">${value['@id']}</a>`;
} else {
let text = value['@value'];
if (!single) {
let lang = value['@language'];
if (lang) text += `<small>@${lang}</small>`;
}
return text;
}
}
function regenerateLink(short) {
if (!short.includes(':')) return short;
const [prefix, id] = short.split(':');
return options.context[prefix] + id;
}
function sortByLang(a, b) {
let al = a["@language"],
bl = b["@language"];
// first no lang
if (!al) return -1;
if (!bl) return 1;
// then english
if (al == "en") return -1;
if (bl == "en") return 1;
// then the rest
return al > bl ? 1 : -1;
}
function mkdirInCase(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
function copySync(src, dest) {
if (!fs.existsSync(src))
return false;
fs.createReadStream(src).pipe(fs.createWriteStream(dest));
}
module.exports = {
getHash,
mkdirInCase,
copySync,
print,
isSignificative,
options
}