interval.d.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /** A named interval. */
  2. export type LiteralTimeInterval =
  3. | "3 months"
  4. | "10 years"
  5. | TimeIntervalName
  6. | (`${TimeIntervalName}s` & Record<never, never>)
  7. | (`${number} ${TimeIntervalName}` & Record<never, never>)
  8. | (`${number} ${TimeIntervalName}s` & Record<never, never>);
  9. /**
  10. * The built-in time intervals; UTC or local time, depending on context. The
  11. * *week* interval is an alias for *sunday*. The *quarter* interval is every
  12. * three months, and the *half* interval is every six months, aligned at the
  13. * start of the year.
  14. */
  15. export type TimeIntervalName =
  16. | "second"
  17. | "minute"
  18. | "hour"
  19. | "day"
  20. | "week"
  21. | "month"
  22. | "quarter" // 3 months
  23. | "half" // 6 months
  24. | "year"
  25. | "monday"
  26. | "tuesday"
  27. | "wednesday"
  28. | "thursday"
  29. | "friday"
  30. | "saturday"
  31. | "sunday";
  32. /** A custom interval implementation. */
  33. export interface IntervalImplementation<T> {
  34. /**
  35. * Returns the value representing the greatest interval boundary less than or
  36. * equal to the specified *value*. For example, day.floor(*date*) typically
  37. * returns 12:00 AM on the given date.
  38. *
  39. * This method is idempotent: if the specified value is already floored to the
  40. * current interval, the same value is returned. Furthermore, the returned
  41. * value is the minimum expressible value of the associated interval, such
  42. * that floor(floor(*value*) - *epsilon*) returns the preceding interval
  43. * boundary value.
  44. */
  45. floor(value: T): T;
  46. /**
  47. * Returns a new value equal to *value* plus *step* intervals. If *step* is
  48. * not specified it defaults to 1. If *step* is negative, then the returned
  49. * value will be less than the specified *value*; if *step* is zero, then the
  50. * same *value* is returned; if *step* is not an integer, it is floored.
  51. *
  52. * This method does not round the specified *value* to the interval. For
  53. * example, if *date* is today at 5:34 PM, then day.offset(*date*, 1) returns
  54. * 5:34 PM tomorrow.
  55. */
  56. offset(value: T, step?: number): T;
  57. }
  58. /**
  59. * A custom interval implementation that also supports the range method, say for
  60. * generating thresholds or ticks.
  61. */
  62. export interface RangeIntervalImplementation<T> extends IntervalImplementation<T> {
  63. /**
  64. * Returns an array of values representing every interval boundary greater
  65. * than or equal to *start* (inclusive) and less than *stop* (exclusive). The
  66. * first value in the returned array is the least boundary greater than or
  67. * equal to *start*; subsequent values are offset by intervals and floored.
  68. */
  69. range(start: T, stop: T): T[];
  70. }
  71. /**
  72. * A custom interval implementation that also supports the range and ceil
  73. * methods, used for nicing scale domains.
  74. */
  75. export interface NiceIntervalImplementation<T> extends RangeIntervalImplementation<T> {
  76. /**
  77. * Returns the value representing the least interval boundary value greater
  78. * than or equal to the specified *value*. For example, day.ceil(*date*)
  79. * typically returns 12:00 AM on the date following the given date.
  80. *
  81. * This method is idempotent: if the specified date is already ceilinged to
  82. * the current interval, the same value is returned. Furthermore, the returned
  83. * value is the maximum expressible value of the associated interval, such
  84. * that ceil(ceil(*value*) + *epsilon*) returns the following interval
  85. * boundary value.
  86. */
  87. ceil(value: T): T;
  88. }
  89. /** A literal that can be automatically promoted to an interval. */
  90. type LiteralInterval<T> = T extends Date ? LiteralTimeInterval : T extends number ? number : never;
  91. /**
  92. * How to partition a continuous range into discrete intervals; one of:
  93. *
  94. * - an object that implements *floor* and *offset* methods
  95. * - a named time interval such as *day* (for date intervals)
  96. * - a number (for number intervals), defining intervals at integer multiples of *n*
  97. */
  98. export type Interval<T = any> = LiteralInterval<T> | IntervalImplementation<T>;
  99. /**
  100. * An interval that also supports the *range* method, used to subdivide a
  101. * continuous range into discrete partitions, say for thresholds or ticks; one
  102. * of:
  103. *
  104. * - an object that implements *floor*, *offset*, and *range* methods
  105. * - a named time interval such as *day* (for date intervals)
  106. * - a number (for number intervals), defining intervals at integer multiples of *n*
  107. */
  108. export type RangeInterval<T = any> = LiteralInterval<T> | RangeIntervalImplementation<T>;
  109. /**
  110. * A range interval that also supports the *ceil* method, used to nice a scale
  111. * domain; one of:
  112. *
  113. * - an object that implements *floor*, *ceil*, *offset*, and *range* methods
  114. * - a named time interval such as *day* (for date intervals)
  115. * - a number (for number intervals), defining intervals at integer multiples of *n*
  116. */
  117. export type NiceInterval<T = any> = LiteralInterval<T> | NiceIntervalImplementation<T>;
  118. /**
  119. * Given a number *period*, returns a corresponding numeric range interval. If
  120. * *period* is a negative number, the returned interval uses 1 / -*period*,
  121. * allowing greater precision when *period* is a negative integer.
  122. */
  123. export function numberInterval(period: number): RangeIntervalImplementation<number>;
  124. /** Given a string *period*, returns a corresponding local time nice interval. */
  125. export function timeInterval(period: LiteralTimeInterval): NiceIntervalImplementation<Date>;
  126. /** Given a string *period*, returns a corresponding UTC nice interval. */
  127. export function utcInterval(period: LiteralTimeInterval): NiceIntervalImplementation<Date>;