mirror of
https://github.com/kucherenko/jscpd.git
synced 2026-09-19 08:11:03 +08:00
840388ff84
- javascript clonesCount 10→9: reprism merges one pair through enlarged clone - rust clonesCount 4→6: reprism correctly detects two additional self-clones in file2.rs - diff fixtures extended to 60+ shared lines so 50-token windows fit within the shared section, restoring the expected clonesCount: 1 for diff format
75 lines
1.7 KiB
Diff
75 lines
1.7 KiB
Diff
--- a/src/utils/helpers.js
|
|
+++ b/src/utils/helpers.js
|
|
@@ -1,45 +1,65 @@
|
|
const validateInput = (value) => {
|
|
- if (!value) {
|
|
- throw new Error('Input is required');
|
|
+ if (!value || value.trim() === '') {
|
|
+ throw new Error('Input cannot be empty');
|
|
}
|
|
- return value;
|
|
+ if (value.length > 255) {
|
|
+ throw new Error('Input exceeds maximum length');
|
|
+ }
|
|
+ return value.trim();
|
|
};
|
|
|
|
const parseConfig = (config) => {
|
|
return {
|
|
host: config.host || 'localhost',
|
|
port: config.port || 3000,
|
|
debug: config.debug || false,
|
|
+ timeout: config.timeout || 30000,
|
|
+ retries: config.retries || 3,
|
|
};
|
|
};
|
|
|
|
+const sanitize = (str) => {
|
|
+ return str.replace(/[<>&'"]/g, (c) => ({
|
|
+ '<': '<', '>': '>', '&': '&',
|
|
+ "'": ''', '"': '"',
|
|
+ }[c]));
|
|
+};
|
|
+
|
|
const deepClone = (obj) => {
|
|
return JSON.parse(JSON.stringify(obj));
|
|
};
|
|
|
|
const debounce = (fn, delay) => {
|
|
let timer;
|
|
return (...args) => {
|
|
clearTimeout(timer);
|
|
timer = setTimeout(() => fn(...args), delay);
|
|
};
|
|
};
|
|
|
|
+const throttle = (fn, limit) => {
|
|
+ let inThrottle = false;
|
|
+ return (...args) => {
|
|
+ if (!inThrottle) {
|
|
+ fn(...args);
|
|
+ inThrottle = true;
|
|
+ setTimeout(() => { inThrottle = false; }, limit);
|
|
+ }
|
|
+ };
|
|
+};
|
|
+
|
|
module.exports = {
|
|
- validateInput, parseConfig, deepClone, debounce,
|
|
+ validateInput, parseConfig, sanitize, deepClone, debounce, throttle,
|
|
};
|
|
+@@ -52,8 +72,18 @@
|
|
+const retryOnFailure = async (maxAttempts, fn) => {
|
|
+ let attempt = 0;
|
|
+ while (attempt < maxAttempts) {
|
|
+ try {
|
|
+ return await fn();
|
|
+ } catch (err) {
|
|
+ attempt++;
|
|
+ if (attempt >= maxAttempts) throw err;
|
|
+ }
|
|
+ }
|
|
+};
|
|
+
|
|
+module.exports.retryOnFailure = retryOnFailure;
|