flow-infer-type
Advanced tools
Comparing version 1.1.3 to 1.2.0
@@ -12,3 +12,3 @@ const obj = `{ | ||
const test = JSON.stringify(toJson(obj), (k,v) => { | ||
const test = JSON.stringify(toJson(obj), (k, v) => { | ||
eval(`var t = ${v}`) | ||
@@ -18,3 +18,3 @@ console.log(t.c.d) | ||
}) | ||
// .map(e => e.split(':')) | ||
// .map(e => e.split(':')) | ||
@@ -21,0 +21,0 @@ console.log(test) |
#!/usr/bin/env node | ||
const createFlowType = require('./index'); | ||
const { createFlowType } = require('./index'); | ||
@@ -6,0 +6,0 @@ const parse = str => { |
@@ -0,25 +1,23 @@ | ||
const _ = require('lodash'); | ||
const R = require('ramda'); | ||
// toFlowType :: Object => String | ||
const toFlowType = obj => JSON.stringify(obj, null, 2); | ||
// serialize :: Object => String | ||
const serialize = obj => JSON.stringify(obj, null, 2); | ||
const cleanup = str => str.replace(/"/g, ''); | ||
const walk = (func, obj) => JSON.parse(JSON.stringify(obj), func); | ||
const createFlowType = obj => cleanup(toFlowType(walk((key, value) => { | ||
if (R.is(Number, value)) { | ||
return 'number'; | ||
} | ||
if (R.is(Boolean, value)) { | ||
return 'boolean'; | ||
} | ||
if (R.isEmpty(value)) { | ||
return 'Object'; | ||
} | ||
if (R.is(String, value)) { | ||
return 'string'; | ||
} | ||
const createFlowType = obj => cleanup(serialize(walk(mapType, obj))); | ||
return value; | ||
}, obj))); | ||
const mapType = (key, value) => R.cond([[R.is(Number), R.always('number')], [R.is(Boolean), R.always('boolean')], [R.isEmpty, R.always('Object')], [R.is(String), R.always('string')], [Array.isArray, value => `Array<${findMode(value)}>`], [R.T, R.always(value)]])(value); | ||
module.exports = createFlowType; | ||
const inferType = R.cond([[R.isNil, R.always('void')], [_.isInteger, R.always('integer')], [R.is(Number), R.always('number')], [_.isDate, R.always('date')], [R.is(String), R.always('string')], [R.T, R.always('mixed')]]); | ||
// findMode :: Array<T> => T | ||
const findMode = R.pipe(R.countBy(R.identity), R.toPairs(), R.sortBy(R.tail), R.takeLast(1), R.map(R.head), R.head); | ||
const mostCommonTypeInAr = R.pipe(R.map(inferType), findMode); | ||
module.exports = { | ||
createFlowType, | ||
mostCommonTypeInAr | ||
}; |
{ | ||
"name": "flow-infer-type", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "Generate flow type description from given object", | ||
@@ -5,0 +5,0 @@ "main": "./dist/bin.js", |
#!/usr/bin/env node | ||
const createFlowType = require('./index') | ||
const { createFlowType } = require('./index') | ||
@@ -5,0 +5,0 @@ const parse = (str) => { |
// @flow | ||
const _ = require('lodash') | ||
const R = require('ramda') | ||
// toFlowType :: Object => String | ||
const toFlowType = (obj) => JSON.stringify(obj, null, 2) | ||
// serialize :: Object => String | ||
const serialize = (obj) => JSON.stringify(obj, null, 2) | ||
const cleanup = (str) => str.replace(/"/g, '') | ||
@@ -10,22 +12,40 @@ const walk = (func, obj) => JSON.parse(JSON.stringify(obj), func) | ||
const createFlowType = (obj) => | ||
cleanup( | ||
toFlowType( | ||
walk((key, value) => { | ||
if (R.is(Number, value)) { | ||
return 'number' | ||
} | ||
if (R.is(Boolean, value)) { | ||
return 'boolean' | ||
} | ||
if (R.isEmpty(value)) { | ||
return 'Object' | ||
} | ||
if (R.is(String, value)) { | ||
return 'string' | ||
} | ||
cleanup(serialize(walk(mapType, obj))) | ||
return value | ||
}, obj))) | ||
const mapType = (key, value) => R.cond([ | ||
[R.is(Number), R.always('number')], | ||
[R.is(Boolean), R.always('boolean')], | ||
[R.isEmpty, R.always('Object')], | ||
[R.is(String), R.always('string')], | ||
[Array.isArray, value => `Array<${findMode(value)}>`], | ||
[R.T, R.always(value)] | ||
])(value) | ||
const inferType = R.cond([ | ||
[R.isNil, R.always('void')], | ||
[_.isInteger, R.always('integer')], | ||
[R.is(Number), R.always('number')], | ||
[_.isDate, R.always('date')], | ||
[R.is(String), R.always('string')], | ||
[R.T, R.always('mixed')] | ||
]) | ||
module.exports = createFlowType | ||
// findMode :: Array<T> => T | ||
const findMode = R.pipe( | ||
R.countBy(R.identity), | ||
R.toPairs(), | ||
R.sortBy(R.tail), | ||
R.takeLast(1), | ||
R.map(R.head), | ||
R.head, | ||
) | ||
const mostCommonTypeInAr = R.pipe( | ||
R.map(inferType), | ||
findMode, | ||
) | ||
module.exports = { | ||
createFlowType, | ||
mostCommonTypeInAr, | ||
} |
// @flow | ||
import test from 'ava' | ||
const createFlowType = require('../src') | ||
const { createFlowType } = require('../src') | ||
@@ -51,3 +51,2 @@ test(t => { | ||
// console.warn('output', createFlowType(user)) | ||
t.deepEqual(createFlowType(user), inferredUser) | ||
@@ -97,7 +96,3 @@ }) | ||
}, | ||
required: [ | ||
string, | ||
string, | ||
string | ||
] | ||
required: Array<string> | ||
} | ||
@@ -104,0 +99,0 @@ }` |
Sorry, the diff of this file is not supported yet
57520
18
213