Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quotes around keys #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
coverage/
44 changes: 44 additions & 0 deletions __tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -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"`;
36 changes: 36 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -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()
})


6 changes: 3 additions & 3 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)}}`
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -28,5 +29,8 @@
"dependencies": {
"commander": "^2.9.0",
"oboe": "^2.1.2"
},
"devDependencies": {
"jest": "^19.0.2"
}
}