-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docs): add mdx file for secondsToTime function
- Loading branch information
1 parent
271d112
commit 25bd6b8
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# secondsToTime | ||
|
||
Formats a number of seconds into a string representation based on a specified format. | ||
|
||
|
||
### Import | ||
|
||
```typescript copy | ||
import { secondsToTime } from '@fullstacksjs/toolbox'; | ||
``` | ||
|
||
### Signature | ||
|
||
```typescript copy | ||
function secondsToTime({ | ||
sec, | ||
format, | ||
}: { | ||
sec: number; | ||
format: 'hh:mm:ss' | 'hh:mm' | 'mm:ss'; | ||
}): string; | ||
``` | ||
|
||
### Examples | ||
|
||
```typescript copy | ||
sleep(1000).then(() => console.log("Hi")) // Logs hi after 1 second | ||
await sleep(1000); console.log("Hi") // Logs hi after 1 second | ||
``` | ||
|
||
|
||
## Import | ||
|
||
```typescript copy | ||
import { secondsToTime } from './secondsToTime'; | ||
``` | ||
|
||
## Signature | ||
|
||
```typescript | ||
function secondsToTime({ | ||
sec, | ||
format, | ||
}: { | ||
sec: number; | ||
format: 'hh:mm:ss' | 'hh:mm' | 'mm:ss'; | ||
}): string; | ||
``` | ||
|
||
## Parameters | ||
|
||
- **`sec`** (`number`): The total number of seconds to be converted. | ||
- **`format`** (`'hh:mm:ss' | 'hh:mm' | 'mm:ss'`): The desired output format. | ||
|
||
## Returns | ||
|
||
- **`string`**: The formatted time string based on the specified format. | ||
|
||
## Examples | ||
|
||
### Formatting to `hh:mm:ss` | ||
|
||
```typescript | ||
const time1 = secondsToTime({ sec: 3661, format: 'hh:mm:ss' }); | ||
// Output: '01:01:01' | ||
``` | ||
|
||
### Formatting Zero Seconds | ||
|
||
```typescript | ||
const time4 = secondsToTime({ sec: 0, format: 'mm:ss' }); | ||
// Output: '00:00' | ||
``` | ||
|
||
## Error Handling | ||
|
||
If an invalid format is provided, the function will throw an error: | ||
|
||
```typescript | ||
try { | ||
secondsToTime({ sec: 120, format: 'ss:ss:ss' }); | ||
} catch (e) { | ||
console.error(e.message); // 'Invalid format: 'ss:ss:ss'. Accepted formats are 'hh:mm:ss', 'hh:mm', or 'mm:ss'.' | ||
} | ||
``` |