Skip to content

Commit

Permalink
Merge pull request #21 from sampoder/patch-1
Browse files Browse the repository at this point in the history
`convertFromLightning()`: only return a Date object
  • Loading branch information
MatthewStanciu authored Dec 13, 2023
2 parents dfd79da + c2ca434 commit bbd9273
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 36 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,15 @@ Examples:
lt.convertFromLightning('8~0~0')

/*
returns {
withSeconds: '12:00:00 PM',
withoutSeconds: '12:00 PM'
}
returns a Date object with time 12:00:00 PM
*/
```

```javascript
lt.convertFromLightning('8~1~a|e')

/*
returns {
withSeconds: '12:09:26 PM',
withoutSeconds: '12:09 PM'
}
returns a Date object with time 12:09:26 PM
*/
```

Expand Down
5 changes: 2 additions & 3 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
Colors,
LightningString,
LightningTimeParts,
StaticColors,
TraditionalTimeString
StaticColors
} from './types'

export const MILLIS_PER_CHARGE = 1318.359375 // 86400000 / 16^4
Expand Down Expand Up @@ -84,7 +83,7 @@ export class LightningTime {
return stripCharges(lightningString)
}

convertFromLightning(lightningString: string): TraditionalTimeString {
convertFromLightning(lightningString: string): Date {
const isValid = validate(lightningString)
if (!isValid) {
throw new Error(
Expand Down
6 changes: 0 additions & 6 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ export interface LightningTimeParts {
charges: string
}

export interface TraditionalTimeString {
withSeconds: string
withoutSeconds: string
date: Date
}

export interface Colors {
boltColor: string
zapColor: string
Expand Down
14 changes: 3 additions & 11 deletions src/utils/ms-to-time.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { TraditionalTimeString } from '../types'

const msToTime = (millis: number): TraditionalTimeString => {
const msToTime = (millis: number): Date => {
const ms = millis % 1000
millis = (millis - ms) / 1000
const secs = millis % 60
Expand All @@ -12,15 +10,9 @@ const msToTime = (millis: number): TraditionalTimeString => {
date.setHours(hrs)
date.setMinutes(mins)
date.setSeconds(secs)
date.setMilliseconds(ms)

return {
withSeconds: date.toLocaleTimeString(),
withoutSeconds: date.toLocaleTimeString([], {
hour: 'numeric',
minute: '2-digit'
}),
date
}
return date
}

export default msToTime
24 changes: 16 additions & 8 deletions test/LightningTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,25 @@ describe('from lightning', () => {
const lightningTime = new LightningTime()
it('should convert from lightning', () => {
const convert = lightningTime.convertFromLightning('8~0~0')
expect(convert).toMatchObject({
withSeconds: '12:00:00 PM',
withoutSeconds: '12:00 PM'
})

const expectedDate = new Date()
expectedDate.setHours(12)
expectedDate.setMinutes(0)
expectedDate.setSeconds(0)
expectedDate.setMilliseconds(0)

expect(convert.getTime()).toEqual(expectedDate.getTime())
})
it('should convert from lightning with charges', () => {
const convert = lightningTime.convertFromLightning('8~0~0|a')
expect(convert).toMatchObject({
withSeconds: '12:00:13 PM',
withoutSeconds: '12:00 PM'
})
const expectedDate = new Date()

expectedDate.setHours(12)
expectedDate.setMinutes(0)
expectedDate.setSeconds(13)
expectedDate.setMilliseconds(183)

expect(convert.getTime()).toEqual(expectedDate.getTime())
})
it('should throw an error when time format is incorrect', () => {
expect(() => lightningTime.convertFromLightning('8~0|')).toThrow(
Expand Down

0 comments on commit bbd9273

Please sign in to comment.