| 1852 | * @since 2.0.0 |
| 1853 | */ |
| 1854 | export const format = (self: Duration): string => { |
| 1855 | if (self.value._tag === "Infinity") { |
| 1856 | return "Infinity" |
| 1857 | } |
| 1858 | if (self.value._tag === "NegativeInfinity") { |
| 1859 | return "-Infinity" |
| 1860 | } |
| 1861 | if (isZero(self)) { |
| 1862 | return "0" |
| 1863 | } |
| 1864 | if (isNegative(self)) { |
| 1865 | return "-" + format(abs(self)) |
| 1866 | } |
| 1867 | |
| 1868 | const fragments = parts(self) |
| 1869 | const pieces = [] |
| 1870 | if (fragments.days !== 0) { |
| 1871 | pieces.push(`${fragments.days}d`) |
| 1872 | } |
| 1873 | |
| 1874 | if (fragments.hours !== 0) { |
| 1875 | pieces.push(`${fragments.hours}h`) |
| 1876 | } |
| 1877 | |
| 1878 | if (fragments.minutes !== 0) { |
| 1879 | pieces.push(`${fragments.minutes}m`) |
| 1880 | } |
| 1881 | |
| 1882 | if (fragments.seconds !== 0) { |
| 1883 | pieces.push(`${fragments.seconds}s`) |
| 1884 | } |
| 1885 | |
| 1886 | if (fragments.millis !== 0) { |
| 1887 | pieces.push(`${fragments.millis}ms`) |
| 1888 | } |
| 1889 | |
| 1890 | if (fragments.nanos !== 0) { |
| 1891 | pieces.push(`${fragments.nanos}ns`) |
| 1892 | } |
| 1893 | |
| 1894 | return pieces.join(" ") |
| 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * Formats a `Duration` as an ISO 8601 duration string. |