-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.js
117 lines (108 loc) · 2.79 KB
/
util.js
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
export async function loadJson (url) {
const response = await fetch(url, {
method: 'GET',
credentials: 'include',
mode: 'no-cors'
})
return response.json()
}
export function genColour (str) {
let hash = 0
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash)
}
let colour = '#'
for (let i = 0; i < 3; i++) {
const value = (hash >> (i * 8)) & 0xFF
colour += ('00' + value.toString(16)).substr(-2)
}
return colour
}
export function revColour (inColour) {
const rgb = inColour.match(/\d+/g)
const luma = 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2] // ITU-R BT.709
if (luma < 128) {
return '#fff'
} else {
return '#000'
}
}
Set.prototype.intersection = function (setB) { // eslint-disable-line
const intersection = new Set()
for (const elem of setB) {
if (this.has(elem)) {
intersection.add(elem)
}
}
return intersection
}
Set.prototype.union = function (setB) { // eslint-disable-line
const union = new Set()
for (const elemA of this) {
union.add(elemA)
}
for (const elemB of setB) {
union.add(elemB)
}
return union
}
Set.prototype.difference = function (setB) { // eslint-disable-line
const difference = new Set()
for (const elem of this) {
if (!setB.has(elem)) {
difference.add(elem)
}
}
return difference
}
Object.prototype.forEach = function (func) { // eslint-disable-line
for (const item in this) {
if (this.hasOwnProperty(item)) { // eslint-disable-line
func(item)
}
}
}
Object.prototype.keys = function () { // eslint-disable-line
const keys = []
this.forEach(item => keys.push(item))
return keys
}
Object.prototype.get = function (key, backstop) { // eslint-disable-line
if (this.hasOwnProperty(key)) { // eslint-disable-line
return this[key]
} else {
return backstop
}
}
/*
* (c)2006 Dean Edwards/Matthias Miller/John Resig
* Special thanks to Dan Webb's domready.js Prototype extension
* and Simon Willison's addLoadEvent
*
* For more info, see:
* http://dean.edwards.name/weblog/2006/06/again/
*
* Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/)
*/
export function addDOMLoadEvent (func) {
if (!window.__load_events) {
const init = function () {
let i = 0
// quit if this function has already been called
if (addDOMLoadEvent.done) { return }
addDOMLoadEvent.done = true
if (window.__load_timer) {
clearInterval(window.__load_timer)
window.__load_timer = null
}
for (i; i < window.__load_events.length; i += 1) {
window.__load_events[i]()
}
window.__load_events = null
}
document.addEventListener('DOMContentLoaded', init, false)
window.onload = init
window.__load_events = []
}
window.__load_events.push(func)
}