@reasonx7/mirror-keys

1.0.1 • Public • Published

mirror-keys

A flexible way to mirror object keys.

Installation

npm i mirror-keys

Usage

Importing

Node modules:

const mk = require('mirror-keys');

ES6 modules:

import mk from 'mirror-keys';

Scenarios

No options

Input:

const output = mk({
  a: 'a',
  b: null,
  nested: {
    x: 'x',
    y: null,
  },
});

Output:

{
  a: 'a',
  b: 'b',
  nested: {
    x: 'x',
    y: 'nested.y',
  },
}

Prefix

Input:

const output = mk({
  a: 'a',
  b: null,
  nested: {
    x: 'x',
    y: null,
  },
}, 'PREFIX::');

Output:

{
  a: 'a',
  b: 'PREFIX::b',
  nested: {
    x: 'x',
    y: 'PREFIX::nested.y',
  },
}

Custom

Input:

const output = mk({
  a: 'a',
  b: null,
  nested: {
    x: 'x',
    y: null,
  },
}, mk.flow(mk.joinPath, key => key.replace('.', '-')));

Output:

{
  a: 'a',
  b: 'b',
  nested: {
    x: 'x',
    y: 'nested-y',
  },
}

Available modifiers:

  • mk.flow(...funcs): string - combines modifiers into one flow.
  • mk.upper(key: string): string - converts key to UPPERCASE.
  • mk.prefix(prefix: string): function - creates a function that adds a prefix to the key.
  • mk.snake(prefix: string): string - converts key to snake_case.
  • mk.joinPath(key: string, path: string): string - adds a path to the nested key.

Readme

Keywords

Package Sidebar

Install

npm i @reasonx7/mirror-keys

Weekly Downloads

0

Version

1.0.1

License

ISC

Unpacked Size

5.37 kB

Total Files

4

Last publish

Collaborators

  • reasonx7