CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-es2015-computed-properties

Babel plugin that compiles ES2015 computed properties to ES5-compatible code

88

1.07x
Overview
Eval results
Files

task.mdevals/scenario-2/

Dynamic Configuration Builder

Build a configuration management system that constructs configuration objects with dynamically computed property names.

Requirements

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:

  • Accept a prefix string as the first parameter
  • Accept a configMap object as the second parameter, where keys are suffixes and values are configuration values
  • Return an object where each property name is prefix + suffix and each value is the corresponding configuration value from the map
  • Support multiple computed properties in a single configuration object
  • Maintain the order of properties as they appear in the input map

Additionally, 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>.

Implementation Files

src/config-builder.js { .file }

// Implement createConfig and mergeConfigs functions here

src/config-builder.test.js { .file }

const { 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);
  });
});

Dependencies { .dependencies }

babel-plugin-transform-es2015-computed-properties { .dependency }

Transforms ES2015 computed properties to ES5-compatible code.

@babel/core { .dependency }

Provides Babel transformation capabilities.

@babel/preset-env { .dependency }

Babel preset for compiling modern JavaScript.

Expected Behavior

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-properties

tile.json