-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutil.js
More file actions
86 lines (75 loc) · 2.7 KB
/
Copy pathutil.js
File metadata and controls
86 lines (75 loc) · 2.7 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
// Copyright 2022 DeepL SE (https://www.deepl.com)
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
function cleanup(dictionary, lifetimeMs, callback) {
const now = new Date();
// Remove all objects with a used timestamp older than specified lifetime
dictionary.forEach((object, id) => {
if (now - object.used > lifetimeMs) {
callback(object, id);
dictionary.delete(id);
}
});
}
function scheduleCleanup(map, callback) {
// Schedules a repeated check for objects older than 10min in given map, callback is called
// for each expired object
setInterval(() => cleanup(map, 600000, callback), 1000);
}
class HttpError extends Error {
constructor(message, status, detail) {
super(message);
this.status_internal = status;
this.detail = detail;
}
status() {
return this.status_internal || 400;
}
body() {
const result = {};
if (this.message) { result.message = this.message; }
if (this.detail) { result.detail = this.detail; }
return result;
}
}
// This is a simplified implementation of this conversion that won't work for all
// BCP-47 codes, but all we currently use at DeepL.
// Before further complicating this implementation, it might make sense to switch
// to a library.
function convertToBcp47(langCode) {
const tokens = langCode.split('-');
return tokens.map((token, index) => {
if (index === 0) return token.toLowerCase();
// Script subtags are exactly 4 alphabetic characters and use title case (e.g. Hans, Hant)
if (token.length === 4 && /^[a-zA-Z]{4}$/.test(token)) {
return token.charAt(0).toUpperCase() + token.slice(1).toLowerCase();
}
return token.toUpperCase();
}).join('-');
}
/**
* Given any two objects with sourceLang and targetLang parameters, checks if they match.
*/
function langPairMatches(dictL, dictR) {
return dictL.sourceLang.toLowerCase() === dictR.sourceLang.toLowerCase()
&& dictL.targetLang.toLowerCase() === dictR.targetLang.toLowerCase();
}
/**
* Generates a random hexadecimal string of the specified length.
*/
function generateRandomHexString(length) {
const hex = '0123456789ABCDEF';
let output = '';
for (let i = 0; i < length; i += 1) {
output += hex.charAt(Math.floor(Math.random() * 16));
}
return output;
}
// Mirrors uuid v8's `validate()`: any RFC 4122 v1–v5 UUID, or the nil UUID.
const UUID_RE = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
function isValidUuid(s) {
return typeof s === 'string' && UUID_RE.test(s);
}
module.exports = {
convertToBcp47, langPairMatches, scheduleCleanup, HttpError, generateRandomHexString, isValidUuid,
};