1.0.1 • Published 6 years ago

@reasonx7/mirror-keys v1.0.1

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

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.