Giter Site home page Giter Site logo

dcflw / core-types-literals Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 45 KB

A core-types-compliant reader for literal JSON, YAML, and TOML files

Home Page: https://www.npmjs.com/package/core-types-literals

License: ISC License

JavaScript 3.66% TypeScript 96.34%

core-types-literals's Introduction

core-types-literals

A core-types-compliant reader for literal JSON, YAML, and TOML files. This is useful if you want to generate a TypeScript interface for your literal JSON file, like a locale object using Typeconv.

Usage

Install this package and typeconv:

npm add -D core-types-literals typeconv

Then create a JS file like this:

import { makeConverter, getTypeScriptWriter } from "typeconv";
import { convertYamlToCoreTypes } from "core-types-literals";

/** @returns {import("typeconv").Reader} */
export function getYamlLiteralReader(name) {
  return {
    kind: "yaml",
    read: (x) => convertYamlToCoreTypes(x, name),
  };
}

const { convert } = makeConverter(
  getYamlLiteralReader("MyInterfaceName"),
  getTypeScriptWriter({ warn: console.warn }),
);

const inputYaml = { data: "hello: World" };
const { data: outputTs } = await convert(inputYaml);
console.log(outputTs);
// export interface MyInterfaceName {
//   hello: "World";
// }

Or combine with reading from/writing to files:

// same stuff as above…
import { readFile, writeFile } from "fs/promises";

const inputFileUrl = new URL("../locales/en.yaml", import.meta.url);
const outputTypingsUrl = new URL("./en.i18n.d.ts", import.meta.url);
readFile(i18nFileUrl, "utf8")
  .then((yamlContents) => convert({ data: yamlContents }))
  .then(({ data: tsInterface }) => writeFile(typingsUrl, tsInterface));

License

ISC

core-types-literals's People

Contributors

illright avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.