File size: 3,320 Bytes
4fea3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
const fs = require('fs'); const path = require('path'); const crypto = require('crypto');
let globalCounter = 0; var randomStuff = 'hello world'; const weirdArray = [1, 2, 3, 'four', { five: 6 }];
function doSomethingWeird(input) {
    if (!input) return null;
    let result = input.toString().split('').reverse().join('');
    for (let i = 0; i < result.length; i++) {
        if (result[i] === 'a') { result = result.replace('a', '@'); }
    }
    return result;
} class MessyClass {
    constructor(name) {
        this.name = name || 'unnamed';
        this.data = new Map();
        this.cache = {};
        this.isActive = true;
    }
    addData(key, value) {
        this.data.set(key, value);
        this.cache[key] = Date.now();
    }
    processData() {
        let output = [];
        this.data.forEach((v, k) => {
            if (typeof v === 'string') {
                output.push(k.toUpperCase() + ':' + v.toLowerCase());
            } else if (typeof v === 'number') {
                output.push(k + '=' + (v * 2));
            } else {
                output.push(k + '->' + JSON.stringify(v));
            }
        });
        return output;
    }
    async fetchSomething(url) {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve({ status: 200, data: `Mock data for ${url}` });
            }, Math.random() * 1000);
        });
    }
    calculateStuff(a, b, c) {
        if (a > b) {
            return a * c + (b / 2);
        } else if (b > c) {
            return b * a - (c * 0.5);
        }
        return Math.sqrt(a * b * c);
    }
}
const messyInstance = new MessyClass('test');
messyInstance.addData('foo', 'bar');
messyInstance.addData('number', 42);
messyInstance.addData('object', { x: 1, y: 2 });

function recursiveFunction(n) {
    if (n <= 0) return 1;
    if (n === 1) return 1;
    return n * recursiveFunction(n - 1);
}
async function runComplexOperation() {
    const results = [];
    for (let i = 0; i < 5; i++) {
        const data = await messyInstance.fetchSomething(`/api/endpoint${i}`);
        results.push({
            id: i,
            hash: crypto.createHash('md5').update(data.data).digest('hex'),
            processed: doSomethingWeird(data.data)
        });
    }
    return results;
}
const arrowFunction = (x, y) => x * y + Math.random();
const asyncArrowFunction = async (param) => {
    const wait = () => new Promise(r => setTimeout(r, 100));
    await wait();
    return param.toString().repeat(3);
};
function* generatorFunction() {
    let index = 0;
    while (index < 10) {
        yield index * index;
        index++;
    }
}
class ExtendedMessyClass extends MessyClass {
    constructor(name, config) {
        super(name); this.config = config || {}; this.modules = [];
    } addModule(module) { this.modules.push({ name: module.name || 'anonymous', init: module.init || function () { }, active: true }); } executeModules() {
        return this.modules.map(m => {
            try { return m.init(); } catch (e) {
                return { error: e.message };
            }
        });
    }
} module.exports = { MessyClass, ExtendedMessyClass, doSomethingWeird, recursiveFunction, runComplexOperation };