-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
69 lines (60 loc) · 1.73 KB
/
util.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
import { $ } from "bun";
import { extname } from "bun:path";
export const renameToHash = async (name) => {
const ext = extname(name);
const _hash = await $`cat $NAME | shasum -a 256 | head -c 64`.env({ NAME: name }).text();
const hash = _hash.trim();
let hashName = `${hash}${ext}`;
if (hash === hashName) {
const _mimeType = await $`file -b --mime-type $NAME`.env({ NAME: name }).text();
const [_t1, _t2] = _mimeType.trim().split('/');
if (_t1.trim() == 'image') {
hashName = `${hash}.${_t2}`;
}
}
await $`mv $SRC $DEST`.env({ SRC: name, DEST: hashName }).quiet();
return [hash, hashName];
};
export const selectBestString = (strings, priorities) => {
// Create a map to store the scores for each string
const scores = new Map();
// Iterate over the strings
for (const str of strings) {
let score = 0;
// Iterate over the priorities
for (const [priority, weight] of priorities) {
// Check if the string matches the priority
if (str.match(priority)) {
// Add the weight to the score
score += weight;
}
}
// Add the score to the map
scores.set(str, score);
}
// Find the string with the highest score
let bestString = null;
let bestScore = -Infinity;
for (const [str, score] of scores.entries()) {
if (score > bestScore) {
bestString = str;
bestScore = score;
}
}
// Return the best string
return bestString;
};
export const querySync = async (relay, filter) => {
return new Promise(async resolve => {
const events = [];
const sub = relay.subscribe([filter], {
onevent(event) {
events.push(event);
},
oneose() {
resolve(events);
sub.close();
},
});
});
};