Spaces:
Paused
Paused
File size: 3,696 Bytes
93d826e | 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | // ES Module imports
import { EventEmitter } from 'events';
import { promisify } from 'util';
// Global constants
const MAX_RETRIES = 3;
const DEFAULT_TIMEOUT = 5000;
// Symbol for private properties
const privateState = Symbol('privateState');
// Class using ES6+ features
class DataProcessor extends EventEmitter {
// Private class field
#cache = new Map();
// Static class field
static version = '1.0.0';
// Constructor with parameter destructuring
constructor({ maxRetries = MAX_RETRIES, timeout = DEFAULT_TIMEOUT } = {}) {
super();
this[privateState] = { maxRetries, timeout };
}
// Async method with error handling
async processData(data) {
try {
const result = await this.#validateAndTransform(data);
this.emit('processed', result);
return result;
} catch (error) {
this.emit('error', error);
throw error;
}
}
// Private method
async #validateAndTransform(data) {
if (!data) throw new Error('Data is required');
return { ...data, timestamp: Date.now() };
}
// Generator method
*iterateCache() {
for (const [key, value] of this.#cache) {
yield { key, value };
}
}
}
// Decorator function (stage 3 proposal)
function deprecated(target, context) {
if (context.kind === 'method') {
const originalMethod = target;
return function(...args) {
console.warn(`Warning: ${context.name} is deprecated`);
return originalMethod.apply(this, args);
};
}
}
// Proxy example
const handler = {
get(target, prop) {
return prop in target ? target[prop] : 'Property not found';
}
};
const proxy = new Proxy({}, handler);
// Promise-based utility function
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// Async generator function
async function* generateSequence(start, end) {
for (let i = start; i <= end; i++) {
await delay(100);
yield i;
}
}
// Higher-order function
const memoize = (fn) => {
const cache = new Map();
return (...args) => {
const key = JSON.stringify(args);
if (cache.has(key)) return cache.get(key);
const result = fn.apply(this, args);
cache.set(key, result);
return result;
};
};
// Custom error class
class ValidationError extends Error {
constructor(message, field) {
super(message);
this.name = 'ValidationError';
this.field = field;
}
}
// Object with getter/setter
const config = {
_theme: 'light',
get theme() {
return this._theme;
},
set theme(value) {
if (!['light', 'dark'].includes(value)) {
throw new ValidationError('Invalid theme', 'theme');
}
this._theme = value;
}
};
// Array methods and destructuring
const processItems = (items) => {
const [first, ...rest] = items;
return rest
.filter(item => item != null)
.map(item => ({ ...item, processed: true }))
.reduce((acc, curr) => {
acc[curr.id] = curr;
return acc;
}, {});
};
// Async/await with Promise.all
const fetchData = async (urls) => {
try {
const responses = await Promise.all(
urls.map(url => fetch(url).then(res => res.json()))
);
return responses;
} catch (error) {
console.error('Failed to fetch data:', error);
throw error;
}
};
// Export statement
export {
DataProcessor,
ValidationError,
processItems,
fetchData,
delay,
memoize,
config
};
|