-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype-helpers.js
230 lines (222 loc) · 6.11 KB
/
type-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import {
isBoolish,
isCurrency,
isDateString,
isEmailShaped,
isFloatish,
isNullish,
isNumeric,
isObjectId,
isTimestamp,
isUuid
} from './utils/type-detectors.js'
const hasLeadingZero = /^0+/
/**
* Returns an array of TypeName.
* @param {any} value - input data
* @returns {string[]}
*/
function detectTypes (value, strictMatching = true) {
const excludedTypes = []
const matchedTypes = prioritizedTypes.reduce((types, typeHelper) => {
if (typeHelper.check(value)) {
if (typeHelper.supercedes) excludedTypes.push(...typeHelper.supercedes)
types.push(typeHelper.type)
}
return types
}, [])
return !strictMatching ? matchedTypes : matchedTypes.filter((type) => excludedTypes.indexOf(type) === -1)
}
/**
* MetaChecks are used to analyze the intermediate results, after the Basic (discreet) type checks are complete.
* They have access to all the data points before it is finally processed.
*/
const MetaChecks = {
TYPE_ENUM: {
type: 'enum',
matchBasicTypes: ['String', 'Number'],
check: (typeInfo, { rowCount, uniques }, { enumAbsoluteLimit, enumPercentThreshold }) => {
if (!uniques || uniques.length === 0) return typeInfo
// TODO: calculate uniqueness using ALL uniques combined from ALL types, this only sees consistently typed data
// const uniqueness = rowCount / uniques.length
const relativeEnumLimit = Math.min(parseInt(String(rowCount * enumPercentThreshold), 10), enumAbsoluteLimit)
if (uniques.length > relativeEnumLimit) return typeInfo
// const enumLimit = uniqueness < enumAbsoluteLimit && relativeEnumLimit < enumAbsoluteLimit
// ? enumAbsoluteLimit
// : relativeEnumLimit
return { enum: uniques, ...typeInfo }
// TODO: calculate entropy using a sum of all non-null detected types, not just typeCount
}
},
TYPE_NULLABLE: {
type: 'nullable',
// matchBasicTypes: ['String', 'Number'],
check: (typeInfo, { rowCount, uniques }, { nullableRowsThreshold }) => {
if (!uniques || uniques.length === 0) return typeInfo
const { types } = typeInfo
let nullishTypeCount = 0
if (types.Null) {
nullishTypeCount += types.Null.count
}
// if (types.Unknown) {
// nullishTypeCount += types.Unknown.count
// }
const nullLimit = rowCount * nullableRowsThreshold
const isNotNullable = nullishTypeCount <= nullLimit
// TODO: Look into specifically checking 'Null' or 'Unknown' type stats
return { nullable: !isNotNullable, ...typeInfo }
// TODO: calculate entropy using a sum of all non-null detected types, not just typeCount
}
},
TYPE_UNIQUE: {
type: 'unique',
// matchBasicTypes: ['String', 'Number'],
check: (typeInfo, { rowCount, uniques }, { uniqueRowsThreshold }) => {
if (!uniques || uniques.length === 0) return typeInfo
// const uniqueness = rowCount / uniques.length
const isUnique = uniques.length === (rowCount * uniqueRowsThreshold)
// TODO: Look into specifically checking 'Null' or 'Unknown' type stats
return { unique: isUnique, ...typeInfo }
// return {unique: uniqueness >= uniqueRowsThreshold, ...typeInfo}
// TODO: calculate entropy using a sum of all non-null detected types, not just typeCount
}
}
}
// Basic Type Filters - rudimentary data sniffing used to tally up "votes" for a given field
/**
* Detect ambiguous field type.
* Will not affect weighted field analysis.
*/
const TYPE_UNKNOWN = {
type: 'Unknown',
check: (value) => value === undefined || value === 'undefined'
}
const TYPE_OBJECT_ID = {
type: 'ObjectId',
supercedes: ['String'],
check: isObjectId
}
const TYPE_UUID = {
type: 'UUID',
supercedes: ['String'],
check: isUuid
}
const TYPE_BOOLEAN = {
type: 'Boolean',
supercedes: ['String'],
check: isBoolish
}
const TYPE_DATE = {
type: 'Date',
supercedes: ['String'],
check: isDateString
}
const TYPE_TIMESTAMP = {
type: 'Timestamp',
supercedes: ['String', 'Number'],
check: isTimestamp
}
const TYPE_CURRENCY = {
type: 'Currency',
supercedes: ['String', 'Number'],
check: isCurrency
}
const TYPE_FLOAT = {
type: 'Float',
supercedes: ['String', 'Number'],
check: isFloatish
}
const TYPE_NUMBER = {
type: 'Number',
check: (value) => {
if (hasLeadingZero.test(String(value))) return false
return !!(value !== null && !Array.isArray(value) && (Number.isInteger(value) || isNumeric(value)))
}
}
const TYPE_EMAIL = {
type: 'Email',
supercedes: ['String'],
check: isEmailShaped
}
const TYPE_STRING = {
type: 'String',
check: (value) => typeof value === 'string' // && value.length >= 1
}
const TYPE_ARRAY = {
type: 'Array',
check: (value) => {
return Array.isArray(value)
}
}
const TYPE_OBJECT = {
type: 'Object',
check: (value) => {
return !Array.isArray(value) && value != null && typeof value === 'object'
}
}
const TYPE_NULL = {
type: 'Null',
check: isNullish
}
const prioritizedTypes = [
TYPE_UNKNOWN,
TYPE_OBJECT_ID,
TYPE_UUID,
TYPE_BOOLEAN,
TYPE_DATE,
TYPE_TIMESTAMP,
TYPE_CURRENCY,
TYPE_FLOAT,
TYPE_NUMBER,
TYPE_NULL,
TYPE_EMAIL,
TYPE_STRING,
TYPE_ARRAY,
TYPE_OBJECT
]
/**
* Type Rank Map: Use to sort Lowest to Highest
*/
const typeRankings = {
[TYPE_UNKNOWN.type]: -1,
[TYPE_OBJECT_ID.type]: 1,
[TYPE_UUID.type]: 2,
[TYPE_BOOLEAN.type]: 3,
[TYPE_DATE.type]: 4,
[TYPE_TIMESTAMP.type]: 5,
[TYPE_CURRENCY.type]: 6,
[TYPE_FLOAT.type]: 7,
[TYPE_NUMBER.type]: 8,
[TYPE_NULL.type]: 10,
[TYPE_EMAIL.type]: 11,
[TYPE_STRING.type]: 12,
[TYPE_ARRAY.type]: 13,
[TYPE_OBJECT.type]: 14
}
export {
typeRankings,
prioritizedTypes,
detectTypes,
MetaChecks,
TYPE_UNKNOWN,
TYPE_OBJECT_ID,
TYPE_UUID,
TYPE_BOOLEAN,
TYPE_DATE,
TYPE_TIMESTAMP,
TYPE_CURRENCY,
TYPE_FLOAT,
TYPE_NUMBER,
TYPE_NULL,
TYPE_EMAIL,
TYPE_STRING,
TYPE_ARRAY,
TYPE_OBJECT
}
// const TYPE_ENUM = {
// type: "String",
// check: (value, fieldInfo, schemaInfo) => {
// // Threshold set to 5% - 5 (or fewer) out of 100 unique strings should enable 'enum' mode
// if (schemaInfo.inputRowCount < 100) return false; // disabled if set too small
// }
// };