JSPM

values-to-keys

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1135
  • Score
    100M100P100Q105853F
  • License MIT

Replace translation values with keys

Package Exports

  • values-to-keys

Readme

values-to-keys

npm version License: MIT

A lightweight utility to replace translation values with their respective keys, useful for i18n workflows.

Installation

npm install values-to-keys
# or
yarn add values-to-keys
# or
pnpm add values-to-keys

Features

  • 🔍 Replaces string values in translation objects with their dot-notation keys
  • 🌲 Handles deeply nested objects and arrays
  • 🔄 Works with any translation structure
  • 📦 Tiny footprint with zero dependencies
  • 🚀 Supports ESM and CommonJS

Usage

The library exports a function that replaces all string values in an object with their respective keys in dot notation.

Basic Example

import { replace } from 'values-to-keys';
// or: import replace from 'values-to-keys';

const translations = {
  greetings: {
    hello: 'Hello World',
    welcome: 'Welcome to our app',
  },
  errors: {
    notFound: 'Not found',
  },
};

replace(translations);

console.log(translations);
// Output:
// {
//   greetings: {
//     hello: 'greetings.hello',
//     welcome: 'greetings.welcome',
//   },
//   errors: {
//     notFound: 'errors.notFound',
//   },
// }

With JSON Translation Files

import translationFile from './locales/en.json';
import { replace } from 'values-to-keys';

replace(translationFile);
// Now translationFile has all string values replaced with their keys

Use Cases

This library is designed to work with translation-keys-generator and similar i18n workflows where you need to:

  1. Generate translation keys from values
  2. Use the keys in your application code
  3. Maintain a clean separation between keys and values

API

replaceTranslationsWithKeys(item, namespace?)

The main function that replaces string values with their keys.

  • item: The object containing translation strings
  • namespace: (Optional) Used internally for recursion, don't pass this in the initial call

Returns the modified object (same reference).

replace

An alias for replaceTranslationsWithKeys for convenience.

License

MIT © Mohamed Elhadi