wrapValue.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import getEasing from './getEasing';
  2. import presets, { PresetsItem } from './presets';
  3. import shouldUseBezier from './shouldUseBezier';
  4. export interface Value {
  5. easing: string;
  6. duration: number | string;
  7. }
  8. export interface DefaultConfig extends PresetsItem {
  9. precision: number;
  10. }
  11. export interface Config {
  12. easing?: string;
  13. duration?: number | string;
  14. delay?: string;
  15. }
  16. export interface Wrapped extends DefaultConfig, Config {
  17. done: boolean;
  18. val?: Value;
  19. }
  20. const defaultConfig: DefaultConfig = {
  21. ...presets.default,
  22. precision: 0.01,
  23. };
  24. export default function wrapValue(val: Value, config: Config = {}) {
  25. if (shouldUseBezier(config)) {
  26. const easing = getEasing(config.easing);
  27. const duration = typeof config.duration === 'number' && config.duration > 0 ? config.duration : 1000;
  28. config = { ...config, easing, duration };
  29. }
  30. let wrapped: Wrapped = { ...defaultConfig, ...config, done: false };
  31. if (val && typeof val === 'object' && 'val' in val) {
  32. if (shouldUseBezier(val)) {
  33. const easing = getEasing(val.easing);
  34. const duration = typeof val.duration === 'number' && val.duration > 0 ?
  35. val.duration :
  36. parseInt(config.duration as string) || 1000;
  37. val = { ...val, easing, duration };
  38. }
  39. wrapped = { ...wrapped, ...val };
  40. } else {
  41. wrapped = { ...wrapped, val };
  42. }
  43. return wrapped;
  44. }