ModApp Utilities
ModApp Utilities (modapp-utils) provides a collection of generic utility functions intended for use across the broader ModApp ecosystem of JavaScript packages. These utilities typically cover common programming patterns such as object manipulation (e.g., deep merging, cloning), array helpers, string transformations, event debouncing/throttling, and unique ID generation. As of version 1.8.0, it serves as a foundational library, abstracting common logic to ensure consistency and reduce boilerplate in related ModApp projects. The package does not explicitly detail its release cadence, but incremental updates can be expected to support the evolving ModApp framework. Its key differentiator is its tailored integration and consistency within the ModApp architecture, rather than offering novel utility functions compared to broader libraries like Lodash or Ramda.
Common errors
-
TypeError: (0, modapp_utils__WEBPACK_IMPORTED_MODULE_0__.debounce) is not a function
cause Attempting to use a named export like `debounce` after an incorrect import, or if `debounce` was not exported or renamed.fixVerify the exact name of the exported function from `modapp-utils`. Ensure you are using named imports: `import { debounce } from 'modapp-utils';`. -
ERR_REQUIRE_ESM: require() of ES Module path/to/node_modules/modapp-utils/index.js from path/to/your/file.js not supported. Instead change the require of index.js to a dynamic import() call.
cause `modapp-utils` is an ES Module, but it is being imported into a CommonJS module using `require()`.fixEither convert your consuming file to an ES Module (by using `import` statements and ensuring your `package.json` has `"type": "module"` or your file ends in `.mjs`), or use dynamic `import('modapp-utils')` if you must remain in CommonJS. -
SyntaxError: Named export 'mergeDeep' not found. The requested module 'modapp-utils' does not provide an export named 'mergeDeep'
cause The module `modapp-utils` does not export a symbol named `mergeDeep`, or it is exported under a different name, or the module itself is incorrectly structured.fixDouble-check the `modapp-utils` documentation or its source code to confirm the correct export name for the deep merge utility. It might be named `deepMerge`, `assignDeep`, or similar. Adjust your import statement accordingly.
Warnings
- gotcha As a utility library, `modapp-utils` likely exports numerous functions. Incorrectly importing non-existent functions or attempting to import a default export when only named exports are available will result in runtime errors.
- breaking Older versions of `modapp-utils` (prior to major version bumps, if any occurred) might have exclusively used CommonJS. Modern versions are likely ESM-first, which can cause `ERR_REQUIRE_ESM` errors in CommonJS-only Node.js environments.
- gotcha While utility libraries are often prime candidates for tree-shaking, the effectiveness of tree-shaking for `modapp-utils` depends on its internal structure and how it's bundled. If not properly implemented, unused utility functions might still be included in your final bundle, increasing its size.
Install
-
npm install modapp-utils -
yarn add modapp-utils -
pnpm add modapp-utils
Imports
- debounce
const { debounce } = require('modapp-utils');import { debounce } from 'modapp-utils'; - mergeDeep
import mergeDeep from 'modapp-utils/mergeDeep';
import { mergeDeep } from 'modapp-utils'; - generateUUID
import { uuid } from 'modapp-utils';import { generateUUID } from 'modapp-utils';
Quickstart
import { debounce, mergeDeep, generateUUID } from 'modapp-utils';
// Example 1: Debouncing a function
const searchInputHandler = (query) => {
console.log('Searching for:', query);
// Simulate API call
};
const debouncedSearch = debounce(searchInputHandler, 500);
document.addEventListener('DOMContentLoaded', () => {
const input = document.createElement('input');
input.placeholder = 'Type to search (debounced)';
input.onkeyup = (event) => debouncedSearch(event.target.value);
document.body.appendChild(input);
// Example 2: Deep merging objects
const defaultConfig = { host: 'localhost', port: 8080, db: { user: 'guest', pass: 'guest' } };
const userConfig = { port: 3000, db: { user: 'admin' }, api: { key: process.env.API_KEY ?? '' } };
const mergedConfig = mergeDeep(defaultConfig, userConfig);
console.log('Merged Configuration:', mergedConfig);
// Example 3: Generating a UUID
const newId = generateUUID();
console.log('Generated UUID:', newId);
// Cleanup for demonstration purposes
setTimeout(() => {
console.log('Demonstration complete. Open your browser console.');
input.remove();
}, 2000);
});