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

@robbyawaldi/json-to-interface

v1.0.2

Published

The **`jsonToInterface`** function generates TypeScript interface from a given JSON object.

Downloads

2

Readme

JSON To Interface

The jsonToInterface function generates TypeScript interface from a given JSON object.

To install dependencies:

bun install

To test:

bun test

Parameters

  • jsonObj : a JSON object
  • interfaceName (optional) : a string representing the name of the interface. If not provided, the interface will be anonymous.

Return

  • A string containing the TypeScript interface.

Example

const exampleJson = {
  name: "John",
  age: 30,
  address: {
    street: "123 Main St",
    city: "New York",
    state: "NY",
    zipcode: 10001,
  },
  hobbies: ["reading", "running"],
};

const interfaceString = jsonToInterface(exampleJson, "Person");
console.log(interfaceString);

Output

export interface Person {
  name: string;
  age: number;
  address: {
    street: string;
    city: string;
    state: string;
    zipcode: number;
  };
  hobbies: string[];
}

Description

The function jsonToInterface takes in a JSON object and an optional interface name. The function then recursively traverses the JSON object and generates a TypeScript interface based on the keys and values of the JSON object.

The function handles different types of values in the JSON object as follows:

  • If the value is null, the generated interface property will have the type null.
  • If the value is an object and not an array, the function recursively generates an interface for the object and sets the property type to the generated interface.
  • If the value is an array, the function determines the type of the array elements.
  • If the array elements are objects, the function recursively generates an interface for the array elements and sets the property type to Array<generatedInterface>.

For all other types of values, the generated interface property will have the type typeof value. The function returns a string containing the generated TypeScript interface. If an interface name is provided, the interface will be named using the provided name. Otherwise, an anonymous interface will be generated. The generated interface string includes the export keyword to make it available for use in other modules.