summaryrefslogtreecommitdiff
path: root/lib.js
blob: b5290859ce02362e62309eb7404438271d11a694 (plain)
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
const Lib = {
  makeStateContainer(intialState, renderCallback) {
    let currentState = intialState
    return {
     set(newState, afterRenderCallback = () => {}) {
      currentState = { ...currentState, ...newState }
      renderCallback()
      afterRenderCallback()
     },
     get() { return currentState }
   }
  },
  splitAt(str, index) {
    return [str.slice(0, index), str.slice(index)]
  },
  readFile(file) {
    return new Promise(function(resolve, reject) {
      const reader = new FileReader()
      reader.onload = function(event) {
        resolve(event.target.result)
      }
      reader.readAsText(file)
    })
  },
  getQueryParam(paramName) {
    const queryPairs = window.location.search.slice(1).split("&").map(k => k.split("="))
    const foundPair = queryPairs.find(([name]) =>  name === paramName)
    if(!foundPair) return null;
    return foundPair[1]
  }
}