Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm's 300,000 packages pre-installed, including mirrorkey with all npm packages installed. Try it out:

var mirrorkey = require("mirrorkey")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

mirrorkey v1.2.0

Simple utility to create a object literal which has the same values as the key names.

MirrorKey

A small utility that takes a object literal and returns a new object literal whose values are the same as keys.

The idea came from react/utils/keyMirror but I added transforms which allows you to transform the values. There are 5 available transforms: none, camel-case, lower-case, dashed & lower-dashed. You pass the transform in as the second argument (default is none),

This utility is useful for creating maps of constants, as it helps you to avoid having to type the same thing twice.

Examples

var myConstants = {
    MY_FOO: null,
    MY_BAR: null
};

mirrorKey(myConstants);
/*
{
    MY_FOO: 'MY_FOO',
    MY_BAR: 'MY_BAR'
}
*/

mirrorKey(myConstants, 'lower-dashed');
/*
{
    MY_FOO: 'my-foo',
    MY_BAR: 'my-bar'
}
*/

You can also pass in an array instead of an object:

mirrorKey(['MY_FOO', 'MY_BAR']);
/*
{
    MY_FOO: 'MY_FOO',
    MY_BAR: 'MY_BAR'
}
*/

// you can also use transforms as above if you pass in an array

Runtime support

Works in node.js and in the browser.

I haven't yet tested this in every browser, but it should work as I haven't used any >ES5 functions.

Installation

Node, Browserify & Webpack: npm install mirrorkey

Bower: bower install mirrorkey

Other: you can also just grap mirrorkey_browser.js or mirrorkey_browser.min.js and include them in your project manually. A global mirrorkey will be available on the window object.

Developing

Clone this repository. Source code is in mirrorkey.js and the tests are in spec/. To run the tests you will need to install jasmine-node with npm install jasmine-node -g. After which you can run npm test to run the tests.

If you wish to build a browser version then run npm install and then grunt which will generate the browser versions for you.

License

See LICENSE file.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free