Babel plugin that compiles ES2015 computed properties to ES5-compatible code
88
Build a configuration management system that constructs configuration objects with dynamically computed property names.
Create a module that exports a createConfig function which accepts a base name prefix and a map of suffixes to values, and returns a configuration object where property names are dynamically computed by concatenating the prefix with each suffix.
The function should:
prefix string as the first parameterconfigMap object as the second parameter, where keys are suffixes and values are configuration valuesprefix + suffix and each value is the corresponding configuration value from the mapAdditionally, create a mergeConfigs function that accepts two configuration objects and merges them into a single object with computed property names based on the original keys. Each merged property should have a key format of merged_<originalKey>.
// Implement createConfig and mergeConfigs functions hereconst { createConfig, mergeConfigs } = require('./config-builder');
describe('createConfig', () => {
test('creates config with multiple computed properties', () => { // @test
const result = createConfig('app', {
'Host': 'localhost',
'Port': 3000,
'Debug': true
});
expect(result).toEqual({
'appHost': 'localhost',
'appPort': 3000,
'appDebug': true
});
});
test('handles empty config map', () => { // @test
const result = createConfig('test', {});
expect(result).toEqual({});
});
});
describe('mergeConfigs', () => {
test('merges two configs with computed keys', () => { // @test
const config1 = { host: 'localhost', port: 3000 };
const config2 = { timeout: 5000, retries: 3 };
const result = mergeConfigs(config1, config2);
expect(result).toHaveProperty('merged_host', 'localhost');
expect(result).toHaveProperty('merged_port', 3000);
expect(result).toHaveProperty('merged_timeout', 5000);
expect(result).toHaveProperty('merged_retries', 3);
});
});Transforms ES2015 computed properties to ES5-compatible code.
Provides Babel transformation capabilities.
Babel preset for compiling modern JavaScript.
The implementation should use ES2015 computed property syntax to create objects with dynamically generated property names. The Babel plugin will transform these computed properties into ES5-compatible code during the build process.
Example usage:
const config = createConfig('db', {
'Name': 'users',
'Collection': 'accounts'
});
// Result: { dbName: 'users', dbCollection: 'accounts' }
const merged = mergeConfigs({ x: 1 }, { y: 2 });
// Result: { merged_x: 1, merged_y: 2 }Install with Tessl CLI
npx tessl i tessl/npm-babel-plugin-transform-es2015-computed-propertiesdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10