Skip to content

Latest commit

 

History

History
115 lines (98 loc) · 2.16 KB

README.md

File metadata and controls

115 lines (98 loc) · 2.16 KB

Vue logo

Rekey

A simple and performant zero dependency library that allows you to modify your object structure using references.

Tests

Statements

Installation

npm i @eramux/rekey

Usage

First we will need to create an object which will be passed to the rekey funcitons

// Define a sample object
let data = {
  key1: "some string",
  settings: {
    name: "John",
    active: true
  },
  users: [
    {
      name: "Marie"
    },
    {
      name: "Steven",
      type: "Human"
    }
  ],
  items: [
    {
      name: "abs"
    },
    {
      name: "pvc",
      supported: false
    }
  ]
}

Rename key:

import { renameKey } from "@eramux/rekey"

// Rename the settings -> name key to 'username'
renameKey(data, "settings.name", "username")
// -> {
//      ...
//      settings: {
//        username: "John"
//        active: true
//      }
//      ...
//    }

// It will automatically rename keys in object arrays
renameKey(data, "items.name", "material")
// -> {
//      ...
//      items: [
//        {
//          material: "abs"
//        }
//        ...
//     ]
//     ...
//    }

// It only changes keys that exist
renameKey(data, "users.type", "species")
// -> {
//      ...
//      users: [
//        {
//          name: "Marie"
//        },
//        {
//          name: "Steven",
//          species: "Human"
//        }
//     ]
//     ...
//    }

Rename key:

// Delete a key nested deeply inside of the object or arrays
deleteKey(data, "users.name")
// -> {
//      ...
//      users: [
//        {},
//        {
//          species: "Human"
//        }
//     ]
//     ...
//    }

Since rekey does not copy the object, you won't have any unexpected memory spikes. It works by only modifying the reference recursively.

Contributions

Any contributions are welcome! Please make sure to first file an issue so we can discuss the problem at hand and you don't create a PR that doesn't get pulled.