From 7c1f7efe62507334ec491a0b2707fe9ba292467e Mon Sep 17 00:00:00 2001 From: thurt Date: Sun, 19 Mar 2017 10:17:20 -0400 Subject: [PATCH 1/5] fixes #2 by adding quotes around keys --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 679e6ca..67f8b70 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ module.exports = function doIt(inputObject, spaceAmount, useTabs) { const indentation = generateIndentation(indentationLevel + 1) for (const key in object) { const value = object[key] - type += `${indentation}${key}: ${anyToType(value, indentationLevel + 1)},\n` + type += `${indentation}"${key}": ${anyToType(value, indentationLevel + 1)},\n` } return `${type}${generateIndentation(indentationLevel)}}` From 3b1db078f1c01a8fe8041b6178ce8a8ab4854b1f Mon Sep 17 00:00:00 2001 From: thurt Date: Sun, 19 Mar 2017 13:35:53 -0400 Subject: [PATCH 2/5] adds jest and npm scripts; --- package.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 840ae58..2b3eb83 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Transform JSON into flow type annotations automatically", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "jest --verbose --coverage", + "test:watch": "npm run test -- --watch" }, "bin": "./bin.js", "repository": { @@ -28,5 +29,8 @@ "dependencies": { "commander": "^2.9.0", "oboe": "^2.1.2" + }, + "devDependencies": { + "jest": "^19.0.2" } } From bd59d835a0916074bfdde28574e24da2bafa260d Mon Sep 17 00:00:00 2001 From: thurt Date: Sun, 19 Mar 2017 13:38:02 -0400 Subject: [PATCH 3/5] changes double quotes around key to single quotes -- this makes snapshot easier to read b/c it does not need to escape a single quote; changes function name doIt -> jsonFlow; --- bin.js | 6 +++--- index.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bin.js b/bin.js index ac545e9..be5676a 100755 --- a/bin.js +++ b/bin.js @@ -4,7 +4,7 @@ const fs = require('fs') const oboe = require('oboe') const program = require('commander') -const doIt = require('./index') +const jsonFlow = require('./index') program .option('-t, --tabs', 'Use tabs instead of spaces') @@ -14,9 +14,9 @@ program let inputJson if (program.args[0]) { inputJson = fs.readFileSync(program.args[0]) - console.log(doIt(JSON.parse(inputJson), program.spaceAmount, program.tabs)) + console.log(jsonFlow(JSON.parse(inputJson), program.spaceAmount, program.tabs)) } else { const stdin = process.stdin - oboe(stdin).done(text => console.log(doIt(text, program.spaceAmount, program.tabs))).fail(console.error) + oboe(stdin).done(text => console.log(jsonFlow(text, program.spaceAmount, program.tabs))).fail(console.error) } diff --git a/index.js b/index.js index 67f8b70..7d0ced2 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = function doIt(inputObject, spaceAmount, useTabs) { +module.exports = function jsonFlow(inputObject, spaceAmount, useTabs) { function arrayToType (array, indentationLevel) { if (array.length > 0) { @@ -25,7 +25,7 @@ module.exports = function doIt(inputObject, spaceAmount, useTabs) { const indentation = generateIndentation(indentationLevel + 1) for (const key in object) { const value = object[key] - type += `${indentation}"${key}": ${anyToType(value, indentationLevel + 1)},\n` + type += `${indentation}'${key}': ${anyToType(value, indentationLevel + 1)},\n` } return `${type}${generateIndentation(indentationLevel)}}` From e648dc7914aeb31af3a73f51779716034f4bf0b2 Mon Sep 17 00:00:00 2001 From: thurt Date: Sun, 19 Mar 2017 13:38:52 -0400 Subject: [PATCH 4/5] updates .gitignore; --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3c3629e..916e0b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +coverage/ From 99ca37463bfd074b242e22fd2420b382c57eafc2 Mon Sep 17 00:00:00 2001 From: thurt Date: Sun, 19 Mar 2017 13:39:12 -0400 Subject: [PATCH 5/5] adds tests for jsonFlow; --- __tests__/__snapshots__/index.test.js.snap | 44 ++++++++++++++++++++++ __tests__/index.test.js | 36 ++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 __tests__/__snapshots__/index.test.js.snap create mode 100644 __tests__/index.test.js diff --git a/__tests__/__snapshots__/index.test.js.snap b/__tests__/__snapshots__/index.test.js.snap new file mode 100644 index 0000000..9b9dc73 --- /dev/null +++ b/__tests__/__snapshots__/index.test.js.snap @@ -0,0 +1,44 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`matches snapshot for null input 1`] = `"void"`; + +exports[`matches snapshot for sampleJson using 2 spaces 1`] = ` +"{ + 'test-list': { + 'id': number, + 'properties': { + 'name': string, + }, + }[], + 'empty-list': [], + 'voided-property': void, +}" +`; + +exports[`matches snapshot for sampleJson using 4 spaces 1`] = ` +"{ + 'test-list': { + 'id': number, + 'properties': { + 'name': string, + }, + }[], + 'empty-list': [], + 'voided-property': void, +}" +`; + +exports[`matches snapshot for sampleJson using tabs 1`] = ` +"{ + 'test-list': { + 'id': number, + 'properties': { + 'name': string, + }, + }[], + 'empty-list': [], + 'voided-property': void, +}" +`; + +exports[`matches snapshot for undefined input 1`] = `"void"`; diff --git a/__tests__/index.test.js b/__tests__/index.test.js new file mode 100644 index 0000000..aec4fb0 --- /dev/null +++ b/__tests__/index.test.js @@ -0,0 +1,36 @@ + +const jsonFlow = require('../') + +const sampleJson = { + 'test-list': [{ + id: 1, + properties: { name: 'a' } + }, { + id: 2, + properties: { name: 'b' } + }], + 'empty-list': [], + 'voided-property': null +} + +it('matches snapshot for undefined input', () => { + expect(jsonFlow(undefined)).toMatchSnapshot() +}) + +it('matches snapshot for null input', () => { + expect(jsonFlow(null)).toMatchSnapshot() +}) + +it('matches snapshot for sampleJson using 2 spaces', () => { + expect(jsonFlow(sampleJson, 2)).toMatchSnapshot() +}) + +it('matches snapshot for sampleJson using 4 spaces', () => { + expect(jsonFlow(sampleJson, 4)).toMatchSnapshot() +}) + +it('matches snapshot for sampleJson using tabs', () => { + expect(jsonFlow(sampleJson, undefined, true)).toMatchSnapshot() +}) + +