npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-to-enum

v1.3.0

Published

Creates TypeScript Enums based on JSON files

Downloads

667

Readme

json-to-enum

CLI tool for TypeScript Enum files generation from JSON files

The Goal

The main goal is to have a convenient tool for working with JSON translation files in TypeScript projects, which are often used by various i18n libraries, for example i18next.

But some other use case may appear, therefore, the tool provides some flexibility in its configuration.

Installation

Local

npm install --save-dev json-to-enum

or

yarn add --dev json-to-enum

Global

yarn global add json-to-enum

or

npm install --global json-to-enum

Usage

You can use the tool by passing all settings through arguments:

json-to-enum --watch --input locales/**/*.json

Example

For instance, we have some JSON file with the next content:

{
  "nav": {
    "home": "Home",
    "about": "About",
    "contacts": "Contacts"
  }
}

By calling json-to-enum --input some-file.json we will get the next Enum file:

enum SomeFile {
  NavHome = 'nav.home',
  NavAbout = 'nav.about',
  NavContacts = 'nav.contacts',
}

export default SomeFile

You can make some customization of the output using configuration.

By calling json-to-enum --input some-file.json --enumValue value values of the resulting Enum will be such as in the source JSON file:

enum SomeFile {
  NavHome = 'Home',
  NavAbout = 'About',
  NavContacts = 'Contacts',
}

export default SomeFile

Configuration file

Also you can create configuration file for more detailed configuration:

json-to-enum init

This command will create configuration file with the next content:

const upperFirst = require('lodash/upperFirst');
const camelCase = require('lodash/camelCase');

module.exports = {
  input: 'src/**/*.json',
  outputFolderCallback: path => path.dir,
  outputFolder: '.', // will be ignored because outputFolderCallback is specified
  enumNameCallback: path => upperFirst(camelCase(path.base)),
  enumFilenameCase: 'kebab',
  enumFilenameEnding: '.ts',
  enumValue: 'path',
  enumValueQuotes: "'",
  enumTabs: false,
  enumSpaces: 2,
  enumExportDefault: true,
  jsonKeySeparator: '.',
  jsonFlattenArray: false,
};

Watch Mode

If you pass --watch, -w argument, the tool will watch changes of source files.