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

mimic-tree

v0.1.0

Published

mimic-tree is a Node.js library for creating dynamic data trees that mimic the structure of source data. It reproduces the original folder paths and file structures like JSON or YAML with different values, maintaining the integrity of the original structu

Downloads

2

Readme

mimic-tree

mimic-tree is a Node.js package designed to convert values in JSON and YAML files within a specified directory, outputting the transformed content to a new directory while preserving the original structure. This can be particularly useful, for instance, when you want to batch translate language resource data.

Features

  • Preserves hierarchical directory structure during conversion and output
  • Custom function-based value conversion for JSON and YAML files
  • Can be combined with translation APIs for batch translation capabilities

Installation

npm install mimic-tree

Usage

Import the mimicTree function and use it with the necessary options as shown below:

import { mimicTree } from 'mimic-tree'

async function main() {
  await mimicTree({
    src: 'basic',
    cwd: __dirname,
    dist: 'basic-result',
    extensions: ['json', 'yaml'],
    convert: (v: string | number | null) => v /* ... convert value ... */
  })
}

Options

  • src (required): Specifies the source root path.
  • cwd: Specifies the current working directory. Defaults to process.cwd().
  • dist (required): Specifies the destination path.
  • extensions: Specifies the file extensions to target. Defaults to ['json']. If you want to target both JSON and YAML files, specify ['json', 'yaml'] and run npm install yarn.
  • convert (required): A function to convert the value. This function is invoked for each value, and it returns the new value.

Use Cases

If your language resource data is managed in JSON or YAML formats, you can use this package to apply translation APIs or custom functions and output the translated data into a new directory.

For instance, when combined with a translation API like Google Translate, you could set it up for batch translations as shown:

import { mimicTree } from 'mimic-tree'
import { resolve } from 'path'
import { translate } from 'your-translate-api'

/**
 * @params fromLocale (e.g. 'en')
 * @params toLocale (e.g. 'ja')
 */
export async function translate(fromLocale: string, toLocale: string) {
  await mimicTree({
    src: fromLocale,
    cwd: resolve(__dirname, 'locales'),
    dist: toLocale,
    extensions: ['json', 'yaml'],
    convert: async (v) =>
      typeof v === 'string' ? await translate(v, fromLocale, toLocale) : v
  })
}

License

MIT

Contributing

see CONTRIBUTING.md