utils.js 1.1 KB

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