Public
Edited
Jul 24, 2023
Importers
Insert cell
Insert cell
Insert cell
typeOf(document.all)
Insert cell
function typeOf(value) {
if (value === null) { return "null"; }
if (value === undefined) { return "undefined"; }
let t = typeof value;
if (t === "undefined") { return "object"; }
return t;
}
Insert cell
Insert cell
function truthy(value) {
if (value === null || value === undefined) { return false; }
return ("undefined" === typeof value) || !!value;
}
Insert cell
Insert cell
function falsy(value) { return !truthy(value); }
Insert cell
Insert cell
function ternary(value) {
if (value === null || value === undefined) { return null; }
return truthy(value);
}
Insert cell
Insert cell
function nullish(value) {
switch (typeOf(value)) {
case "null":
case "undefined":
return true;
}
return false;
}
Insert cell
Insert cell
function isPrimitive(value) {
if (arguments.length != 1) { throw new SyntaxError(`Exactly one argument is expected for ${isPrimitive.name}`); }
switch (typeOf(value)) {
case "object":
case "function":
return false;
}
return true;
}
Insert cell
isClass(String, new String(NaN))
Insert cell
isClass.proto.String("hello")
Insert cell
new Promise((accept, reject) => {
let gen = Generators.worker(String.raw`globalThis.addEventListener("message", ({data}) => postMessage(eval(String(data))))`);
gen.return && invalidation.then(() => gen.return());
let worker = gen[Symbol.iterator]().next().value;
worker.addEventListener("message", ({data}) => accept(data), {passive: true, once: true});
worker.postMessage(`[]`);
invalidation.then(reject);
})
Insert cell
Insert cell
isClass = {
const getProto = globalThis.Reflect?.getPrototypeOf
? globalThis.Reflect.getPrototypeOf.bind(globalThis.Reflect)
: Object.getPrototypeOf.bind(Object);

const setDesc = globalThis.Reflect?.defineProperty
? globalThis.Reflect.defineProperty.bind(globalThis.Reflect)
: Object.defineProperty.bind(Object);

const getOwnStringKeys = globalThis.Reflect?.ownKeys
? globalThis.Reflect.ownKeys.bind(globalThis.Reflect)
: Object.getOwnPropertyNames.bind(Object);

setDesc(isClass, "proto", {
enumerable: true,
value: isClassOrProto
});

for (let name of "BigInt Boolean Function Number Object String Symbol".split(/\s+/)) {
if (typeof globalThis[name] === "function" && typeOf(globalThis[name]) === "function") {
let f = function(value) {
return this(name, value);
}
setDesc(f, "name", {
enumerable: true,
configurable: true,
get: () => `is${name}`
});
isClass[name] || setDesc(isClass, name, {
enumerable: true,
value: f
});
isClassOrProto[name] || setDesc(isClassOrProto, name, {
enumerable: true,
value: f
});
}
}

return isClass;

// const protoNames = new Set("bigint boolean null number string symbol undefined".split(/\s+/));
// const realms = new WeakMap();
function isClassOrProto(className, value) {
let t = typeOf(value);
switch (t) {
case "function":
case "object":
return isClass(className, value);
}
return classToString(className).toLowerCase() === typeOf(value);
}

function isClass(className, value) {
switch (typeOf(value)) {
case "function":
case "object":
break;
default:
return false;
}
if (className === null) { return getProto(value) === null; }
if (getProto(value) === null) { return false; }
className = classToString(className);
let global;
if (value instanceof {}.constructor) {
// this realm
global = globalThis;
} else {
// other realm
let v, p;
v = value;
while (true) {
p = getProto(v);
if (p === null) { break; }
v = p;
}
let Object = v.constructor;
global = new (Object.getPrototypeOf(Object).constructor)(`return globalThis ?? this;`)();
if (global.globalThis !== global) { return null; }
}
let targetProto, p;
if (className in global && isObj(global[className]) && value instanceof global[className]) {
targetProto = global[className].prototype;
if ((p = getProto(value)) === targetProto) { return true; }
while (p !== null) {
p = getProto(p);
if (p === targetProto) { return true; }
}
}
return false;
}

function classToString(className) {
return typeof className === "string"
? className
: (className = String(typeof className === "function" ? (className.name || className) : className));
}

function isObj(value) {
switch (typeOf(value)) {
case "object":
case "function":
return true;
}
return false;
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more