Skip to content

Commit

Permalink
feat: sleep time advisor (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-sharma7 authored Oct 26, 2024
1 parent a91a230 commit c6416a4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [Mental Health Management](./guide/mentalHealth.md)
- [Focus Time Management](./guide/focusTimeManagement.md)
- [Water Intake Reminder](./guide/waterIntakeReminder.md)
- [Sleep Time Advisor](./guide/sleepTimeAdvisor.md)

</details>

Expand Down
35 changes: 35 additions & 0 deletions docs/guide/sleepTimeAdvisor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sleep Time Advisor.

## Description

The sleepTimeAdvisor function helps users determine an optimal bedtime based on their desired wake-up time, number of sleep cycles, and time zone. Each sleep cycle is assumed to last approximately 90 minutes, following standard recommendations for uninterrupted, restful sleep.

## Usage

```javascript
import { sleepTimeAdvisor } from 'world-clockify';

const result = sleepTimeAdvisor('07:00', 5, 'America/New_York');

console.log(result.suggestedSleepTime);
console.log(result.message);
```

## Expected Output

```bash
bedtime, e.g., "11:30 PM"
"To get 5 cycles of sleep, you should go to bed by 11:30 PM."
```

## Parameters

```javascript
- {string} wakeUpTime
- {number} sleepCycles
- {string} preferredTimeZone
```

## Returns

- (object): As a result.
21 changes: 21 additions & 0 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,24 @@ export const waterIntakeReminder = (preference: waterUserPreference) => {

return reminders;
};

/**
* sleep schedule based on their desired wake-up time and how many hours of sleep they need.
* @param {string} wakeUpTime
* @param {number} sleepCycles
* @param {string} preferredTimeZone
*/

export const sleepTimeAdvisor = (wakeUpTime: string, sleepCycles: number, preferredTimeZone: string) => {
const cycleDuration = 90; // in minutes
const totalSleepTime = cycleDuration * sleepCycles;

const wakeUp = DateTime.fromFormat(wakeUpTime, 'HH:mm', { zone: preferredTimeZone });

const suggestedSleepTime = wakeUp.minus({ minutes: totalSleepTime });

return {
suggestedSleepTime: suggestedSleepTime.toFormat('hh:mm a'),
message: `To get ${sleepCycles} cycles of sleep, you should go to bed by ${suggestedSleepTime.toFormat('hh:mm a')}.`,
};
};
11 changes: 11 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
sheduleWorkAndBreaks,
focusTimeManager,
waterIntakeReminder,
sleepTimeAdvisor,
} from '../src/function.js';

describe('Timezone-Aware Date Helper', () => {
Expand Down Expand Up @@ -172,4 +173,14 @@ describe('Timezone-Aware Date Helper', () => {

expect(waterIntake).toEqual(expectedReminders);
});

it('should suggest sleep time', () => {
const wakeUpTime = '07:00';
const sleepCycles = 5;
const timeZone = 'America/New_York';

const result = sleepTimeAdvisor(wakeUpTime, sleepCycles, timeZone);
expect(result.suggestedSleepTime).toBe('11:30 PM');
expect(result.message).toBe('To get 5 cycles of sleep, you should go to bed by 11:30 PM.');
});
});

0 comments on commit c6416a4

Please sign in to comment.