forked from AnalogJ/scrutiny
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AnalogJ#634 from bauzer714/addDeviceHoursSetting
Create a setting for user to indicate humanized or hours on dashboard/device detail
- Loading branch information
Showing
11 changed files
with
121 additions
and
18 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
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
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
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
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
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
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
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
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,52 @@ | ||
import { DeviceHoursPipe } from "./device-hours.pipe"; | ||
|
||
describe("DeviceHoursPipe", () => { | ||
it("create an instance", () => { | ||
const pipe = new DeviceHoursPipe(); | ||
expect(pipe).toBeTruthy(); | ||
}); | ||
|
||
describe("#transform", () => { | ||
const testCases = [ | ||
{ | ||
input: 12345, | ||
configuration: "device_hours", | ||
result: "12345 hours", | ||
}, | ||
{ | ||
input: 15273, | ||
configuration: "humanize", | ||
result: "1 year, 8 months, 3 weeks, 6 days, 15 hours", | ||
}, | ||
{ | ||
input: 48, | ||
configuration: null, | ||
result: "2 days", | ||
}, | ||
{ | ||
input: 168, | ||
configuration: "scrutiny", | ||
result: "1 week", | ||
}, | ||
{ | ||
input: null, | ||
configuration: "device_hours", | ||
result: "Unknown", | ||
}, | ||
{ | ||
input: null, | ||
configuration: "humanize", | ||
result: "Unknown", | ||
}, | ||
]; | ||
|
||
testCases.forEach((test, index) => { | ||
it(`format input '${test.input}' with configuration '${test.configuration}', should be '${test.result}' (testcase: ${index + 1})`, () => { | ||
// test | ||
const pipe = new DeviceHoursPipe(); | ||
const formatted = pipe.transform(test.input, test.configuration); | ||
expect(formatted).toEqual(test.result); | ||
}); | ||
}); | ||
}); | ||
}); |
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,19 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import humanizeDuration from 'humanize-duration'; | ||
|
||
@Pipe({ name: 'deviceHours' }) | ||
export class DeviceHoursPipe implements PipeTransform { | ||
static format(hoursOfRunTime: number, unit: string, humanizeConfig: object): string { | ||
if (hoursOfRunTime === null) { | ||
return 'Unknown'; | ||
} | ||
if (unit === 'device_hours') { | ||
return `${hoursOfRunTime} hours`; | ||
} | ||
return humanizeDuration(hoursOfRunTime * 60 * 60 * 1000, humanizeConfig); | ||
} | ||
|
||
transform(hoursOfRunTime: number, unit = 'humanize', humanizeConfig: any = {}): string { | ||
return DeviceHoursPipe.format(hoursOfRunTime, unit, humanizeConfig) | ||
} | ||
} |
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