| 1704 | * @since 2.0.0 |
| 1705 | */ |
| 1706 | export const format = (self: Duration): string => { |
| 1707 | if (self.value._tag === "Infinity") { |
| 1708 | return "Infinity" |
| 1709 | } |
| 1710 | if (self.value._tag === "NegativeInfinity") { |
| 1711 | return "-Infinity" |
| 1712 | } |
| 1713 | if (isZero(self)) { |
| 1714 | return "0" |
| 1715 | } |
| 1716 | if (isNegative(self)) { |
| 1717 | return "-" + format(abs(self)) |
| 1718 | } |
| 1719 | |
| 1720 | const fragments = parts(self) |
| 1721 | const pieces = [] |
| 1722 | if (fragments.days !== 0) { |
| 1723 | pieces.push(`${fragments.days}d`) |
| 1724 | } |
| 1725 | |
| 1726 | if (fragments.hours !== 0) { |
| 1727 | pieces.push(`${fragments.hours}h`) |
| 1728 | } |
| 1729 | |
| 1730 | if (fragments.minutes !== 0) { |
| 1731 | pieces.push(`${fragments.minutes}m`) |
| 1732 | } |
| 1733 | |
| 1734 | if (fragments.seconds !== 0) { |
| 1735 | pieces.push(`${fragments.seconds}s`) |
| 1736 | } |
| 1737 | |
| 1738 | if (fragments.millis !== 0) { |
| 1739 | pieces.push(`${fragments.millis}ms`) |
| 1740 | } |
| 1741 | |
| 1742 | if (fragments.nanos !== 0) { |
| 1743 | pieces.push(`${fragments.nanos}ns`) |
| 1744 | } |
| 1745 | |
| 1746 | return pieces.join(" ") |
| 1747 | } |
| 1748 | |
| 1749 | /** |
| 1750 | * Reducer for summing `Duration`s. |