import { KeyStyle } from '../core/types' ; import { ROOT_KEY } from './flat' ; import { Node , LocaleTree , LocaleNode , LocaleRecord , Config } from '~/core' ; export function caseInsensitiveMatch ( a , b ) { return a . toUpperCase ( ) === b . toUpperCase ( ) ; } export function getKeyname ( keypath ) { const keys = keypath . split ( /\./g ) ; if ( ! keys . length ) return '' ; return keys [ keys . length - 1 ] ; } export function notEmpty < T > ( value ) { return value !== null && value !== undefined ; } export function escapeMarkdown ( text ) { return text . replace ( /\|/g , '\\|' ) . replace ( /\[/g , '\\[' ) . replace ( /\]/g , '\\]' ) . replace ( /\(/g , '\\(' ) . replace ( /\)/g , '\\)' ) ; } export function resolveFlattenRootKeypath ( keypath ) { if ( keypath . endsWith ( ROOT_KEY ) ) keypath = keypath . slice ( 0 , - ROOT_KEY . length ) ; if ( keypath . endsWith ( '.' ) ) keypath = keypath . slice ( 0 , - 1 ) ; return keypath ; } export function resolveFlattenRoot ( node ) ; export function resolveFlattenRoot ( node ) ; export function resolveFlattenRoot ( node ) ; export function resolveFlattenRoot ( node ? ) ; export function resolveFlattenRoot ( node ? ) { if ( node ?. type === 'tree' && node . getChild ( ROOT_KEY ) ?. type === 'node' ) return node . getChild ( ROOT_KEY ) ; return node ; } export function set ( obj , key , value , override = true ) { if ( Array . isArray ( obj ) ) { const num = parseInt ( key ) ; if ( ! isNaN ( num ) ) { if ( override || obj [ num ] == null ) obj [ num ] = value ; return obj [ num ] ; } } if ( override || obj [ key ] == null ) obj [ key ] = value ; return obj [ key ] ; } export function setNested ( obj , keys , value ) { if ( ! keys . length ) return ; const key = keys [ 0 ] ; if ( keys . length === 1 ) set ( obj , key , value ) ; else setNested ( set ( obj , key , { } , false ) , keys . slice ( 1 ) , value ) ; } export function applyPendingToObject ( obj , keypath , value , keyStyle ? ) { if ( ! Config . disablePathParsing ) keypath = resolveFlattenRootKeypath ( keypath ) ; if ( keyStyle === 'flat' ) obj [ keypath ] = value ; else setNested ( obj , keypath . split ( /\./g ) , value ) ; return obj ; } export function abbreviateNumber ( value ) { let newValue = value ; const suffixes = [ '' , 'K' , 'M' , 'B' , 'T' ] ; let suffixNum = 0 ; while ( newValue >= 1000 ) { newValue /= 1000 ; suffixNum ++ ; } let result = newValue . toPrecision ( 3 ) ; result += suffixes [ suffixNum ] ; return result ; }