types.md 1.49 KB
Newer Older
1
2
3
4
5
6
# g1.types

`g1.types(data)` returns the column data types. For example:

```js
var data = [
7
  {a: 1, b: 1.1, c: 'c', d: '2014-04-04', e: true, f: new Date()},
8
9
  {a: 2, b: 2},
]
10
11
12
g.types(data)                   // {"a":"number","b":"number","c":"string","d":"string","e":"boolean","f":"date"}
g1.types(data, {convert: true})   // {"a":"number","b":"number","c":"string","d":"date","e":"boolean","f":"date"}

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
```

## g1.types options

`types()` accepts 2 parameters:

- `data`: a list of objects
- `options`: a dictionary that may contain these keys:
  - `convert`: converts values to the right type. For example, "1" is converted to 1. default: `false`
  - `limit`: number of rows to evaluate. default: 1000
  - `ignore`: list of values that should be ignored. default: `[null, undefined]`

Rules:

- Evaluate up to `limit` rows
- Ignore values that are keys in the `ignore` option. Only consider the rest
- If `convert` is `false`, then for each column:
  - If all values are Date objects -> `date`
  - Else if all values are numbers -> `number`
  - Else if all values are strings -> `string`
  - Else if all values are bools -> `boolean`
  - Else if there are no values or is undefined or null -> `null`
  - Else -> `mixed`
- Else if `convert` is `true`, then for each column:
  - If all values can be converted to Date -> `date`
  - Else if all values can be converted to numbers -> `number`
  - Else if all values are bools -> `boolean`
  - Else if there are no values or is undefined or null  -> `null`
  - Else -> `string`