Skip to content

Commit

Permalink
Migrate to prettier (closes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
dokmic committed Jan 24, 2020
1 parent 5e6744a commit 6b36225
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 152 deletions.
9 changes: 6 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
"airbnb-base",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"tsconfigRootDir": "."
},
"plugins": [
"@typescript-eslint"
"@typescript-eslint",
"prettier"
],
"rules": {
"@typescript-eslint/camelcase": ["error", { "properties": "never", "ignoreDestructuring": true }],
Expand All @@ -22,7 +24,8 @@
"import/prefer-default-export": "off",
"max-len": ["error", { "code": 120 }],
"no-param-reassign": "off",
"no-shadow": "off"
"no-shadow": "off",
"prettier/prettier": "error"
},
"settings": {
"import/resolver": {
Expand Down
24 changes: 0 additions & 24 deletions .jsbeautifyrc

This file was deleted.

2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/src
.editorconfig
.eslintrc
.jsbeautifyrc
.prettierrc
.travis.yml
babel.config.js
rollup.config.js
Expand Down
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arrowParens": "always",
"printWidth": 120,
"singleQuote": true,
"trailingComma": "all"
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
"codecov": "^3.0",
"eslint": "^6.0",
"eslint-config-airbnb-base": "^14.0",
"eslint-config-prettier": "^6.0",
"eslint-plugin-import": "^2.0",
"eslint-plugin-prettier": "^3.0",
"jest": "^24.0",
"js-beautify": "^1.0",
"prettier": "^1.0",
"rollup": "^1.23",
"rollup-plugin-babel": "^4.0",
"rollup-plugin-dts": "^1.0",
Expand Down
34 changes: 18 additions & 16 deletions src/index.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,25 @@ const contexts = {

const derivedContexts = {
schema: 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-1',
data: [{
schema: 'iglu:com.snowplowanalytics.snowplow/ua_parser_context/jsonschema/1-0-0',
data: {
useragentFamily: 'IE',
useragentMajor: '7',
useragentMinor: '0',
useragentPatch: null,
useragentVersion: 'IE 7.0',
osFamily: 'Windows XP',
osMajor: null,
osMinor: null,
osPatch: null,
osPatchMinor: null,
osVersion: 'Windows XP',
deviceFamily: 'Other',
data: [
{
schema: 'iglu:com.snowplowanalytics.snowplow/ua_parser_context/jsonschema/1-0-0',
data: {
useragentFamily: 'IE',
useragentMajor: '7',
useragentMinor: '0',
useragentPatch: null,
useragentVersion: 'IE 7.0',
osFamily: 'Windows XP',
osMajor: null,
osMinor: null,
osPatch: null,
osPatchMinor: null,
osVersion: 'Windows XP',
deviceFamily: 'Other',
},
},
}],
],
};

const unstructEvent = {
Expand Down
27 changes: 16 additions & 11 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,30 @@ describe('transform', () => {
});

it('should fail on fields number', () => {
expect(() => transform(encode({ a: '1', b: '2' })))
.toThrow('Wrong event fields number.');
expect(() => transform(encode({ a: '1', b: '2' }))).toThrow('Wrong event fields number.');
});

it('should fail on one field', () => {
expect(() => transform(encode({ ...event, tr_tax_base: 'bad_tax_base' })))
.toThrow("Invalid value for field 'tr_tax_base'.");
expect(() => transform(encode({ ...event, tr_tax_base: 'bad_tax_base' }))).toThrow(
"Invalid value for field 'tr_tax_base'.",
);
});

it('should fail on multiple fields', () => {
expect(() => transform(encode({ ...event, dvce_ismobile: 'bad_dvce_ismobile', tr_tax_base: 'bad_tax_base' })))
.toThrow([
"Invalid value for field 'dvce_ismobile'.",
"Invalid value for field 'tr_tax_base'.",
].join('\n'));
expect(() =>
transform(
encode({
...event,
dvce_ismobile: 'bad_dvce_ismobile',
tr_tax_base: 'bad_tax_base',
}),
),
).toThrow(["Invalid value for field 'dvce_ismobile'.", "Invalid value for field 'tr_tax_base'."].join('\n'));
});

it('should not return geo_location', () => {
expect(() => transform(encode({ ...event, geo_latitude: '', geo_longitude: '' })))
.not.toHaveProperty('geo_location');
expect(() => transform(encode({ ...event, geo_latitude: '', geo_longitude: '' }))).not.toHaveProperty(
'geo_location',
);
});
});
26 changes: 10 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,18 @@ function jsonifyGoodEvent(event: string[]): Event {

const errors = [] as string[];
const output = Object.keys(EVENT_STRUCTURE)
.reduce(
(output, key, index) => {
if (event[index]) {
try {
output.push(...EVENT_STRUCTURE[key](key, event[index]));
} catch (e) {
errors.push(e.message);
}
.reduce((output, key, index) => {
if (event[index]) {
try {
output.push(...EVENT_STRUCTURE[key](key, event[index]));
} catch (e) {
errors.push(e.message);
}
}

return output;
},
[] as Field[],
)
.reduce(
(output, { key, value }) => Object.assign(output, { [key]: value }),
{} as Event,
);
return output;
}, [] as Field[])
.reduce((output, { key, value }) => Object.assign(output, { [key]: value }), {} as Event);

if (errors.length) {
throw new TypeError(errors.join('\n'));
Expand Down
10 changes: 1 addition & 9 deletions src/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/

import {
Boolean,
Contexts,
Double,
Integer,
String,
Timestamp,
Unstruct,
} from './types';
import { Boolean, Contexts, Double, Integer, String, Timestamp, Unstruct } from './types';

export const LATITUDE_INDEX = 22;
export const LONGITUDE_INDEX = 23;
Expand Down
115 changes: 62 additions & 53 deletions src/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,7 @@
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/

import {
Boolean,
Contexts,
Double,
Integer,
String,
Timestamp,
Unstruct,
} from './types';
import { Boolean, Contexts, Double, Integer, String, Timestamp, Unstruct } from './types';

describe('String', () => {
it('should parse string', () => {
Expand Down Expand Up @@ -69,68 +61,85 @@ describe('Timestamp', () => {

describe('Contexts', () => {
it('should parse contexts', () => {
expect(Contexts('', JSON.stringify({
data: [
{
data: { unique: true },
schema: 'iglu:com.acme/unduplicated/jsonschema/1-0-0',
},
{
data: { value: 1 },
schema: 'iglu:com.acme/duplicated/jsonschema/1-0-0',
},
{
data: { value: 2 },
schema: 'iglu:com.acme/duplicated/jsonschema/1-0-0',
},
],
schema: 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0',
}))).toEqual([
expect(
Contexts(
'',
JSON.stringify({
data: [
{
data: { unique: true },
schema: 'iglu:com.acme/unduplicated/jsonschema/1-0-0',
},
{
data: { value: 1 },
schema: 'iglu:com.acme/duplicated/jsonschema/1-0-0',
},
{
data: { value: 2 },
schema: 'iglu:com.acme/duplicated/jsonschema/1-0-0',
},
],
schema: 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0',
}),
),
).toEqual([
{
key: 'contexts_com_acme_unduplicated_1',
value: [
{ unique: true },
],
value: [{ unique: true }],
},
{
key: 'contexts_com_acme_duplicated_1',
value: [
{ value: 1 },
{ value: 2 },
],
value: [{ value: 1 }, { value: 2 }],
},
]);
});
});

describe('Unstruct', () => {
it('should parse unstruct', () => {
expect(Unstruct('', JSON.stringify({
data: {
data: { key: 'value' },
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1',
expect(
Unstruct(
'',
JSON.stringify({
data: {
data: { key: 'value' },
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1',
},
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
}),
),
).toEqual([
{
key: 'unstruct_event_com_snowplowanalytics_snowplow_link_click_1',
value: { key: 'value' },
},
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
}))).toEqual([{
key: 'unstruct_event_com_snowplowanalytics_snowplow_link_click_1',
value: { key: 'value' },
}]);
]);
});

it('should throw an error on missing data', () => {
expect(() => Unstruct('', JSON.stringify({
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
data: {},
}))).toThrow();
expect(() =>
Unstruct(
'',
JSON.stringify({
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
data: {},
}),
),
).toThrow();
});

it('should throw an error on invalid schema', () => {
expect(() => Unstruct('', JSON.stringify({
data: {
data: { key: 'value' },
schema: 'something',
},
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
}))).toThrow();
expect(() =>
Unstruct(
'',
JSON.stringify({
data: {
data: { key: 'value' },
schema: 'something',
},
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
}),
),
).toThrow();
});
});
Loading

0 comments on commit 6b36225

Please sign in to comment.