Mastering Aggregated Exports in JavaScript Modules
Aggregating Exports:
Write a JavaScript program to aggregate exports from two different modules into one module and use them.
Solution-1:
JavaScript Code:
File: mathUtils.js
// mathUtils.js
// This file contains named exports for mathematical operations.
// Exporting functions from mathUtils.js
export const add = (a, b) => a + b; // Adds two numbers
export const subtract = (a, b) => a - b; // Subtracts two numbers
File: stringUtils.js
// stringUtils.js
// This file contains named exports for string operations.
// Exporting functions from stringUtils.js
export const toUpperCase = (str) => str.toUpperCase(); // Converts a string to uppercase
export const toLowerCase = (str) => str.toLowerCase(); // Converts a string to lowercase
File: index.js
// index.js
//This file aggregates exports from both modules.
// Aggregating exports from mathUtils and stringUtils
export * from './mathUtils.js'; // Aggregates all exports from mathUtils
export * from './stringUtils.js'; // Aggregates all exports from stringUtils
File: main.js
//main.js
// This file imports and uses the aggregated exports.
// Importing aggregated exports
import { add, subtract, toUpperCase, toLowerCase } from './index.js';
// Using the imported functions
console.log(add(5, 3)); // Logs 8
console.log(subtract(10, 4)); // Logs 6
console.log(toUpperCase('hello')); // Logs HELLO
console.log(toLowerCase('WORLD')); // Logs world
Output:
8 6 HELLO world
Explanation:
- mathUtils.js and stringUtils.js define named exports for different utilities.
- index.js aggregates exports from both modules using export *.
- main.js imports all functions directly from index.js and uses them.
Solution-2: Aggregating Exports Using Named Imports and Exports
JavaScript Code:
File: stringUtils.js
// stringUtils.js
// This file contains named exports for string operations (same as above).
// Exporting functions from stringUtils.js
export const toUpperCase = (str) => str.toUpperCase(); // Converts a string to uppercase
export const toLowerCase = (str) => str.toLowerCase(); // Converts a string to lowercase
File: mathUtils.js
// mathUtils.js
// This file contains named exports for mathematical operations (same as above).
// Exporting functions from mathUtils.js
export const add = (a, b) => a + b; // Adds two numbers
export const subtract = (a, b) => a - b; // Subtracts two numbers
File: index.js
// index.js
// This file explicitly imports and exports the modules.
// Importing named exports from both modules
import { add, subtract } from './mathUtils.js';
import { toUpperCase, toLowerCase } from './stringUtils.js';
// Re-exporting them from index.js
export { add, subtract, toUpperCase, toLowerCase };
File: main.js
//main.js
// This file imports and uses the aggregated exports.
// Importing aggregated exports
import { add, subtract, toUpperCase, toLowerCase } from './index.js';
// Using the imported functions
console.log(add(7, 2)); // Logs 9
console.log(subtract(15, 5)); // Logs 10
console.log(toUpperCase('coding')); // Logs CODING
console.log(toLowerCase('EXERCISES')); // Logs exercise
Output:
9 10 CODING exercises
Explanation:
- mathUtils.js and stringUtils.js define named exports for different utilities.
- index.js explicitly imports the exports from these modules and re-exports them.
- main.js imports the aggregated exports from index.js and uses them.
Improve this sample solution and post your code through Disqus
Previous: Constants Export and Import in JavaScript Modules.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics