Skip to content

Commit 44ab68c

Browse files
committed
Infer string formats
1 parent 2cec336 commit 44ab68c

24 files changed

+1935
-31
lines changed

.vscode/launch.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Debug Jest All Tests",
9+
"type": "node",
10+
"request": "launch",
11+
"runtimeArgs": [
12+
"--inspect-brk",
13+
"${workspaceRoot}/node_modules/.bin/jest",
14+
"--runInBand"
15+
],
16+
"console": "integratedTerminal",
17+
"internalConsoleOptions": "neverOpen"
18+
},
19+
{
20+
"name": "Debug Jest Test File",
21+
"type": "node",
22+
"request": "launch",
23+
"runtimeArgs": [
24+
"--inspect-brk",
25+
"${workspaceRoot}/node_modules/.bin/jest",
26+
"--runInBand"
27+
],
28+
"args": ["${fileBasename}", "--no-cache"],
29+
"console": "integratedTerminal",
30+
"internalConsoleOptions": "neverOpen"
31+
}
32+
]
33+
}

README.md

+56-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,63 @@
1-
# Typescript Node Package Repository Template
1+
# 🤔 JSON Infer Type
22

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
44
5-
<!-- ![Coverage lines](./badges/badge-lines.svg) -->
6-
<!-- ![Tests](https://github.com/jsonhero-io/ts-node-package-template/actions/workflows/test.yml/badge.svg?branch=main) -->
7-
<!-- [![Downloads](https://img.shields.io/npm/dm/%40jsonhero%2Fts-node-package-template.svg)](https://npmjs.com/@jsonhero/ts-node-package-template) -->
8-
<!-- [![Install size](https://packagephobia.com/badge?p=%40jsonhero%2Fts-node-package-template)](https://packagephobia.com/result?p=@jsonhero/ts-node-package-template) -->
5+
![Coverage lines](./badges/badge-lines.svg)
6+
![Tests](https://github.com/jsonhero-io/json-infer-types/actions/workflows/test.yml/badge.svg?branch=main)
7+
[![Downloads](https://img.shields.io/npm/dm/%40jsonhero%2Fjson-infer-types.svg)](https://npmjs.com/@jsonhero/json-infer-types)
8+
[![Install size](https://packagephobia.com/badge?p=%40jsonhero%2Fjson-infer-types)](https://packagephobia.com/result?p=@jsonhero/json-infer-types)
99

10-
## Features
10+
## 🚀 Features
1111

1212
- 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
1626

17-
## Usage
27+
## 💻 Usage
1828

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

Comments
 (0)