license import * as tf from '@tensorflow/tfjs-core' ; /**
 * A return value for a mapping function that can be applied via deepMap.
 *
 * If recurse is true, the value should be empty, and iteration will continue
 * into the object or array.
 */ export type DeepMapResult = { value : any ; recurse : boolean ; } ; param input param mapFn export function deepMap ( input , mapFn ) { return deepMapInternal ( input , mapFn ) ; } param seen param containedIn function deepMapInternal ( input , mapFn , seen = new Map ( ) , containedIn = new Set ( ) ) { if ( input == null ) { return null ; } if ( typeof Blob === 'function' && input instanceof Blob ) { return input . slice ( ) ; } if ( containedIn . has ( input ) ) { throw new Error ( 'Circular references are not supported.' ) ; } if ( seen . has ( input ) ) { return seen . get ( input ) ; } const result = mapFn ( input ) ; if ( result . recurse && result . value !== null ) { throw new Error ( 'A deep map function may not return both a value and recurse=true.' ) ; } if ( ! result . recurse ) { seen . set ( input , result . value ) ; return result . value ; } else if ( isIterable ( input ) ) { const mappedIterable = Array . isArray ( input ) ? [ ] : { } ; containedIn . add ( input ) ; for ( const k in input ) { const child = input [ k ] ; const childResult = deepMapInternal ( child , mapFn , seen , containedIn ) ; mappedIterable [ k ] = childResult ; } containedIn . delete ( input ) ; if ( input . __proto__ ) { mappedIterable . __proto__ = input . __proto__ ; } return mappedIterable ; } else { throw new Error ( `Can't recurse into non-iterable type: ${ input }` ) ; } } param inputs param zipFn export function deepZip ( inputs , zipFn = zipToList ) { return deepZipInternal ( inputs , zipFn ) ; } param containedIn function deepZipInternal ( inputs , zipFn , containedIn = new Set ( ) ) { const input = inputs [ 0 ] ; if ( containedIn . has ( input ) ) { throw new Error ( 'Circular references are not supported.' ) ; } const result = zipFn ( inputs ) ; if ( result . recurse && result . value !== null ) { throw new Error ( 'A deep zip function may not return both a value and recurse=true.' ) ; } if ( ! result . recurse ) { return result . value ; } else if ( isIterable ( input ) ) { const mappedIterable = Array . isArray ( input ) ? [ ] : { } ; containedIn . add ( input ) ; for ( const k in input ) { const children = inputs . map ( x => x [ k ] ) ; const childResult = deepZipInternal ( children , zipFn , containedIn ) ; mappedIterable [ k ] = childResult ; } containedIn . delete ( input ) ; return mappedIterable ; } else { throw new Error ( `Can't recurse into non-iterable type: ${ input }` ) ; } } export function zipToList ( x ) { if ( x === null ) { return null ; } if ( isIterable ( x [ 0 ] ) ) { return { value : null , recurse : true } ; } else { return { value : x , recurse : false } ; } } /**
 * A return value for an async map function for use with deepMapAndAwaitAll.
 *
 * If recurse is true, the value should be empty, and iteration will continue
 * into the object or array.
 */ export type DeepMapAsyncResult = { value : Promise < any > ; recurse : boolean ; } ; param input param mapFn export async function deepMapAndAwaitAll ( input , mapFn ) { const seen = new Map ( ) ; deepMapInternal ( input , mapFn , seen ) ; for ( const key of Array . from ( seen . keys ( ) ) ) { const value = seen . get ( key ) ; if ( tf . util . isPromise ( value ) ) { const mappedValue = await value ; seen . set ( key , mappedValue ) ; } } const result = deepMapInternal ( input , mapFn , seen ) ; return result ; } returns export function isIterable ( obj ) { let isTextDecoder = false ; if ( tf . env ( ) . get ( 'IS_BROWSER' ) ) { isTextDecoder = obj instanceof TextDecoder ; } else { const { StringDecoder } = require ( 'string_decoder' ) ; isTextDecoder = obj instanceof StringDecoder ; } return obj != null && ( ! ArrayBuffer . isView ( obj ) ) && ( Array . isArray ( obj ) || ( typeof obj === 'object' && ! ( obj instanceof tf . Tensor ) && ! ( obj instanceof Promise ) && ! isTextDecoder ) ) ; } returns export function canTensorify ( obj ) { return obj == null || isPrimitive ( obj ) || Array . isArray ( obj ) || ( typeof obj === 'object' && ( obj instanceof tf . Tensor ) ) || tf . util . isTypedArray ( obj ) ; } /**
 * Returns true if the given `value` is a primitive type. Otherwise returns
 * false. This is equivalant to node util.isPrimitive
 */ function isPrimitive ( value ) { return ( value === null || ( typeof value !== 'object' && typeof value !== 'function' ) ) ; }