import { redgreen } from 'root/lib/utils' ; export function countOfObject ( obj ) { const t = typeof obj ; if ( t !== 'object' || obj === null ) { return 0 ; } return Object . keys ( obj ) . length ; } export function sumOfObject ( obj ) { const t = typeof obj ; let i = 0 ; if ( t !== 'object' || obj === null ) { return 0 ; } Object . keys ( obj ) . forEach ( ( key ) => ( i += parseInt ( obj [ key ] , 10 ) ) ) ; return i ; } export function jsonParse ( input ) { if ( typeof input === 'string' ) { try { return JSON . parse ( input ) ; } catch ( e ) { return { } ; } } return input ; } export function hexToRgbA ( hex ) { let c ; if ( /^#([A-Fa-f0-9]{3}){1,2}$/ . test ( hex ) ) { c = hex . substring ( 1 ) . split ( '' ) ; if ( + c . length === 3 ) { c = [ c [ 0 ] , c [ 0 ] , c [ 1 ] , c [ 1 ] , c [ 2 ] , c [ 2 ] ] ; } c = `0x${ c . join ( '' ) }` ; return `rgba(${ [ ( c >> 16 ) & 255 , ( c >> 8 ) & 255 , c & 255 ] . join ( ',' ) },1)` ; } throw new Error ( 'Bad Hex' ) ; } export function LZ ( time ) { return time >= 10 ? time . toString ( ) : `0${ time . toString ( ) }` ; } export function shuffle ( a ) { for ( let i = a . length - 1 ; i > 0 ; i -- ) { const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ; [ a [ i ] , a [ j ] ] = [ a [ j ] , a [ i ] ] ; } return a ; } export function CountSteps ( wrpercent ) { const minwl = 30 ; const maxwl = 60 ; const stepping = ( maxwl - minwl ) / redgreen . length ; let clrange = 0 ; if ( wrpercent > minwl ) { if ( wrpercent > maxwl ) { clrange = redgreen . length - 1 ; } else { clrange = Math . floor ( ( wrpercent - minwl ) / stepping ) - 1 ; if ( clrange < 0 ) { clrange = 0 ; } if ( clrange >= redgreen . length - 1 ) { clrange = redgreen . length - 1 ; } } } return clrange ; } export function getRandomInt ( max ) { return Math . floor ( Math . random ( ) * Math . floor ( max ) ) ; } export function slugify ( text ) { return text . toString ( ) . toLowerCase ( ) . replace ( /\s+/g , '-' ) . replace ( /[^\w\-]+/g , '' ) . replace ( /\-\-+/g , '-' ) . replace ( /^-+/ , '' ) . replace ( /-+$/ , '' ) ; } export function jsUcfirst ( str ) { return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ; } export function substrcount ( str , subString , allowOverlapping ? ) { str += '' ; subString += '' ; if ( subString . length <= 0 ) { return str . length + 1 ; } let n = 0 ; let pos = 0 ; const step = allowOverlapping ? 1 : subString . length ; while ( true ) { pos = str . indexOf ( subString , pos ) ; if ( pos >= 0 ) { ++ n ; pos += step ; } else { break ; } } return n ; } export function Cut ( str , from , to , offset ? ) { const startIndex = str . indexOf ( from , offset ) ; const endIndex = str . indexOf ( to , startIndex + from . length ) ; if ( startIndex !== - 1 && endIndex !== - 1 ) { const result = str . substring ( startIndex + from . length , endIndex ) ; return result ; } else { return '' ; } }