diff --git a/.eslintrc b/.eslintrc index 5b747ad..65a0379 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,7 +3,8 @@ "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": { @@ -11,7 +12,8 @@ "tsconfigRootDir": "." }, "plugins": [ - "@typescript-eslint" + "@typescript-eslint", + "prettier" ], "rules": { "@typescript-eslint/camelcase": ["error", { "properties": "never", "ignoreDestructuring": true }], @@ -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": { diff --git a/.jsbeautifyrc b/.jsbeautifyrc deleted file mode 100644 index 940e86c..0000000 --- a/.jsbeautifyrc +++ /dev/null @@ -1,24 +0,0 @@ -{ - "js": { - "allowed_file_extensions": ["js", "ts", "json", "jsbeautifyrc"], - "brace_style": "collapse,preserve-inline", - "break_chained_methods": false, - "e4x": false, - "end_with_newline": false, - "indent_char": " ", - "indent_level": 0, - "indent_size": 2, - "indent_with_tabs": false, - "jslint_happy": false, - "keep_array_indentation": false, - "keep_function_indentation": false, - "max_preserve_newlines": 2, - "preserve_newlines": true, - "space_after_anon_function": false, - "space_before_conditional": true, - "space_in_empty_paren": false, - "space_in_paren": false, - "unescape_strings": false, - "wrap_line_length": 0 - } -} diff --git a/.npmignore b/.npmignore index 4343140..4a3f0fc 100644 --- a/.npmignore +++ b/.npmignore @@ -2,7 +2,7 @@ /src .editorconfig .eslintrc -.jsbeautifyrc +.prettierrc .travis.yml babel.config.js rollup.config.js diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..61109b6 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "arrowParens": "always", + "printWidth": 120, + "singleQuote": true, + "trailingComma": "all" +} diff --git a/package.json b/package.json index b6b20c3..28e1090 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.fixture.ts b/src/index.fixture.ts index a5b0f54..9a6580d 100644 --- a/src/index.fixture.ts +++ b/src/index.fixture.ts @@ -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 = { diff --git a/src/index.spec.ts b/src/index.spec.ts index 095ce34..4fa93c2 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -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', + ); }); }); diff --git a/src/index.ts b/src/index.ts index 0e9b385..dc86c47 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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')); diff --git a/src/structure.ts b/src/structure.ts index ae3c339..9b138c3 100644 --- a/src/structure.ts +++ b/src/structure.ts @@ -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; diff --git a/src/types.spec.ts b/src/types.spec.ts index 9632ba8..0bb34a9 100644 --- a/src/types.spec.ts +++ b/src/types.spec.ts @@ -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', () => { @@ -69,35 +61,35 @@ 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 }], }, ]); }); @@ -105,32 +97,49 @@ describe('Contexts', () => { 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(); }); }); diff --git a/src/types.ts b/src/types.ts index 3203143..5a9c5b2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -55,24 +55,22 @@ function fixSchema(prefix: string, schema: string): string { * @return List of validated tuples containing a fixed schema string and the original data Object. */ export function Contexts(key: string, contexts: string): Field[] { - const distinctContexts = (JSON.parse(contexts).data as Context[]) - .reduce( - (contexts, context) => { - const schema = fixSchema('contexts', context.schema); + const distinctContexts = (JSON.parse(contexts).data as Context[]).reduce((contexts, context) => { + const schema = fixSchema('contexts', context.schema); - if (!contexts[schema]) { - contexts[schema] = []; - } + if (!contexts[schema]) { + contexts[schema] = []; + } - contexts[schema].push(context.data); + contexts[schema].push(context.data); - return contexts; - }, - {}, - ); + return contexts; + }, {}); - return Object.keys(distinctContexts) - .map((key) => ({ key, value: distinctContexts[key] })); + return Object.keys(distinctContexts).map((key) => ({ + key, + value: distinctContexts[key], + })); } /** @@ -88,10 +86,12 @@ export function Unstruct(key: string, unstruct: string): Field[] { throw new TypeError('Could not extract inner data field from unstructured event.'); } - return [{ - key: fixSchema('unstruct_event', context.schema), - value: context.data, - }]; + return [ + { + key: fixSchema('unstruct_event', context.schema), + value: context.data, + }, + ]; } export function String(key: string, value: string): Field[] {