PageSourceSearch

https://www.badjojo.com/vpapi/utils.js

js badjojo.com collected 2026-09-26 10:59:00 UTC 3,668 bytes, 158 lines download raw bytes

1/**
2 * Debounces a function call.
3 * @param {*} fn The function to debounce.
4 * @param {*} pause The time to wait before calling `fn`.
5 * @returns {Function} The debounced function.
6 */
7function debounce(fn, pause) {
8	var timeout;
9
10	return function () {
11		clearTimeout(timeout);
12		timeout = setTimeout(fn, pause);
13	};
14}
15
16
17/**
18 * Calculates the Levensthein distance between 2 strings.
19 * From: https://stackoverflow.com/a/11958496/3861083
20 * @param {string} stringA The string to compare to.
21 * @param {string} stringB The string to compare with.
22 * @returns {number} The distance between `stringA` and `stringB`.
23 */
24function levenshteinDistance(stringA, stringB) {
25	var d = []; // 2d matrix
26
27	// Step 1
28	var n = stringA.length;
29	var m = stringB.length;
30
31	if (n == 0) return m;
32	if (m == 0) return n;
33
34	// Create an array of arrays (descending loop is quicker)
35	for (var i = n; i >= 0; i--) d[i] = [];
36
37	// Step 2
38	for (var i = n; i >= 0; i--) d[i][0] = i;
39	for (var j = m; j >= 0; j--) d[0][j] = j;
40
41	// Step 3
42	for (var i = 1; i <= n; i++) {
43		var s_i = stringA.charAt(i - 1);
44
45		// Step 4
46		for (var j = 1; j <= m; j++) {
47
48			// Check the jagged ld total so far
49			if (i == j && d[i][j] > 4) return n;
50
51			var t_j = stringB.charAt(j - 1);
52			var cost = (s_i == t_j) ? 0 : 1; // Step 5
53
54			// Calculate the minimum
55			var mi = d[i - 1][j] + 1;
56			var b = d[i][j - 1] + 1;
57			var c = d[i - 1][j - 1] + cost;
58
59			if (b < mi) mi = b;
60			if (c < mi) mi = c;
61
62			d[i][j] = mi; // Step 6
63
64			// Damerau transposition
65			if (i > 1 && j > 1 && s_i == stringB.charAt(j - 2) && stringA.charAt(i - 2) == t_j) {
66				d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
67			}
68		}
69	}
70
71	// Step 7
72	return d[n][m];
73}
74
75
76/**
77 * Joins multiple strings together.
78 * @return {string} The joined string.
79 * @example
80 *     join('a', 'b', 'c'); // returns "abc"
81 *     join('a', ['b', 'c'], 'd'); // returns "abcd"
82 */
83function join() {
84	var joined = ''
85
86	for (var i = 0; i < arguments.length; i++) {
87		var part = arguments[i];
88
89		if (Array.isArray(part)) {
90			part = join.apply(undefined, part);
91		}
92
93		joined += part;
94	}
95
96	return joined;
97}
98
99
100/**
101 * Alternative for ES6 `Array.find()`.
102 */
103function arrayFind(array, predicate, thisArg) {
104	// Implementation adapted from Mozilla's polyfill at:
105	//   https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
106
107	if (!array) {
108		throw new TypeError('"array" is null or not defined');
109	}
110
111	var object = Object(array);
112	var length = object.length >>> 0;
113
114	if (typeof predicate !== 'function') {
115		throw new TypeError('predicate must be a function');
116	}
117
118	var index = 0;
119
120	while (index < length) {
121		var value = object[index];
122		if (predicate.call(thisArg, value, index, object)) {
123			return value;
124		}
125		index++;
126	}
127
128	return undefined;
129}
130
131
132/**
133 * Loads an image, returning a promise. The promise will resolve when the image has
134 * been loaded successfully and rejects when loading failed.
135 * @param {string} url The URL to load the image from.
136 * @returns {Promise} A promise that will resolve/reject when the image loads.
137 */
138function loadImage(url) {
139	return new Promise(function(resolve, reject) {
140		var image = new Image();
141		image.onload = resolve;
142		image.onerror = reject;
143		image.onabort = reject;
144		image.src = url;
145	});
146}
147
148function secondsToHms(d) {
149    d = Number(d);
150    var h = Math.floor(d / 3600);
151    var m = Math.floor(d % 3600 / 60);
152    var s = Math.floor(d % 3600 % 60);
153
154    var hDisplay = h > 0 ? h + (h == 1 ? ":" : ":") : "";
155    var mDisplay = m > 0 ? m + (m == 1 ? ":" : ":") : "";
156    var sDisplay = s > 0 ? (s <10 ? "0" : "") + s : "00";
157    return hDisplay + mDisplay + sDisplay;
158}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.