|
|
|
import morph from 'morphmorph'
|
|
|
|
|
|
|
|
const KEY = 'CARBON_STATE'
|
|
|
|
|
|
|
|
const assign = morph.assign(KEY)
|
|
|
|
|
|
|
|
const parse = v => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(v)
|
|
|
|
} catch (e) {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const toggle = stateField => state => ({ [stateField]: !state[stateField] })
|
|
|
|
|
|
|
|
// https://gist.github.com/alexgibson/1704515
|
|
|
|
// TODO use https://github.com/sindresorhus/escape-goat/
|
|
|
|
export const escapeHtml = s => {
|
|
|
|
if (typeof s === 'string') {
|
|
|
|
return s
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/\//g, '/')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const unescapeHtml = s => {
|
|
|
|
if (typeof s === 'string') {
|
|
|
|
return s
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(///g, '/')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const parseRGBA = obj => `rgba(${obj.r},${obj.g},${obj.b},${obj.a})`
|
|
|
|
|
|
|
|
export const getState = morph.compose(
|
|
|
|
parse,
|
|
|
|
escapeHtml,
|
|
|
|
morph.get(KEY)
|
|
|
|
)
|
|
|
|
export const saveState = (window, v) => assign(window, JSON.stringify(v))
|
|
|
|
|
|
|
|
export const capitalizeFirstLetter = s => s.charAt(0).toUpperCase() + s.slice(1)
|
|
|
|
|
|
|
|
export const range = n => [...Array(n).keys()]
|
|
|
|
|
|
|
|
export const fileToDataURL = blob =>
|
|
|
|
new Promise(res => {
|
|
|
|
const reader = new FileReader()
|
|
|
|
reader.onload = e => res(e.target.result)
|
|
|
|
reader.readAsDataURL(blob)
|
|
|
|
})
|
|
|
|
|
|
|
|
export const formatCode = async code => {
|
|
|
|
const prettier = await import('prettier/standalone')
|
|
|
|
const babylonParser = await import('prettier/parser-babylon')
|
|
|
|
|
|
|
|
return prettier.format(code, {
|
|
|
|
parser: 'babylon',
|
|
|
|
plugins: [babylonParser],
|
|
|
|
semi: false,
|
|
|
|
singleQuote: true
|
|
|
|
})
|
|
|
|
}
|