Skip to content

ReasonX7/mirror-keys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mirror-keys

A flexible way to mirror object keys.

Installation

npm i @reasonx7/mirror-keys

Usage

Importing

Node modules:

const mk = require('@reasonx7/mirror-keys');

ES6 modules:

import mk from '@reasonx7/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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published