-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathStringUtils.ts
More file actions
46 lines (35 loc) · 1.25 KB
/
Copy pathStringUtils.ts
File metadata and controls
46 lines (35 loc) · 1.25 KB
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
import memoize from 'lodash.memoize'
export const isString = (value: any) => typeof value === 'string'
export namespace StringUtils {
export const classPrefix = 'mde'
export const truncate = (str: string, max: number = 40) => {
return isString(str) && str.length > max ? str.slice(0, max) + '...' : str
}
/**
* Five levels of brightness from 1 to 5.
*
* @param brightness
*/
export const getRandomColor = (brightness: number) => {
if (brightness < 1 || brightness > 5)
throw new Error(
'Only five brightness levels, from 1 to 5, are acceptable.',
)
const variance = 255 / 5
const getByte = () =>
Math.round(variance * (brightness - 1) + Math.random() * variance)
const rgb = [0, 0, 0].map(() => getByte()).join(',')
return `rgb(${rgb})`
}
export const toClipboard = (data: string, mimeType = 'text/plain') => {
document.addEventListener('copy', function (event: ClipboardEvent) {
event.clipboardData?.setData(mimeType, data)
event.preventDefault()
})
document.execCommand('copy', false)
}
export const getSize = memoize((content: string) => new Blob([content]).size)
export function getPrefixedClass(className) {
return `${classPrefix}-${className}`
}
}