@bokeh/bokehjs
Version:
Interactive, novel data visualization
682 lines • 20.3 kB
JavaScript
import * as tp from "./util/types";
import { is_Color } from "./util/color";
import { keys, values, entries, typed_values, typed_entries, is_empty, PlainObjectProxy } from "./util/object";
import { has_refs } from "./util/refs";
const ESMap = globalThis.Map;
const ESSet = globalThis.Set;
const DOMNode = globalThis.Node;
export class Kind {
static __name__;
get kind_name() {
return this.constructor.__name__;
}
get kind_args() {
return [];
}
toString() {
const { kind_name, kind_args } = this;
if (kind_args.length == 0) {
return kind_name;
}
else {
return `${kind_name}(${kind_args.map((arg) => `${arg}`).join(", ")})`;
}
}
}
export var Kinds;
(function (Kinds) {
class Primitive extends Kind {
static __name__ = "Primitive";
may_have_refs() {
return false;
}
}
Kinds.Primitive = Primitive;
class Any extends Primitive {
static __name__ = "Any";
valid(value) {
return value !== undefined;
}
may_have_refs() {
return true;
}
}
Kinds.Any = Any;
class Unknown extends Primitive {
static __name__ = "Unknown";
valid(value) {
return value !== undefined;
}
may_have_refs() {
return true;
}
}
Kinds.Unknown = Unknown;
class Bool extends Primitive {
static __name__ = "Bool";
valid(value) {
return tp.isBoolean(value);
}
}
Kinds.Bool = Bool;
class Ref extends Kind {
obj_type;
static __name__ = "Ref";
constructor(obj_type) {
super();
this.obj_type = obj_type;
}
valid(value) {
return value instanceof this.obj_type;
}
get type_name() {
// NOTE: `__name__` is injected by a compiler transform
const tp = this.obj_type;
return tp.__name__ ?? tp.toString();
}
toString() {
return `${this.kind_name}(${this.type_name})`;
}
may_have_refs() {
const { obj_type } = this;
return has_refs in obj_type ? obj_type[has_refs] : true;
}
}
Kinds.Ref = Ref;
class AnyRef extends Kind {
static __name__ = "AnyRef";
valid(value) {
return tp.isObject(value);
}
may_have_refs() {
return true;
}
}
Kinds.AnyRef = AnyRef;
class Float extends Primitive {
static __name__ = "Float";
valid(value) {
return tp.isNumber(value);
}
}
Kinds.Float = Float;
class Int extends Float {
static __name__ = "Int";
valid(value) {
return super.valid(value) && tp.isInteger(value);
}
}
Kinds.Int = Int;
class Percent extends Float {
static __name__ = "Percent";
valid(value) {
return super.valid(value) && 0 <= value && value <= 1;
}
}
Kinds.Percent = Percent;
class Or extends Kind {
types;
static __name__ = "Or";
constructor(types) {
super();
this.types = types;
this.types = types;
}
valid(value) {
return this.types.some((type) => type.valid(value));
}
get kind_args() {
return this.types;
}
may_have_refs() {
return this.types.some((type) => type.may_have_refs());
}
}
Kinds.Or = Or;
class And extends Kind {
static __name__ = "And";
types;
constructor(type0, type1) {
super();
this.types = [type0, type1];
}
valid(value) {
return this.types.some((type) => type.valid(value)); // TODO not sure if this is correct, probably not
}
get kind_args() {
return this.types;
}
may_have_refs() {
return this.types.some((type) => type.may_have_refs());
}
}
Kinds.And = And;
class Tuple extends Kind {
types;
static __name__ = "Tuple";
constructor(types) {
super();
this.types = types;
this.types = types;
}
valid(value) {
if (!tp.isArray(value)) {
return false;
}
for (let i = 0; i < this.types.length; i++) {
const type = this.types[i];
const item = value[i];
if (!type.valid(item)) {
return false;
}
}
return true;
}
get kind_args() {
return this.types;
}
may_have_refs() {
return this.types.some((type) => type.may_have_refs());
}
}
Kinds.Tuple = Tuple;
class Struct extends Kind {
struct_type;
static __name__ = "Struct";
constructor(struct_type) {
super();
this.struct_type = struct_type;
}
valid(value) {
if (!tp.isPlainObject(value)) {
return false;
}
const struct_type_proxy = new PlainObjectProxy(this.struct_type);
for (const key of keys(value)) {
if (!struct_type_proxy.has(key)) {
return false;
}
}
for (const [key, item_type] of struct_type_proxy) {
const item = value[key];
if (!item_type.valid(item)) {
return false;
}
}
return true;
}
toString() {
const items = typed_entries(this.struct_type).map(([key, kind]) => `${key.toString()}: ${kind}`).join(", ");
return `${this.kind_name}({${items}})`;
}
may_have_refs() {
return typed_values(this.struct_type).some((kind) => kind.may_have_refs());
}
}
Kinds.Struct = Struct;
class PartialStruct extends Kind {
struct_type;
static __name__ = "PartialStruct";
constructor(struct_type) {
super();
this.struct_type = struct_type;
}
valid(value) {
if (!tp.isPlainObject(value)) {
return false;
}
const value_proxy = new PlainObjectProxy(value);
const struct_type_proxy = new PlainObjectProxy(this.struct_type);
for (const key of value_proxy.keys()) {
if (!struct_type_proxy.has(key)) {
return false;
}
}
for (const [key, item_type] of struct_type_proxy) {
const item = value_proxy.get(key);
if (item === undefined) {
continue;
}
if (!item_type.valid(item)) {
return false;
}
}
return true;
}
toString() {
const items = typed_entries(this.struct_type).map(([key, kind]) => `${key.toString()}?: ${kind}`).join(", ");
return `${this.kind_name}({${items}})`;
}
may_have_refs() {
return typed_values(this.struct_type).some((kind) => kind.may_have_refs());
}
}
Kinds.PartialStruct = PartialStruct;
class Iterable extends Kind {
item_type;
static __name__ = "Iterable";
constructor(item_type) {
super();
this.item_type = item_type;
}
valid(value) {
return tp.isIterable(value);
}
get kind_args() {
return [this.item_type];
}
may_have_refs() {
return this.item_type.may_have_refs();
}
}
Kinds.Iterable = Iterable;
class Arrayable extends Kind {
item_type;
static __name__ = "Arrayable";
constructor(item_type) {
super();
this.item_type = item_type;
}
valid(value) {
return tp.isArray(value) || tp.isTypedArray(value); // TODO: too specific
}
get kind_args() {
return [this.item_type];
}
may_have_refs() {
return this.item_type.may_have_refs();
}
}
Kinds.Arrayable = Arrayable;
class List extends Kind {
item_type;
static __name__ = "List";
constructor(item_type) {
super();
this.item_type = item_type;
}
valid(value) {
return tp.isArray(value) && value.every((item) => this.item_type.valid(item));
}
get kind_args() {
return [this.item_type];
}
may_have_refs() {
return this.item_type.may_have_refs();
}
}
Kinds.List = List;
class NonEmptyList extends List {
static __name__ = "NonEmptyList";
valid(value) {
return super.valid(value) && value.length != 0;
}
get kind_args() {
return [this.item_type];
}
}
Kinds.NonEmptyList = NonEmptyList;
class Null extends Primitive {
static __name__ = "Null";
valid(value) {
return value === null;
}
}
Kinds.Null = Null;
class Nullable extends Kind {
base_type;
static __name__ = "Nullable";
constructor(base_type) {
super();
this.base_type = base_type;
}
valid(value) {
return value === null || this.base_type.valid(value);
}
get kind_args() {
return [this.base_type];
}
may_have_refs() {
return this.base_type.may_have_refs();
}
}
Kinds.Nullable = Nullable;
class Opt extends Kind {
base_type;
static __name__ = "Opt";
constructor(base_type) {
super();
this.base_type = base_type;
}
valid(value) {
return value === undefined || this.base_type.valid(value);
}
get kind_args() {
return [this.base_type];
}
may_have_refs() {
return this.base_type.may_have_refs();
}
}
Kinds.Opt = Opt;
class Bytes extends Kind {
static __name__ = "Bytes";
valid(value) {
return value instanceof ArrayBuffer;
}
may_have_refs() {
return false;
}
}
Kinds.Bytes = Bytes;
class Str extends Primitive {
static __name__ = "Str";
valid(value) {
return tp.isString(value);
}
}
Kinds.Str = Str;
class PrefixedStr extends Primitive {
prefix;
static __name__ = "PrefixedStr";
constructor(prefix) {
super();
this.prefix = prefix;
}
valid(value) {
return tp.isString(value) && value.startsWith(this.prefix);
}
get kind_args() {
return [this.prefix];
}
}
Kinds.PrefixedStr = PrefixedStr;
class Regex extends Str {
regex;
static __name__ = "Regex";
constructor(regex) {
super();
this.regex = regex;
}
valid(value) {
return super.valid(value) && this.regex.test(value);
}
get kind_args() {
return [this.regex];
}
}
Kinds.Regex = Regex;
class Enum extends Primitive {
static __name__ = "Enum";
values;
constructor(values) {
super();
this.values = new ESSet(values);
}
valid(value) {
return this.values.has(value);
}
*[Symbol.iterator]() {
yield* this.values;
}
get kind_args() {
return [...this.values];
}
}
Kinds.Enum = Enum;
class Dict extends Kind {
item_type;
static __name__ = "Dict";
constructor(item_type) {
super();
this.item_type = item_type;
}
valid(value) {
if (!(value instanceof ESMap || tp.isPlainObject(value))) {
return false;
}
for (const item of values(value)) {
if (!this.item_type.valid(item)) {
return false;
}
}
return true;
}
get kind_args() {
return [this.item_type];
}
may_have_refs() {
return this.item_type.may_have_refs();
}
}
Kinds.Dict = Dict;
class KeyVal extends Kind {
key_type;
item_type;
static __name__ = "KeyVal";
constructor(key_type, item_type) {
super();
this.key_type = key_type;
this.item_type = item_type;
}
valid(value) {
if (!(value instanceof ESMap || tp.isPlainObject(value))) {
return false;
}
for (const [key, item] of entries(value)) {
if (!this.key_type.valid(key) || !this.item_type.valid(item)) {
return false;
}
}
return true;
}
get kind_args() {
return [this.key_type, this.item_type];
}
may_have_refs() {
return this.key_type.may_have_refs() || this.item_type.may_have_refs();
}
}
Kinds.KeyVal = KeyVal;
class Mapping extends Kind {
key_type;
item_type;
static __name__ = "Mapping";
constructor(key_type, item_type) {
super();
this.key_type = key_type;
this.item_type = item_type;
}
coerce(value) {
// HACK accommodate for deserialization of {type: "map"}
if (tp.isPlainObject(value) && is_empty(value)) {
return new ESMap();
}
else {
return value;
}
}
valid(value) {
if (!(value instanceof ESMap)) {
return false;
}
for (const [key, item] of value.entries()) {
if (!(this.key_type.valid(key) && this.item_type.valid(item))) {
return false;
}
}
return true;
}
get kind_args() {
return [this.key_type, this.item_type];
}
may_have_refs() {
return this.key_type.may_have_refs() || this.item_type.may_have_refs();
}
}
Kinds.Mapping = Mapping;
class Set extends Kind {
item_type;
static __name__ = "Set";
constructor(item_type) {
super();
this.item_type = item_type;
}
valid(value) {
if (!(value instanceof ESSet)) {
return false;
}
for (const item of value) {
if (!this.item_type.valid(item)) {
return false;
}
}
return true;
}
get kind_args() {
return [this.item_type];
}
may_have_refs() {
return this.item_type.may_have_refs();
}
}
Kinds.Set = Set;
class Color extends Kind {
static __name__ = "Color";
valid(value) {
return is_Color(value);
}
may_have_refs() {
return false;
}
}
Kinds.Color = Color;
class CSSLength extends Str {
static __name__ = "CSSLength";
}
Kinds.CSSLength = CSSLength;
class Func extends Kind {
args_types;
ret_type;
static __name__ = "Func";
constructor(args_types, ret_type) {
super();
this.args_types = args_types;
this.ret_type = ret_type;
}
valid(value) {
return tp.isFunction(value);
}
toString() {
const { args_types, ret_type } = this;
const args = args_types == null ? "?" : args_types.map((type) => type.toString()).join(", ");
const ret = ret_type == null ? "?" : ret_type.toString();
return `${this.kind_name}((${args}), ${ret})`;
}
may_have_refs() {
return false;
}
}
Kinds.Func = Func;
class NonNegative extends Kind {
base_type;
static __name__ = "NonNegative";
constructor(base_type) {
super();
this.base_type = base_type;
}
valid(value) {
return this.base_type.valid(value) && value >= 0;
}
get kind_args() {
return [this.base_type];
}
may_have_refs() {
return this.base_type.may_have_refs();
}
}
Kinds.NonNegative = NonNegative;
class Positive extends Kind {
base_type;
static __name__ = "Positive";
constructor(base_type) {
super();
this.base_type = base_type;
}
valid(value) {
return this.base_type.valid(value) && value > 0;
}
get kind_args() {
return [this.base_type];
}
may_have_refs() {
return this.base_type.may_have_refs();
}
}
Kinds.Positive = Positive;
class Node extends Kind {
static __name__ = "Node";
valid(value) {
return value instanceof DOMNode;
}
may_have_refs() {
return false;
}
}
Kinds.Node = Node;
})(Kinds || (Kinds = {}));
export const Any = new Kinds.Any();
export const Unknown = new Kinds.Unknown();
export const Bool = new Kinds.Bool();
export const Float = new Kinds.Float();
export const Int = new Kinds.Int();
export const Bytes = new Kinds.Bytes();
export const Str = new Kinds.Str();
export const PrefixedStr = (prefix) => new Kinds.PrefixedStr(prefix);
export const Regex = (regex) => new Kinds.Regex(regex);
export const Null = new Kinds.Null();
export const Nullable = (base_type) => new Kinds.Nullable(base_type);
export const Opt = (base_type) => new Kinds.Opt(base_type);
export const Or = (...types) => new Kinds.Or(types);
export const And = (type0, type1) => new Kinds.And(type0, type1);
export const Tuple = (...types) => new Kinds.Tuple(types);
export const Struct = (struct_type) => new Kinds.Struct(struct_type);
export const PartialStruct = (struct_type) => new Kinds.PartialStruct(struct_type);
export const Iterable = (item_type) => new Kinds.Iterable(item_type);
export const Arrayable = (item_type) => new Kinds.Arrayable(item_type);
export const List = (item_type) => new Kinds.List(item_type);
export const NonEmptyList = (item_type) => new Kinds.NonEmptyList(item_type);
export const Dict = (item_type) => new Kinds.Dict(item_type);
export const KeyVal = (key_type, item_type) => new Kinds.KeyVal(key_type, item_type);
export const Mapping = (key_type, item_type) => new Kinds.Mapping(key_type, item_type);
export const Set = (item_type) => new Kinds.Set(item_type);
export const Enum = (...values) => new Kinds.Enum(values);
export const Ref = (obj_type) => new Kinds.Ref(obj_type);
export const AnyRef = () => new Kinds.AnyRef();
export const Func = (args_types, ret_type) => new Kinds.Func(args_types, ret_type);
export const Func0 = (ret_type) => new Kinds.Func([], ret_type);
export const Node = new Kinds.Node();
export const NonNegative = (base_type) => new Kinds.NonNegative(base_type);
export const Positive = (base_type) => new Kinds.Positive(base_type);
export const Percent = new Kinds.Percent();
export const Alpha = Percent;
export const Color = new Kinds.Color();
export const Auto = Enum("auto");
export const CSSLength = new Kinds.CSSLength();
export const FontSize = Str;
export const Font = Str;
export const Angle = Float;
// backwards compatibility aliases (these collide with built-in types)
/** @deprecated */
export const Boolean = Bool;
/** @deprecated */
export const String = Str;
/** @deprecated */
export const Number = Float;
/** @deprecated */
export const Array = List;
/** @deprecated */
export const Map = Mapping;
/** @deprecated */
export const Function = Func;
//# sourceMappingURL=kinds.js.map