All files / src/utils general.tsx

51.72% Statements 90/174
35.85% Branches 19/53
43.75% Functions 21/48
54% Lines 81/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329                                    55x 55x 55x 55x 55x 55x 55x     55x 55x           55x       55x                         55x                                 55x 4x                                                   2x 1x         1x           55x                     55x 2x 2x 2x 2x 1x   1x           55x 5x           55x                               55x                           55x                       55x         3x 3x 3x 8x 8x 8x 8x   4x     55x 8x 8x 15x 5x   10x 10x         55x             55x 7x     55x       55x               55x             55x 7x 7x         55x 1x 1x   1x       55x 2x 2x 1x   1x       55x 15x     55x 8x     55x 1x     55x 6x     55x           11x 18x 6x 4x         55x                                     55x       55x               55x          
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { Button, HTMLSelect, InputGroup, Intent } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import copy from 'copy-to-clipboard';
import FileSaver from 'file-saver';
import hasOwnProp from 'has-own-prop';
import numeral from 'numeral';
import React from 'react';
import { Filter, FilterRender } from 'react-table';
 
import { AppToaster } from '../singletons/toaster';
export function wait(ms: number): Promise<void> {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
}
 
export function addFilter(filters: Filter[], id: string, value: string): Filter[] {
  return addFilterRaw(filters, id, `"${value}"`);
}
 
export function addFilterRaw(filters: Filter[], id: string, value: string): Filter[] {
  const currentFilter = filters.find(f => f.id === id);
  if (currentFilter) {
    filters = filters.filter(f => f.id !== id);
    if (currentFilter.value !== value) {
      filters = filters.concat({ id, value });
    }
  } else {
    filters = filters.concat({ id, value });
  }
  return filters;
}
 
export function makeTextFilter(placeholder = ''): FilterRender {
  return ({ filter, onChange, key }) => {
    const filterValue = filter ? filter.value : '';
    return (
      <InputGroup
        key={key}
        onChange={(e: any) => onChange(e.target.value)}
        value={filterValue}
        rightElement={
          filterValue && <Button icon={IconNames.CROSS} minimal onClick={() => onChange('')} />
        }
        placeholder={placeholder}
      />
    );
  };
}
 
export function makeBooleanFilter(): FilterRender {
  return ({ filter, onChange, key }) => {
    const filterValue = filter ? filter.value : '';
    return (
      <HTMLSelect
        key={key}
        style={{ width: '100%' }}
        onChange={(event: any) => onChange(event.target.value)}
        value={filterValue || 'all'}
        fill
      >
        <option value="all">Show all</option>
        <option value="true">true</option>
        <option value="false">false</option>
      </HTMLSelect>
    );
  };
}
 
// ----------------------------
 
interface NeedleAndMode {
  needle: string;
  mode: 'exact' | 'prefix';
}
 
function getNeedleAndMode(input: string): NeedleAndMode {
  if (input.startsWith(`"`) && input.endsWith(`"`)) {
    return {
      needle: input.slice(1, -1),
      mode: 'exact',
    };
  }
  return {
    needle: input.startsWith(`"`) ? input.substring(1) : input,
    mode: 'prefix',
  };
}
 
export function booleanCustomTableFilter(filter: Filter, value: any): boolean {
  if (value == null) return false;
  const haystack = String(value).toLowerCase();
  const needleAndMode: NeedleAndMode = getNeedleAndMode(filter.value.toLowerCase());
  const needle = needleAndMode.needle;
  if (needleAndMode.mode === 'exact') {
    return needle === haystack;
  }
  return haystack.startsWith(needle);
}
 
export function sqlQueryCustomTableFilter(filter: Filter): string {
  const columnName = JSON.stringify(filter.id);
  const needleAndMode: NeedleAndMode = getNeedleAndMode(filter.value);
  const needle = needleAndMode.needle;
  if (needleAndMode.mode === 'exact') {
    return `${columnName} = '${needle}'`;
  } else {
    return `LOWER(${columnName}) LIKE LOWER('${needle}%')`;
  }
}
 
// ----------------------------
 
export function caseInsensitiveContains(testString: string, searchString: string): boolean {
  Eif (!searchString) return true;
  return testString.toLowerCase().includes(searchString.toLowerCase());
}
 
// ----------------------------
 
export function countBy<T>(
  array: T[],
  fn: (x: T, index: number) => string = String,
): Record<string, number> {
  const counts: Record<string, number> = {};
  for (let i = 0; i < array.length; i++) {
    const key = fn(array[i], i);
    counts[key] = (counts[key] || 0) + 1;
  }
  return counts;
}
 
function identity(x: any): any {
  return x;
}
 
