utils.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. export function createValueSpan( id = null ) {
  2. const span = document.createElement( 'span' );
  3. span.className = 'value';
  4. if ( id !== null ) span.id = id;
  5. return span;
  6. }
  7. export function setText( element, text ) {
  8. const el = element instanceof HTMLElement ? element : document.getElementById( element );
  9. if ( el && el.textContent !== text ) {
  10. el.textContent = text;
  11. }
  12. }
  13. export function getText( element ) {
  14. const el = element instanceof HTMLElement ? element : document.getElementById( element );
  15. return el ? el.textContent : null;
  16. }
  17. export function splitPath( fullPath ) {
  18. const lastSlash = fullPath.lastIndexOf( '/' );
  19. if ( lastSlash === - 1 ) {
  20. return {
  21. path: '',
  22. name: fullPath.trim()
  23. };
  24. }
  25. const path = fullPath.substring( 0, lastSlash ).trim();
  26. const name = fullPath.substring( lastSlash + 1 ).trim();
  27. return { path, name };
  28. }
  29. export function splitCamelCase( str ) {
  30. return str.replace( /([a-z0-9])([A-Z])/g, '$1 $2' ).trim();
  31. }
  32. export function formatBytes( bytes, decimals = 2 ) {
  33. if ( bytes === 0 ) return '0 Bytes';
  34. const k = 1024;
  35. const dm = decimals < 0 ? 0 : decimals;
  36. const sizes = [ 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ];
  37. const i = Math.floor( Math.log( bytes ) / Math.log( k ) );
  38. return parseFloat( ( bytes / Math.pow( k, i ) ).toFixed( dm ) ) + ' ' + sizes[ i ];
  39. }
粤ICP备19079148号