utils.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. export function ease( target, current, deltaTime, duration ) {
  2. if ( duration <= 0 ) return current;
  3. const t = Math.min( 1, deltaTime / duration );
  4. target += ( current - target ) * t;
  5. return target;
  6. }
  7. export function createValueSpan( id = null ) {
  8. const span = document.createElement( 'span' );
  9. span.className = 'value';
  10. if ( id !== null ) span.id = id;
  11. return span;
  12. }
  13. export function setText( element, text ) {
  14. const el = element instanceof HTMLElement ? element : document.getElementById( element );
  15. if ( el && el.textContent !== text ) {
  16. el.textContent = text;
  17. }
  18. }
  19. export function getText( element ) {
  20. const el = element instanceof HTMLElement ? element : document.getElementById( element );
  21. return el ? el.textContent : null;
  22. }
  23. export function splitPath( fullPath ) {
  24. const lastSlash = fullPath.lastIndexOf( '/' );
  25. if ( lastSlash === - 1 ) {
  26. return {
  27. path: '',
  28. name: fullPath.trim()
  29. };
  30. }
  31. const path = fullPath.substring( 0, lastSlash ).trim();
  32. const name = fullPath.substring( lastSlash + 1 ).trim();
  33. return { path, name };
  34. }
  35. export function splitCamelCase( str ) {
  36. return str.replace( /([a-z0-9])([A-Z])/g, '$1 $2' ).trim();
  37. }
粤ICP备19079148号