Skip to content

Commit

Permalink
fix(generators): use modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed May 18, 2018
1 parent d40b9d2 commit 9cec118
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 52 deletions.
8 changes: 4 additions & 4 deletions packages/nivo-generators/src/chord.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const range = require('lodash.range')
const random = require('lodash.random')
const sets = require('./sets')
import range from 'lodash.range'
import random from 'lodash.random'
import { names } from './sets'

module.exports = ({ keys = sets.names, size = 7, minValue = 0, maxValue = 2000 } = {}) => {
export default ({ keys = names, size = 7, minValue = 0, maxValue = 2000 } = {}) => {
const maxSize = Math.min(keys.length, size)
const selectedKeys = keys.slice(0, maxSize)

Expand Down
2 changes: 1 addition & 1 deletion packages/nivo-generators/src/color.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
exports.randColor = () => `hsl(${Math.round(Math.random() * 360)}, 70%, 50%)`
export const randColor = () => `hsl(${Math.round(Math.random() * 360)}, 70%, 50%)`
69 changes: 36 additions & 33 deletions packages/nivo-generators/src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const range = require('lodash.range')
const random = require('lodash.random')
const shuffle = require('lodash.shuffle')
const { timeDays } = require('d3-time')
const { timeFormat } = require('d3-time-format')
const color = require('./color')
const sets = require('./sets')

exports.sets = sets
exports.randColor = color.randColor

exports.generateProgrammingLanguageStats = (shouldShuffle = true, limit = -1) => {
import range from 'lodash.range'
import random from 'lodash.random'
import shuffle from 'lodash.shuffle'
import { timeDays } from 'd3-time'
import { timeFormat } from 'd3-time-format'
import * as color from './color'
import * as sets from './sets'

export { sets }
export const randColor = color.randColor

export const generateProgrammingLanguageStats = (shouldShuffle = true, limit = -1) => {
let langs = sets.programmingLanguages
if (shouldShuffle) {
langs = shuffle(langs)
Expand All @@ -21,11 +21,11 @@ exports.generateProgrammingLanguageStats = (shouldShuffle = true, limit = -1) =>
return langs.slice(0, limit).map(language => ({
label: language,
value: Math.round(Math.random() * 600),
color: exports.randColor(),
color: randColor(),
}))
}

exports.uniqRand = generator => {
export const uniqRand = generator => {
const used = []

return (...args) => {
Expand All @@ -40,24 +40,24 @@ exports.uniqRand = generator => {
}
}

exports.randCountryCode = () => shuffle(sets.countryCodes)[0]
export const randCountryCode = () => shuffle(sets.countryCodes)[0]

exports.generateDrinkStats = (size = 16) => {
export const generateDrinkStats = (size = 16) => {
const rand = () => random(0, 60)
const types = ['whisky', 'rhum', 'gin', 'vodka', 'cognac']
const country = exports.uniqRand(exports.randCountryCode)
const country = uniqRand(randCountryCode)

const data = types.map(id => ({
id,
color: exports.randColor(),
color: randColor(),
data: [],
}))

range(size).forEach(() => {
const x = country()
types.forEach(id => {
data.find(d => d.id === id).data.push({
color: exports.randColor(),
color: randColor(),
x,
y: rand(),
})
Expand All @@ -67,7 +67,7 @@ exports.generateDrinkStats = (size = 16) => {
return data
}

exports.generateSerie = (length = 20) => {
export const generateSerie = (length = 20) => {
const data = []
const max = 100 + Math.random() * (Math.random() * 600)

Expand All @@ -78,21 +78,21 @@ exports.generateSerie = (length = 20) => {
return data
}

exports.generateStackData = (size = 3) => {
export const generateStackData = (size = 3) => {
const length = 16
return range(size).map(() => exports.generateSerie(length).map((v, i) => ({ x: i, y: v })))
return range(size).map(() => generateSerie(length).map((v, i) => ({ x: i, y: v })))
}

exports.generateCountriesPopulation = size => {
const countryCode = exports.uniqRand(exports.randCountryCode())
export const generateCountriesPopulation = size => {
const countryCode = uniqRand(randCountryCode())

return range(size).map(() => ({
country: countryCode(),
population: 200 + Math.round(Math.random() * Math.random() * 1000000),
}))
}

exports.generateDayCounts = (from, to, maxSize = 0.9) => {
export const generateDayCounts = (from, to, maxSize = 0.9) => {
const days = timeDays(from, to)

const size =
Expand All @@ -111,15 +111,18 @@ exports.generateDayCounts = (from, to, maxSize = 0.9) => {
})
}

exports.generateCountriesData = (keys, { size = 12, min = 0, max = 200, withColors = true } = {}) =>
export const generateCountriesData = (
keys,
{ size = 12, min = 0, max = 200, withColors = true } = {}
) =>
sets.countryCodes.slice(0, size).map(country => {
const d = {
country,
}
keys.forEach(key => {
d[key] = random(min, max)
if (withColors === true) {
d[`${key}Color`] = exports.randColor()
d[`${key}Color`] = randColor()
}
})

Expand Down Expand Up @@ -208,22 +211,22 @@ const libTreeItems = [
],
]

exports.generateLibTree = (name = 'nivo', limit, children = libTreeItems) => {
export const generateLibTree = (name = 'nivo', limit, children = libTreeItems) => {
limit = limit || children.length
if (limit > children.length) {
limit = children.length
}

const tree = {
name,
color: exports.randColor(),
color: randColor(),
}
if (children && children.length > 0) {
tree.children = range(limit).map((o, i) => {
const leaf = children[i]

// full path `${name}.${leaf[0]}`
return exports.generateLibTree(leaf[0], null, leaf[1] || [])
return generateLibTree(leaf[0], null, leaf[1] || [])
})
} else {
tree.loc = Math.round(Math.random() * 200000)
Expand All @@ -234,7 +237,7 @@ exports.generateLibTree = (name = 'nivo', limit, children = libTreeItems) => {

const wines = ['chardonay', 'carmenere', 'syrah']
const wineTastes = ['fruity', 'bitter', 'heavy', 'strong', 'sunny']
exports.generateWinesTastes = ({ randMin = 20, randMax = 120 } = {}) => {
export const generateWinesTastes = ({ randMin = 20, randMax = 120 } = {}) => {
const data = wineTastes.map(taste => {
const d = { taste }
wines.forEach(wine => {
Expand All @@ -247,5 +250,5 @@ exports.generateWinesTastes = ({ randMin = 20, randMax = 120 } = {}) => {
return { data, keys: wines }
}

exports.generateSankeyData = require('./sankey')
exports.generateChordData = require('./chord')
export { default as generateSankeyData } from './sankey'
export { default as generateChordData } from './chord'
16 changes: 8 additions & 8 deletions packages/nivo-generators/src/sankey.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const range = require('lodash.range')
const random = require('lodash.random')
const shuffle = require('lodash.shuffle')
const color = require('./color')
const sets = require('./sets')
import range from 'lodash.range'
import random from 'lodash.random'
import shuffle from 'lodash.shuffle'
import { randColor } from './color'
import { names } from './sets'

const availableNodes = sets.names.map(name => ({ id: name }))
const availableNodes = names.map(name => ({ id: name }))

const getNodeTargets = (id, links, currentPath) => {
const targets = links.filter(({ source }) => source === id).map(({ target }) => {
Expand Down Expand Up @@ -43,10 +43,10 @@ const getNodesTargets = links =>
return targetsById
}, {})

module.exports = ({ nodeCount, maxIterations = 3 } = {}) => {
export default ({ nodeCount, maxIterations = 3 } = {}) => {
const nodes = availableNodes.slice(0, nodeCount).map(node =>
Object.assign({}, node, {
color: color.randColor(),
color: randColor(),
})
)

Expand Down
2 changes: 1 addition & 1 deletion packages/nivo-generators/src/sets/countryCodes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = [
export default [
'AD',
'AE',
'AF',
Expand Down
6 changes: 3 additions & 3 deletions packages/nivo-generators/src/sets/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.countryCodes = require('./countryCodes')
exports.names = require('./names')
exports.programmingLanguages = require('./programmingLanguages')
export { default as countryCodes } from './countryCodes'
export { default as names } from './names'
export { default as programmingLanguages } from './programmingLanguages'
2 changes: 1 addition & 1 deletion packages/nivo-generators/src/sets/names.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = [
export default [
'John',
'Raoul',
'Jane',
Expand Down
2 changes: 1 addition & 1 deletion packages/nivo-generators/src/sets/programmingLanguages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = [
export default [
'php',
'make',
'javascript',
Expand Down

0 comments on commit 9cec118

Please sign in to comment.