jsr-core/unknownutil

feat: add predicate functions of basic types like `Promise`, `Date`, `Iterable`, etc.

lambdalisue opened this issue ยท 6 comments

We can use isInstanceOf or isObjectOf now but for convinience.

  • Promise
  • Date
  • Temporal
  • Worker
  • Iterable
  • AsyncIterable
  • Disposable
  • AsyncDisposable
  • etc.

Are they added under is?
Like:

import { isPromise } from "@core/unknownutil/is/promise";
import { is } from "@core/unknownutil/is";

const x: unknown = 0;
isPromise(x);
is.Promise(x);
type Jsonable = {
  /**
   * Returns a JSON value that can be specified to {@linkcode JSON.stringify}.
   *
   * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior|toJSON() behavior}
   */
  toJSON(key: string | number): unknown;
};

function isJsonable(x: unknown): x is Jsonable {
  return x != null && isFunction((x as Jsonable).toJSON);
}

๐Ÿ’ญ I wonder if we define this in unknownutil.

Originally posted by @lambdalisue in vim-denops/deno-denops-std#260 (comment)

const isInstanceOfBoolean = isInstanceOf(Boolean);
const isInstanceOfNumber = isInstanceOf(Number);
const isInstanceOfString = isInstanceOf(String);

๐Ÿ’ญ I wonder if we define these in unknownutil like isBooleanInstance, etc.

Originally posted by @lambdalisue in vim-denops/deno-denops-std#260 (comment)

type Jsonable = {

  /**

   * Returns a JSON value that can be specified to {@linkcode JSON.stringify}.

   *

   * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior|toJSON() behavior}

   */

  toJSON(key: string | number): unknown;

};



function isJsonable(x: unknown): x is Jsonable {

  return x != null && isFunction((x as Jsonable).toJSON);

}

๐Ÿ’ญ I wonder if we define this in unknownutil.

Originally posted by @lambdalisue in vim-denops/deno-denops-std#260 (comment)

I think null is jsonable

I think null is jsonable

Discussion and implementation of isJsonable in #128.

I changed my mind. It's impossible to define "what is the bacis types".