|
1 |
| -# Typescript Node Package Repository Template |
| 1 | +# 🤔 JSON Infer Type |
2 | 2 |
|
3 |
| -> Create a new repo from this template to get started creating a Typescript npm package |
| 3 | +> Infer the types of JSON documents & values, with a large set of formats for strings |
4 | 4 |
|
5 |
| -<!--  --> |
6 |
| -<!--  --> |
7 |
| -<!-- [](https://npmjs.com/@jsonhero/ts-node-package-template) --> |
8 |
| -<!-- [](https://packagephobia.com/result?p=@jsonhero/ts-node-package-template) --> |
| 5 | + |
| 6 | + |
| 7 | +[](https://npmjs.com/@jsonhero/json-infer-types) |
| 8 | +[](https://packagephobia.com/result?p=@jsonhero/json-infer-types) |
9 | 9 |
|
10 |
| -## Features |
| 10 | +## 🚀 Features |
11 | 11 |
|
12 | 12 | - Written in typescript
|
13 |
| -- Github workflows for running tests and publishing package to NPM on Github release |
14 |
| -- ts-node and ts-jest integration |
15 |
| -- Generate coverage badges |
| 13 | +- Infers types of values inside objects and arrays |
| 14 | +- Includes a large set of formats for strings |
| 15 | + - Dates and times (and timestamps) |
| 16 | + - Email addresses |
| 17 | + - Currencies |
| 18 | + - Countries |
| 19 | + - Top-Level Domains |
| 20 | + - IP Addresses |
| 21 | + - Languages |
| 22 | + - Phone Numbers |
| 23 | + - URIs |
| 24 | + - UUIDs |
| 25 | + - Hostnames |
16 | 26 |
|
17 |
| -## Usage |
| 27 | +## 💻 Usage |
18 | 28 |
|
19 |
| -Create a new repository from this template on Github with the [following instructions](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-repository-from-a-template) |
| 29 | +Install JSON Infer Types |
| 30 | + |
| 31 | +```bash |
| 32 | +$ npm install --save @jsonhero/json-infer-types |
| 33 | +``` |
| 34 | + |
| 35 | +`inferType` takes any JSON value and returns a `JSONValueType` object: |
| 36 | + |
| 37 | +```js |
| 38 | +const { inferType } = require("@jsonhero/json-infer-types"); |
| 39 | + |
| 40 | +inferType(123); // => { name: "int" } |
| 41 | +``` |
| 42 | + |
| 43 | +The following basic types are supported: |
| 44 | + |
| 45 | +```js |
| 46 | +inferType(null); // => { name: "null" } |
| 47 | +inferType(true); // => { name: "bool" } |
| 48 | +inferType(123); // => { name: "int" } |
| 49 | +inferType(123.456); // => { name: "float" } |
| 50 | +inferType("hello world"); // => { name: "string" } |
| 51 | +``` |
| 52 | + |
| 53 | +Objects have an additional `properties` property that infers its value types |
| 54 | + |
| 55 | +```js |
| 56 | +inferType({ foo: "bar" }); |
| 57 | +``` |
| 58 | + |
| 59 | +Will result in |
| 60 | + |
| 61 | +```json |
| 62 | +{ "name": "object", "properties": { "foo": { "name": "string" } } } |
| 63 | +``` |
0 commit comments