Skip to content

Commit 61c1223

Browse files
authored
feat!: update snake_case schema properties to camelCase (#190)
1 parent accee3d commit 61c1223

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ below accordingly).
9292
"count": 4, // 4 documents counted with _id
9393
"type": "Number", // the type of _id is `Number`
9494
"probability": 1, // all documents had an _id field
95-
"has_duplicates": false, // therefore no duplicates
95+
"hasDuplicates": false, // therefore no duplicates
9696
"types": [ // an array of Type objects, @see `./lib/types/`
9797
{
9898
"name": "Number", // name of the type
@@ -118,7 +118,7 @@ below accordingly).
118118
"Number",
119119
"Undefined" // for convenience, we treat Undefined as its own type
120120
],
121-
"has_duplicates": false, // there were no duplicate values
121+
"hasDuplicates": false, // there were no duplicate values
122122
"types": [
123123
{
124124
"name": "Boolean",

src/schema-analyzer.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
/* eslint-disable camelcase */
32
import Reservoir from 'reservoir';
43
import type {
54
Document,
@@ -52,8 +51,8 @@ export type BaseSchemaType = {
5251
bsonType: string;
5352

5453
// As `values` is from a sample reservoir this isn't a true check for duplicates/uniqueness.
55-
// We cannot compute `unique` and `has_duplicates` when `storeValues` is false.
56-
has_duplicates?: boolean;
54+
// We cannot compute `unique` and `hasDuplicates` when `storeValues` is false.
55+
hasDuplicates?: boolean;
5756
unique?: number;
5857
}
5958

@@ -69,8 +68,8 @@ export type PrimitiveSchemaType = BaseSchemaType & {
6968
export type ArraySchemaType = BaseSchemaType & {
7069
name: 'Array';
7170
lengths: number[];
72-
average_length: number;
73-
total_count: number;
71+
averageLength: number;
72+
totalCount: number;
7473
// eslint-disable-next-line no-use-before-define
7574
types: SchemaType[];
7675
}
@@ -91,7 +90,7 @@ export type SchemaField = {
9190
path: string[];
9291
type: string | string[];
9392
probability: number;
94-
has_duplicates: boolean;
93+
hasDuplicates: boolean;
9594
types: SchemaType[];
9695
};
9796

@@ -259,26 +258,26 @@ function computeUniqueForType(type: SchemaAnalysisType) {
259258

260259
/**
261260
* Final pass through the result to add missing information:
262-
* - Compute `probability`, `unique`, `has_duplicates` and
263-
* `average_length` fields.
261+
* - Compute `probability`, `unique`, `hasDuplicates` and
262+
* `averageLength` fields.
264263
* - Add `Undefined` pseudo-types.
265264
* - Collapse `type` arrays to single string if length 1.
266265
* - Turn fields and types objects into arrays to conform with original
267266
* schema parser.
268267
*/
269268
function finalizeSchema(schemaAnalysis: SchemaAnalysisRoot): SchemaField[] {
270269
function finalizeArrayFieldProperties(type: SchemaAnalysisArrayType) {
271-
const total_count = Object.values(type.types)
270+
const totalCount = Object.values(type.types)
272271
.map((v: any) => v.count)
273272
.reduce((p, c) => p + c, 0);
274273

275-
const types = finalizeSchemaFieldTypes(type.types, total_count);
274+
const types = finalizeSchemaFieldTypes(type.types, totalCount);
276275

277276
return {
278277
types,
279-
total_count,
278+
totalCount,
280279
lengths: type.lengths,
281-
average_length: total_count / type.lengths.length
280+
averageLength: totalCount / type.lengths.length
282281
};
283282
}
284283

@@ -292,7 +291,7 @@ function finalizeSchema(schemaAnalysis: SchemaAnalysisRoot): SchemaField[] {
292291
count: type.count,
293292
probability: type.count / parentCount,
294293
unique,
295-
has_duplicates: computeHasDuplicatesForType(type, unique),
294+
hasDuplicates: computeHasDuplicatesForType(type, unique),
296295
values: isNullType(type) ? undefined : type.values,
297296
bsonType: type.bsonType, // Note: `Object` is replaced with `Document`.
298297
...(isArrayType(type) ? finalizeArrayFieldProperties(type) : {}),
@@ -312,7 +311,7 @@ function finalizeSchema(schemaAnalysis: SchemaAnalysisRoot): SchemaField[] {
312311
name: 'Undefined',
313312
bsonType: 'Undefined',
314313
unique: undefinedCount > 1 ? 0 : 1,
315-
has_duplicates: undefinedCount > 1,
314+
hasDuplicates: undefinedCount > 1,
316315
path: field.path,
317316
count: undefinedCount,
318317
probability: undefinedCount / parentCount
@@ -325,7 +324,7 @@ function finalizeSchema(schemaAnalysis: SchemaAnalysisRoot): SchemaField[] {
325324
count: field.count,
326325
type: fieldTypes.length === 1 ? fieldTypes[0].name : fieldTypes.map((v: SchemaType) => v.name), // Or one value or array.
327326
probability: field.count / parentCount,
328-
has_duplicates: !!fieldTypes.find((v: SchemaType) => v.has_duplicates),
327+
hasDuplicates: !!fieldTypes.find((v: SchemaType) => v.hasDuplicates),
329328
types: fieldTypes
330329
};
331330
}).sort(fieldComparator);

test/array-object-types.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('arrays and objects as type (INT-203 restructuring)', function() {
6969
});
7070

7171
it('should return the total count of all containing values', function() {
72-
assert.equal((arr as ArraySchemaType).total_count, 8);
72+
assert.equal((arr as ArraySchemaType).totalCount, 8);
7373
});
7474

7575
it('should return the type distribution inside an array', function() {

test/basic-embedded-array.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('basic embedded array', function() {
3131
});
3232

3333
it('should have an average length of 1.5 for followingIds', function() {
34-
assert.equal(followingIds.average_length, 1.5);
34+
assert.equal(followingIds.averageLength, 1.5);
3535
});
3636

3737
it('should have a sum of probability for followingIds of 1', function() {

test/basic-unique.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import bson from 'bson';
44
import getSchema from '../src';
55
import type { Schema, PrimitiveSchemaType } from '../src/schema-analyzer';
66

7-
describe('has_duplicates', function() {
7+
describe('hasDuplicates', function() {
88
const docs: {
99
num: number;
1010
str: string;
@@ -22,7 +22,7 @@ describe('has_duplicates', function() {
2222
});
2323

2424
it('should not have duplicates', function() {
25-
assert.equal(schema.fields.find(v => v.name === 'num')?.has_duplicates, false);
25+
assert.equal(schema.fields.find(v => v.name === 'num')?.hasDuplicates, false);
2626
});
2727

2828
it('should have 10000 number values for the `num` field', function() {
@@ -76,7 +76,7 @@ describe('unique', function() {
7676
});
7777

7878
it('should not have duplicates for `_id`', function() {
79-
assert.equal(schema.fields.find(v => v.name === '_id')?.has_duplicates, false);
79+
assert.equal(schema.fields.find(v => v.name === '_id')?.hasDuplicates, false);
8080
});
8181

8282
it('should have count of 2 for `registered`', function() {
@@ -104,10 +104,10 @@ describe('unique', function() {
104104
});
105105

106106
it('should not have duplicate values for b', function() {
107-
assert.equal(schema.fields.find(v => v.name === 'b')?.has_duplicates, false);
107+
assert.equal(schema.fields.find(v => v.name === 'b')?.hasDuplicates, false);
108108
});
109109

110110
it('should have duplicates for `registered`', function() {
111-
assert.equal(schema.fields.find(v => v.name === 'registered')?.has_duplicates, true);
111+
assert.equal(schema.fields.find(v => v.name === 'registered')?.hasDuplicates, true);
112112
});
113113
});

0 commit comments

Comments
 (0)