import { DEFAULT_OPTIONS } from './constants' ; import { Expiration , Fn , IsEqual , IsMatchingKey , Key , Moizeable , Moized , Options , } from './types' ; private description param functions returns export function combine < Arg , Result > ( ... functions ) { return functions . reduce ( function ( f , g ) { if ( typeof f === 'function' ) { return typeof g === 'function' ? function ( this ) { f . apply ( this , arguments ) ; g . apply ( this , arguments ) ; } : f ; } if ( typeof g === 'function' ) { return g ; } } ) ; } private description param functions returns export function compose < Method > ( ... functions ) { return functions . reduce ( function ( f , g ) { if ( typeof f === 'function' ) { return typeof g === 'function' ? function ( this ) { return f ( g . apply ( this , arguments ) ) ; } : f ; } if ( typeof g === 'function' ) { return g ; } } ) ; } private description param expirations param key returns export function findExpirationIndex ( expirations , key ) { for ( let index = 0 ; index < expirations . length ; index ++ ) { if ( expirations [ index ] . key === key ) { return index ; } } return - 1 ; } private description param isEqual param isMatchingKey returns export function createFindKeyIndex ( isEqual , isMatchingKey ) { const areKeysEqual = typeof isMatchingKey === 'function' ? isMatchingKey : function ( cacheKey , key ) { for ( let index = 0 ; index < key . length ; index ++ ) { if ( ! isEqual ( cacheKey [ index ] , key [ index ] ) ) { return false ; } } return true ; } ; return function ( keys , key ) { for ( let keysIndex = 0 ; keysIndex < keys . length ; keysIndex ++ ) { if ( keys [ keysIndex ] . length === key . length && areKeysEqual ( keys [ keysIndex ] , key ) ) { return keysIndex ; } } return - 1 ; } ; } private description param originalOptions param newOptions returns export function mergeOptions ( originalOptions , newOptions ) { return ! newOptions || newOptions === DEFAULT_OPTIONS ? originalOptions : { ... originalOptions , ... newOptions , onCacheAdd : combine ( originalOptions . onCacheAdd , newOptions . onCacheAdd ) , onCacheChange : combine ( originalOptions . onCacheChange , newOptions . onCacheChange ) , onCacheHit : combine ( originalOptions . onCacheHit , newOptions . onCacheHit ) , transformArgs : compose ( originalOptions . transformArgs , newOptions . transformArgs ) , } ; } export function isMoized ( fn ) { return typeof fn === 'function' && ( fn as Moizeable ) . isMoized ; } export function setName ( fn , originalFunctionName , profileName ) { try { const name = profileName || originalFunctionName || 'anonymous' ; Object . defineProperty ( fn , 'name' , { configurable : true , enumerable : false , value : `moized(${ name })` , writable : true , } ) ; } catch { } }