Object
size

Returns the size of the given collection.

⚠️

It returns -1 if the value is an unsupported type.

Syntax

import { size } from '@opentf/std';
 
size(val: unknown): number;

Examples

// Invalid objs
// ************
 
size() //=> -1
 
size(undefined) //=> -1
 
size(null) //=> -1
 
size(1) //=> -1
 
size(1.5) //=> -1
 
size(1n) //=> -1
 
size(false) //=> -1
 
size(true) //=> -1
 
// Eempty objs
// ***********
 
size('') //=> 0
 
size([]) //=> 0
 
size({}) //=> 0
 
size(new Map()) //=> 0
 
size(new Set()) //=> 0
 
size(new ArrayBuffer(0)) //=> 0
 
size(new Uint8Array(0)) //=> 0
 
// Non empty objs
// **************
 
size(' ') //=> 1
 
size('abc') //=> 3
 
size([1]) //=> 1
 
size({ length: 0, size: 0, byteLength: 0 }) //=> 3
 
size(new Map([[1, 1]])) //=> 1
 
size(new Set(['a', 'b'])) //=> 2
 
size(new ArrayBuffer(8)) //=> 8
 
size(new Uint8Array(10)) //=> 10
 
size(new Uint8Array(new ArrayBuffer(10), 1, 5)) //=> 5
 
const buffer = new ArrayBuffer(16);
size(new DataView(buffer)) //=> 16

Try