export function lookupBy<T, Q>(
  array: T[],
  keyFn: (x: T, index: number) => string = String,
  valueFn: (x: T, index: number) => Q = identity,
): Record<string, Q> {
  const lookup: Record<string, Q> = {};
  const n = array.length;
  for (let i = 0; i < n; i++) {
    const a = array[i];
    lookup[keyFn(a, i)] = valueFn(a, i);
  }
  return lookup;
}
 
export function mapRecord<T, Q>(
  record: Record<string, T>,
  fn: (value: T, key: string) => Q,
): Record<string, Q> {
  const newRecord: Record<string, Q> = {};
  const keys = Object.keys(record);
  for (const key of keys) {
    newRecord[key] = fn(record[key], key);
  }
  return newRecord;
}
 
export function groupBy<T, Q>(
  array: readonly T[],
  keyFn: (x: T, index: number) => string,
  aggregateFn: (xs: readonly T[], key: string) => Q,
): Q[] {
  const buckets: Record<string, T[]> = {};
  const n = array.length;
  for (let i = 0; i < n; i++) {
    const value = array[i];
    const key = keyFn(value, i);
    buckets[key] = buckets[key] || [];
    buckets[key].push(value);
  }
  return Object.keys(buckets).map(key => aggregateFn(buckets[key], key));
}
 
export function uniq(array: readonly string[]): string[] {
  const seen: Record<string, boolean> = {};
  return array.filter(s => {
    if (hasOwnProp(seen, s)) {
      return false;
    } else {
      seen[s] = true;
      return true;
    }
  });
}
 
export function parseList(list: string): string[] {
  if (!list) return [];
  return list.split(',');
}
 
// ----------------------------
 
export function formatNumber(n: number): string {
  return numeral(n).format('0,0');
}
 
export function formatBytes(n: number): string {
  return numeral(n).format('0.00 b');
}
 
export function formatBytesCompact(n: number): string {
  return numeral(n).format('0.00b');
}
 
function pad2(str: string | number): string {
  return ('00' + str).substr(-2);
}
 
export function formatDuration(ms: number): string {
  const timeInHours = Math.floor(ms / 3600000);
  const timeInMin = Math.floor(ms / 60000) % 60;
  const timeInSec = Math.floor(ms / 1000) % 60;
  return timeInHours + ':' + pad2(timeInMin) + ':' + pad2(timeInSec);
}
 
export function pluralIfNeeded(n: number, singular: string, plural?: string): string {
  Eif (!plural) plural = singular + 's';
  return `${formatNumber(n)} ${n === 1 ? singular : plural}`;
}
 
// ----------------------------
 
export function parseJson(json: string): any {
  try {
    return JSON.parse(json);
  } catch (e) {
    return undefined;
  }
}
 
export function validJson(json: string): boolean {
  try {
    JSON.parse(json);
    return true;
  } catch (e) {
    return false;
  }
}
 
export function filterMap<T, Q>(xs: T[], f: (x: T, i: number) => Q | undefined): Q[] {
  return xs.map(f).filter((x: Q | undefined) => typeof x !== 'undefined') as Q[];
}
 
export function compact<T>(xs: (T | undefined | false | null | '')[]): T[] {
  return xs.filter(Boolean) as T[];
}
 
export function assemble<T>(...xs: (T | undefined | false | null | '')[]): T[] {
  return xs.filter(Boolean) as T[];
}
 
export function alphanumericCompare(a: string, b: string): number {
  return String(a).localeCompare(b, undefined, { numeric: true });
}
 
export function sortWithPrefixSuffix(
  things: string[],
  prefix: string[],
  suffix: string[],
  cmp: null | ((a: string, b: string) => number),
): string[] {
  const pre = uniq(prefix.filter(x => things.includes(x)));
  const mid = things.filter(x => !prefix.includes(x) && !suffix.includes(x));
  const post = uniq(suffix.filter(x => things.includes(x)));
  return pre.concat(cmp ? mid.sort(cmp) : mid, post);
}
 
// ----------------------------
 
export function downloadFile(text: string, type: string, filename: string): void {
  let blobType: string = '';
  switch (type) {
    case 'json':
      blobType = 'application/json';
      break;
    case 'tsv':
      blobType = 'text/tab-separated-values';
      break;
    default:
      // csv
      blobType = `text/${type}`;
  }
  const blob = new Blob([text], {
    type: blobType,
  });
  FileSaver.saveAs(blob, filename);
}
 
export function escapeSqlIdentifier(identifier: string): string {
  return `"${identifier.replace(/"/g, '""')}"`;
}
 
export function copyAndAlert(copyString: string, alertMessage: string): void {
  copy(copyString, { format: 'text/plain' });
  AppToaster.show({
    message: alertMessage,
    intent: Intent.SUCCESS,
  });
}
 
export function delay(ms: number) {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
}