legal-markdown-js
Version:
Node.js implementation of LegalMarkdown for processing legal documents with markdown and YAML - Complete feature parity with Ruby version
90 lines • 2.73 kB
JavaScript
/**
* Functional Programming Utilities
*
* This module provides functional programming utilities and patterns
* for the Legal Markdown processing system.
*
* Features:
* - Function debouncing for performance optimization
* - Deep object merging for configuration handling
* - Type guards and utility functions
*
* @example
* ```typescript
* import { debounce, deepMerge } from './functional.js';
*
* // Create debounced function
* const debouncedSave = debounce(saveDocument, 300);
*
* // Merge configuration objects
* const config = deepMerge(defaultConfig, userConfig);
* ```
*
* @module
*/
/**
* Creates a debounced version of a function that delays execution
*
* @param {T} func - The function to debounce
* @param {number} wait - The delay in milliseconds
* @returns {Function} The debounced function
* @example
* ```typescript
* const debouncedSave = debounce(saveDocument, 300);
* debouncedSave(content); // Will only execute after 300ms of inactivity
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function debounce(func, wait) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), wait);
};
}
/**
* Performs a deep merge of objects, combining nested properties
*
* @param {T} target - The target object to merge into
* @param {...Partial<T>} sources - The source objects to merge from
* @returns {T} The merged object
* @example
* ```typescript
* const merged = deepMerge(
* { a: { b: 1 } },
* { a: { c: 2 } }
* );
* // Returns: { a: { b: 1, c: 2 } }
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function deepMerge(target, ...sources) {
if (!sources.length)
return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key])
Object.assign(target, { [key]: {} });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
deepMerge(target[key], source[key]);
}
else {
Object.assign(target, { [key]: source[key] });
}
}
}
return deepMerge(target, ...sources);
}
/**
* Type guard to check if an item is a plain object
*
* @param {any} item - The item to check
* @returns {boolean} True if the item is a plain object, false otherwise
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
//# sourceMappingURL=functional.js.map