This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
central-estimates-time-conversion.user.js
73 lines (61 loc) · 2.05 KB
/
central-estimates-time-conversion.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// ==UserScript==
// @name Central Estimates time conversion
// @namespace https://central.tri.be/
// @version 0.1
// @description Makes sure estimate field accepts format such as 2h 5m and converts it into an float value.
// @author Crisoforo Gaspar Hernandez
// @include https://central.tri.be/issues/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var input = document.getElementById('issue_estimated_hours');
var KEYS = {
MINUTE: 'm',
HOUR: 'h',
};
if ( ! input ) {
return;
}
input.style.width = '40px';
var NOT_FOUND = -1;
var decimalPlaces = 2;
function to_float( str ) {
var number = parseFloat( str );
return isNaN( number ) ? 0 : number;
}
function format( input ) {
return input.toFixed( decimalPlaces );
}
function getAmount( value ) {
return value
// Separate words
.split(' ')
// Translate strings to float numbers
.map( function( chunk ) {
if ( chunk.indexOf( KEYS.HOUR ) !== NOT_FOUND ) {
return to_float( chunk.replace( KEYS.HOUR, '') );
} else if ( chunk.indexOf( KEYS.MINUTE ) !== NOT_FOUND ) {
return to_float( chunk.replace( KEYS.MINUTE, '') ) / 60 ;
} else {
return 0;
}
// return the math (sum) on them
}).reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
}
function onBlur() {
if ( ! input.value ) {
return;
}
var value = input.value.toLowerCase();
// does not contain an h or an m
if ( value.indexOf( KEYS.HOUR ) === NOT_FOUND && value.indexOf( KEYS.MINUTE ) === NOT_FOUND ) {
input.value = format( to_float( value ) );
} else {
input.value = format( to_float( getAmount( value ) ) );
}
}
input.addEventListener( 'blur', onBlur );
})();