File size: 3,815 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import forOwn from 'lodash/forOwn.js'
import isPlainObject from 'lodash/isPlainObject.js'
import isArray from 'lodash/isArray.js'
import isString from 'lodash/isString.js'
import isBoolean from 'lodash/isBoolean.js'
import isEqual from 'lodash/isEqual.js'
import dedent from 'dedent-js'

const compactArrays = (str: string) => {
    let out = ''
    let inString = false
    let quoteChar = ''

    for (let i = 0; i < str.length; i++) {
        const c = str[i]

        // enter/exit string literal
        if (!inString && (c === '"' || c === "'")) {
            inString = true
            quoteChar = c
            out += c
            continue
        }
        if (inString && c === quoteChar && str[i - 1] !== '\\') {
            inString = false
            quoteChar = ''
            out += c
            continue
        }

        // handle [ outside strings
        if (!inString && c === '[') {
            out += '['
            // skip any spaces immediately after
            while (str[i + 1] === ' ') i++
            continue
        }

        // handle ] outside strings
        if (!inString && c === ']') {
            // remove any spaces we just output
            while (out.endsWith(' ')) {
                out = out.slice(0, -1)
            }
            out += ']'
            continue
        }

        // default: copy character
        out += c
    }

    return out
}

const indent = (content: string, spaces: number = 8) =>
    content
        .split('\n')
        .map((line, i) => {
            if (i === 0) return line
            return `${' '.repeat(spaces)}${line}`
        })
        .join('\n')

const toJson = (value: any) => {
    const jsonString = JSON.stringify(value, null, 4)
    const normalized = jsonString
        .replace(/^(\s+)"([a-z]{1}[a-z]*)"\: /gim, (_match, space, key) => {
            return `${space}${key}: `
        })
        .replace(/"/gm, `'`)

    const compacted = compactArrays(normalized.replace(/\n/gm, ' ').replace(/\s{2,}/g, ' '))
    if (compacted.length <= 100) return compacted

    return indent(normalized)
}

export const generateChartCode = (
    name: string,
    props: any,
    {
        dataKey,
        children = [],
        defaults = {},
        pkg = 'nivo',
    }: {
        dataKey?: string
        children?: any[]
        defaults?: any
        pkg?: string
    } = {}
) => {
    const properties = []
    let args = ''

    if (dataKey !== undefined) {
        properties.push(`${dataKey}={${dataKey}}`)
        args = `{ ${dataKey} /* see ${dataKey} tab */ }`
    }

    forOwn(props, (_value, key) => {
        if (_value === undefined) return
        if (defaults && isEqual(defaults[key], _value)) return
        // Ignore the theme property as it's customized for the website.
        if (key === 'theme') return

        let value: string | undefined
        if (isPlainObject(_value)) {
            value = `{${toJson(_value)}}`
        } else if (isArray(_value)) {
            value = `{${toJson(_value)}}`
        } else if (isString(_value)) {
            value = `"${_value}"`
        } else if (isBoolean(_value)) {
            value = `{${_value ? 'true' : 'false'}}`
        } else if (typeof _value === 'function') {
            value = `{${indent(dedent(_value.toString()), 8)}}`
        } else if (_value === null) {
            value = `{null}`
        } else {
            value = `{${_value}}`
        }

        if (!value) return

        properties.push(`${key}=${value}`)
    })

    const imports = [`Responsive${name}`, ...children.map(([c]) => c)].map(
        i => `import { ${i} } from '${pkg}'`
    )

    return `${imports.join('\n')}\n
const My${name} = (${args}) => (
    <Responsive${name} /* or ${name} for fixed dimensions */
        ${properties.join('\n        ')}
    />
)`
}