util.ts 2.6 KB
Newer Older
1
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
R
Ryan Dahl 已提交
2
import { TypedArray } from "./types";
R
Ryan Dahl 已提交
3

R
Ryan Dahl 已提交
4
let logDebug = false;
R
Ryan Dahl 已提交
5

6
// @internal
R
Ryan Dahl 已提交
7 8 9
export function setLogDebug(debug: boolean): void {
  logDebug = debug;
}
10

11
/**
P
Parsa Ghadimi 已提交
12
 * Debug logging for deno.
迷渡's avatar
迷渡 已提交
13
 * Enable with the `--log-debug` or `-D` command line flag.
14 15
 * @internal
 */
16 17
// tslint:disable-next-line:no-any
export function log(...args: any[]): void {
R
Ryan Dahl 已提交
18 19
  if (logDebug) {
    console.log("DEBUG JS -", ...args);
20 21 22
  }
}

23
// @internal
R
Ryan Dahl 已提交
24
export function assert(cond: boolean, msg = "assert") {
25
  if (!cond) {
R
Ryan Dahl 已提交
26
    throw Error(msg);
27 28 29
  }
}

30
// @internal
31 32 33 34 35
export function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
  const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
  return ab as ArrayBuffer;
}

36
// @internal
37 38 39 40
export function arrayToStr(ui8: Uint8Array): string {
  return String.fromCharCode(...ui8);
}

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/**
 * A `Resolvable` is a Promise with the `reject` and `resolve` functions
 * placed as methods on the promise object itself. It allows you to do:
 *
 *     const p = createResolvable<number>();
 *     ...
 *     p.resolve(42);
 *
 * It'd be prettier to make Resolvable a class that inherits from Promise,
 * rather than an interface. This is possible in ES2016, however typescript
 * produces broken code when targeting ES5 code.
 * See https://github.com/Microsoft/TypeScript/issues/15202
 * At the time of writing, the github issue is closed but the problem remains.
 *
 * @internal
 */

K
Kitson Kelly 已提交
58
export interface ResolvableMethods<T> {
59 60 61 62
  resolve: (value?: T | PromiseLike<T>) => void;
  // tslint:disable-next-line:no-any
  reject: (reason?: any) => void;
}
K
Kitson Kelly 已提交
63

64
// @internal
R
Ryan Dahl 已提交
65
export type Resolvable<T> = Promise<T> & ResolvableMethods<T>;
K
Kitson Kelly 已提交
66

67
// @internal
68
export function createResolvable<T>(): Resolvable<T> {
K
Kitson Kelly 已提交
69
  let methods: ResolvableMethods<T>;
70 71 72
  const promise = new Promise<T>((resolve, reject) => {
    methods = { resolve, reject };
  });
K
Kitson Kelly 已提交
73 74 75
  // TypeScript doesn't know that the Promise callback occurs synchronously
  // therefore use of not null assertion (`!`)
  return Object.assign(promise, methods!) as Resolvable<T>;
76
}
77

78
// @internal
79 80 81 82
export function notImplemented(): never {
  throw new Error("Not implemented");
}

83
// @internal
84 85 86
export function unreachable(): never {
  throw new Error("Code not reachable");
}
R
Ryan Dahl 已提交
87

P
Parsa Ghadimi 已提交
88
// @internal
R
Ryan Dahl 已提交
89 90 91 92 93 94 95
export function hexdump(u8: Uint8Array): string {
  return Array.prototype.map
    .call(u8, (x: number) => {
      return ("00" + x.toString(16)).slice(-2);
    })
    .join(" ");
}
P
Parsa Ghadimi 已提交
96 97 98 99 100 101 102 103

// @internal
export function containsOnlyASCII(str: string): boolean {
  if (typeof str !== "string") {
    return false;
  }
  return /^[\x00-\x7F]*$/.test(str);
}