@@ -2,6 +2,7 @@ import { type Data, type NdArray, type TypedArray } from 'ndarray';
22
33import {
44 type ArrayShape ,
5+ type BigIntegerType ,
56 type BooleanType ,
67 type ComplexArray ,
78 type ComplexType ,
@@ -30,20 +31,12 @@ import {
3031import {
3132 type AnyNumArray ,
3233 type AxisScaleType ,
34+ type BigIntTypedArray ,
3335 type ColorScaleType ,
3436 type NumArray ,
3537} from './vis-models' ;
3638import { AXIS_SCALE_TYPES , COLOR_SCALE_TYPES , getValues } from './vis-utils' ;
3739
38- const PRINTABLE_DTYPES = new Set ( [
39- DTypeClass . Integer ,
40- DTypeClass . Float ,
41- DTypeClass . String ,
42- DTypeClass . Bool ,
43- DTypeClass . Enum ,
44- DTypeClass . Complex ,
45- ] ) ;
46-
4740export function isAbsolutePath ( path : string ) : boolean {
4841 return path . startsWith ( '/' ) ;
4942}
@@ -86,6 +79,12 @@ function assertNum(val: unknown): asserts val is number {
8679 }
8780}
8881
82+ function assertNumOrBigint ( val : unknown ) : asserts val is number {
83+ if ( typeof val !== 'number' && typeof val !== 'bigint' ) {
84+ throw new TypeError ( 'Expected number' ) ;
85+ }
86+ }
87+
8988function assertNumOrBool ( val : unknown ) : asserts val is number | boolean {
9089 if ( typeof val !== 'number' && typeof val !== 'boolean' ) {
9190 throw new TypeError ( 'Expected boolean or number' ) ;
@@ -142,6 +141,10 @@ export function isTypedArray(val: unknown): val is TypedArray {
142141 ) ;
143142}
144143
144+ export function isBigIntTypeArray ( val : unknown ) : val is BigIntTypedArray {
145+ return val instanceof BigInt64Array || val instanceof BigUint64Array ;
146+ }
147+
145148export function isGroup ( entity : Entity ) : entity is Group {
146149 return entity . kind === EntityKind . Group ;
147150}
@@ -313,11 +316,23 @@ export function assertNumericType<S extends Shape>(
313316 }
314317}
315318
319+ export function isBigIntegerType ( type : DType ) : type is BigIntegerType {
320+ return type . class === DTypeClass . BigInteger ;
321+ }
322+
323+ export function isNumericLikeType ( type : DType ) : type is NumericLikeType {
324+ return (
325+ isNumericType ( type ) ||
326+ isBigIntegerType ( type ) ||
327+ isBoolType ( type ) ||
328+ isEnumType ( type )
329+ ) ;
330+ }
331+
316332export function hasNumericLikeType < S extends Shape > (
317333 dataset : Dataset < S > ,
318334) : dataset is Dataset < S , NumericLikeType > {
319- const { type } = dataset ;
320- return isNumericType ( type ) || isBoolType ( type ) || isEnumType ( type ) ;
335+ return isNumericLikeType ( dataset . type ) ;
321336}
322337
323338export function assertNumericLikeType < S extends Shape > (
@@ -360,22 +375,20 @@ export function assertNumericLikeOrComplexType<S extends Shape>(
360375 }
361376}
362377
378+ export function isPrintableType ( type : DType ) : type is PrintableType {
379+ return isStringType ( type ) || isNumericLikeType ( type ) || isComplexType ( type ) ;
380+ }
381+
363382export function hasPrintableType < S extends Shape > (
364- entity : Dataset < S > ,
365- ) : entity is Dataset < S , PrintableType > {
366- return PRINTABLE_DTYPES . has ( entity . type . class ) ;
383+ dataset : Dataset < S > ,
384+ ) : dataset is Dataset < S , PrintableType > {
385+ return isPrintableType ( dataset . type ) ;
367386}
368387
369388export function assertPrintableType < S extends Shape > (
370389 dataset : Dataset < S > ,
371390) : asserts dataset is Dataset < S , PrintableType > {
372- if (
373- ! hasStringType ( dataset ) &&
374- ! hasNumericType ( dataset ) &&
375- ! hasBoolType ( dataset ) &&
376- ! hasEnumType ( dataset ) &&
377- ! hasComplexType ( dataset )
378- ) {
391+ if ( ! hasPrintableType ( dataset ) ) {
379392 throw new Error ( 'Expected dataset to have displayable type' ) ;
380393 }
381394}
@@ -402,7 +415,7 @@ export function hasPrintableCompoundType<S extends Shape>(
402415 dataset : Dataset < S , CompoundType > ,
403416) : dataset is Dataset < S , CompoundType < PrintableType > > {
404417 const { fields } = dataset . type ;
405- return Object . values ( fields ) . every ( ( f ) => PRINTABLE_DTYPES . has ( f . class ) ) ;
418+ return Object . values ( fields ) . every ( isPrintableType ) ;
406419}
407420
408421export function assertPrintableCompoundType < S extends Shape > (
@@ -420,22 +433,24 @@ export function isComplexValue(
420433 return type . class === DTypeClass . Complex ;
421434}
422435
423- function assertPrimitiveValue (
436+ function assertScalarValue (
424437 type : DType ,
425438 value : unknown ,
426439) : asserts value is ScalarValue {
427440 if ( isNumericType ( type ) ) {
428441 assertNum ( value ) ;
429442 } else if ( isStringType ( type ) ) {
430443 assertStr ( value ) ;
444+ } else if ( isBigIntegerType ( type ) ) {
445+ assertNumOrBigint ( value ) ;
431446 } else if ( isBoolType ( type ) ) {
432447 assertNumOrBool ( value ) ;
433448 } else if ( isComplexType ( type ) ) {
434449 assertComplex ( value ) ;
435450 } else if ( isCompoundType ( type ) ) {
436451 assertArray ( value ) ;
437452 Object . values ( type . fields ) . forEach ( ( fieldType , index ) => {
438- assertPrimitiveValue ( fieldType , value [ index ] ) ;
453+ assertScalarValue ( fieldType , value [ index ] ) ;
439454 } ) ;
440455 }
441456}
@@ -447,14 +462,18 @@ export function assertDatasetValue<D extends Dataset<ScalarShape | ArrayShape>>(
447462 const { type } = dataset ;
448463
449464 if ( hasScalarShape ( dataset ) ) {
450- assertPrimitiveValue ( type , value ) ;
465+ assertScalarValue ( type , value ) ;
451466 } else {
452- if ( ! Array . isArray ( value ) && ! isTypedArray ( value ) ) {
467+ if (
468+ ! Array . isArray ( value ) &&
469+ ! isTypedArray ( value ) &&
470+ ! isBigIntTypeArray ( value )
471+ ) {
453472 throw new TypeError ( 'Expected array or typed array' ) ;
454473 }
455474
456475 if ( value . length > 0 ) {
457- assertPrimitiveValue ( type , value [ 0 ] ) ;
476+ assertScalarValue ( type , value [ 0 ] ) ;
458477 }
459478 }
460479}
0 commit comments