# type-analyzer

> Infer types from columns in JSON

Latest version **0.4.0** (published 2022-03-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install type-analyzer
pnpm add type-analyzer
yarn add type-analyzer
bun add type-analyzer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2022-03-24 |
| First published | 2017-08-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 85.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 38 |
| Maintainers | ericsoco, uber, apercu, heshan0131 |

## Links

- npm: https://www.npmjs.com/package/type-analyzer
- Repository: https://github.com/uber-web/type-analyzer
- Homepage: https://github.com/uber-web/type-analyzer#readme
- Issues: https://github.com/uber-web/type-analyzer/issues
- npm.io page: https://npm.io/package/type-analyzer

## Recent versions

- 0.4.0 (latest) — 2022-03-24
- 0.3.0 — 2020-10-06
- 0.2.6 — 2020-10-05
- 0.2.5 — 2020-01-26
- 0.2.4 — 2020-01-08
- 0.2.3 — 2019-10-09
- 0.2.2 — 2019-10-07
- 0.2.1 — 2019-01-03
- 0.1.4 — 2018-04-03
- 0.1.3 — 2018-03-01
- 0.1.2 — 2018-01-18
- 0.1.1 — 2017-12-19
- 0.1.0 — 2017-08-16
- 0.0.0 — 2017-08-11

## README

# type-analyzer

Infer data types from CSV columns.

## Overview

This package provides a single interface for generating the datatype for a given
row-column formatted dataset. We support the following datatypes:

* **DATE**
* **TIME**
* **DATETIME**
* **NUMBER**
* **INT**
* **FLOAT**
* **CURRENCY**
* **PERCENT**
* **STRING**
* **ARRAY**
* **OBJECT**
* **ZIPCODE**
* **BOOLEAN**
* **GEOMETRY**
* **GEOMETRY_FROM_STRING**
* **PAIR_GEOMETRY_FROM_STRING**
* **NONE**

## Installation

    npm install type-analyzer

## Usage

### `Analyzer.computeColMeta(data, rules, options)` (Function)

**Parameters**

-  `data` **Array**  _required_ An array of row object
-  `rules` **Array**  _optional_ An array of custom regex rules
-  `options` **Object**  _optional_ Option object
-  `options.ignoreDataTypes` **Array**  _optional_ Data types to ignore

```js
var Analyzer = require('type-analyzer').Analyzer;
var data = [
    {
        "ST_AsText": "MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))",
        "name": "san_francisco",
        "lat": "37.7749295",
        "lng": "-122.4194155",
        "launch_date": "2010-06-05",
        "added_at": "2010-06-05 12:00"
    },
    {
        "ST_AsText": "MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))",
        "name": "paris",
        "lat": "48.856666",
        "lng": "2.3509871",
        "launch_date": "2011-12-04",
        "added_at": "2010-06-05 12:00"
    },
]
var colMeta = Analyzer.computeColMeta(data);
```
- **`rules`**

You can pass in an array of custom rules. For example. if you want to ensure that a column full of ids represented as numbers is identified as a column of strings. Rules can be matched with either exact `name` of the column, or `regex` used to match names. Note: Analyzer prefers rules using name over regex since better performance.

```js
var Analyzer = require('type-analyzer').Analyzer;

var colMeta = Analyzer.computeColMeta(data, [{name: 'id', dataType: 'STRING'}]);
// or
var colMeta = Analyzer.computeColMeta(data, [{regex: /id/, dataType: 'STRING'}]);
```

- **`options.ignoreDataTypes`**

You can also pass in `ignoreDataTypes` to ignore certain types. This will improve your type checking performance.

```js
var DATA_TYPES = require('type-analyzer').DATA_TYPES;

var colMeta = Analyzer.computeColMeta(arr, [], {ignoredDataTypes: DATA_TYPES.CURRENCY})[0].type,
```

And it will short cut around the usual analysis system and give
you back the column formatted as you'd expect.

### `DATA_TYPES`

You can import all availale types as a constant.


## Update
Breaking changes with v1.0.0: Regex has moved into src, but can more easily be
accessed from the module.exports from the root. As part of a larger clean up
many extraneous util files were removed.

---
_Source: https://npm.io/package/type-analyzer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
