/ embark-ui / src / utils / utils.js
utils.js
 1  import Convert from 'ansi-to-html';
 2  import qs from 'qs';
 3  
 4  export function last(array) {
 5    return array && array.length ? array[array.length - 1] : undefined;
 6  }
 7  /* eslint no-bitwise: "off" */
 8  export function hashCode(str) {
 9    let hash = 0;
10    if (str.length === 0) return hash;
11    for (let i = 0; i < str.length; i++) {
12      const char = str.charCodeAt(i);
13      hash = ((hash << 5) - hash) + char;
14      hash = hash & hash; // Convert to 32bit integer
15    }
16    return hash;
17  }
18  
19  export function ansiToHtml(text) {
20    const convert = new Convert();
21    return convert.toHtml(text.replace(/\n/g,'<br>'));
22  }
23  
24  export function getQueryToken(location) {
25    return qs.parse(location.search, {ignoreQueryPrefix: true}).token;
26  }
27  
28  export function stripQueryToken(location) {
29    const _location = Object.assign({}, location);
30    _location.search = _location.search.replace(
31      /(\?|&?)(token=[\w-]*)(&?)/,
32      (_, p1, p2, p3) => (p2 ? (p3 === '&' ? p1 : '') : '')
33    );
34    return _location;
35  }