Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
dde56a78
Commit
dde56a78
authored
Aug 31, 2017
by
Bryce Johnson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for custom daysPerWeek and hoursPerDay to prettyTime.parseSeconds.
parent
e5842838
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
88 additions
and
6 deletions
+88
-6
app/assets/javascripts/lib/utils/pretty_time.js
app/assets/javascripts/lib/utils/pretty_time.js
+7
-6
spec/javascripts/pretty_time_spec.js
spec/javascripts/pretty_time_spec.js
+81
-0
No files found.
app/assets/javascripts/lib/utils/pretty_time.js
View file @
dde56a78
...
...
@@ -2,19 +2,20 @@ import _ from 'underscore';
(()
=>
{
/*
* TODO: Make these methods more configurable (e.g.
parseSeconds timePeriodContstraints,
*
stringifyTime condensed or
non-condensed, abbreviateTimelengths)
* TODO: Make these methods more configurable (e.g.
stringifyTime condensed or
* non-condensed, abbreviateTimelengths)
* */
const
utils
=
window
.
gl
.
utils
=
gl
.
utils
||
{};
const
prettyTime
=
utils
.
prettyTime
=
{
/*
* Accepts seconds and returns a timeObject { weeks: #, days: #, hours: #, minutes: # }
* Seconds can be negative or positive, zero or non-zero.
* Seconds can be negative or positive, zero or non-zero. Can be configured for any day
* or week length.
*/
parseSeconds
(
seconds
)
{
const
DAYS_PER_WEEK
=
5
;
const
HOURS_PER_DAY
=
8
;
parseSeconds
(
seconds
,
{
daysPerWeek
=
5
,
hoursPerDay
=
8
}
=
{}
)
{
const
DAYS_PER_WEEK
=
daysPerWeek
;
const
HOURS_PER_DAY
=
hoursPerDay
;
const
MINUTES_PER_HOUR
=
60
;
const
MINUTES_PER_WEEK
=
DAYS_PER_WEEK
*
HOURS_PER_DAY
*
MINUTES_PER_HOUR
;
const
MINUTES_PER_DAY
=
HOURS_PER_DAY
*
MINUTES_PER_HOUR
;
...
...
spec/javascripts/pretty_time_spec.js
View file @
dde56a78
...
...
@@ -76,6 +76,87 @@ import '~/lib/utils/pretty_time';
expect
(
aboveOneWeek
.
days
).
toBe
(
3
);
expect
(
aboveOneWeek
.
weeks
).
toBe
(
173
);
});
it
(
'
should correctly accept a custom param for hoursPerDay
'
,
function
()
{
const
parser
=
prettyTime
.
parseSeconds
;
const
config
=
{
hoursPerDay
:
24
};
const
aboveOneHour
=
parser
(
4800
,
config
);
expect
(
aboveOneHour
.
minutes
).
toBe
(
20
);
expect
(
aboveOneHour
.
hours
).
toBe
(
1
);
expect
(
aboveOneHour
.
days
).
toBe
(
0
);
expect
(
aboveOneHour
.
weeks
).
toBe
(
0
);
const
aboveOneDay
=
parser
(
110000
,
config
);
expect
(
aboveOneDay
.
minutes
).
toBe
(
33
);
expect
(
aboveOneDay
.
hours
).
toBe
(
6
);
expect
(
aboveOneDay
.
days
).
toBe
(
1
);
expect
(
aboveOneDay
.
weeks
).
toBe
(
0
);
const
aboveOneWeek
=
parser
(
25000000
,
config
);
expect
(
aboveOneWeek
.
minutes
).
toBe
(
26
);
expect
(
aboveOneWeek
.
hours
).
toBe
(
8
);
expect
(
aboveOneWeek
.
days
).
toBe
(
4
);
expect
(
aboveOneWeek
.
weeks
).
toBe
(
57
);
});
it
(
'
should correctly accept a custom param for daysPerWeek
'
,
function
()
{
const
parser
=
prettyTime
.
parseSeconds
;
const
config
=
{
daysPerWeek
:
7
};
const
aboveOneHour
=
parser
(
4800
,
config
);
expect
(
aboveOneHour
.
minutes
).
toBe
(
20
);
expect
(
aboveOneHour
.
hours
).
toBe
(
1
);
expect
(
aboveOneHour
.
days
).
toBe
(
0
);
expect
(
aboveOneHour
.
weeks
).
toBe
(
0
);
const
aboveOneDay
=
parser
(
110000
,
config
);
expect
(
aboveOneDay
.
minutes
).
toBe
(
33
);
expect
(
aboveOneDay
.
hours
).
toBe
(
6
);
expect
(
aboveOneDay
.
days
).
toBe
(
3
);
expect
(
aboveOneDay
.
weeks
).
toBe
(
0
);
const
aboveOneWeek
=
parser
(
25000000
,
config
);
expect
(
aboveOneWeek
.
minutes
).
toBe
(
26
);
expect
(
aboveOneWeek
.
hours
).
toBe
(
0
);
expect
(
aboveOneWeek
.
days
).
toBe
(
0
);
expect
(
aboveOneWeek
.
weeks
).
toBe
(
124
);
});
it
(
'
should correctly accept custom params for daysPerWeek and hoursPerDay
'
,
function
()
{
const
parser
=
prettyTime
.
parseSeconds
;
const
config
=
{
daysPerWeek
:
55
,
hoursPerDay
:
14
};
const
aboveOneHour
=
parser
(
4800
,
config
);
expect
(
aboveOneHour
.
minutes
).
toBe
(
20
);
expect
(
aboveOneHour
.
hours
).
toBe
(
1
);
expect
(
aboveOneHour
.
days
).
toBe
(
0
);
expect
(
aboveOneHour
.
weeks
).
toBe
(
0
);
const
aboveOneDay
=
parser
(
110000
,
config
);
expect
(
aboveOneDay
.
minutes
).
toBe
(
33
);
expect
(
aboveOneDay
.
hours
).
toBe
(
2
);
expect
(
aboveOneDay
.
days
).
toBe
(
2
);
expect
(
aboveOneDay
.
weeks
).
toBe
(
0
);
const
aboveOneWeek
=
parser
(
25000000
,
config
);
expect
(
aboveOneWeek
.
minutes
).
toBe
(
26
);
expect
(
aboveOneWeek
.
hours
).
toBe
(
0
);
expect
(
aboveOneWeek
.
days
).
toBe
(
1
);
expect
(
aboveOneWeek
.
weeks
).
toBe
(
9
);
});
});
describe
(
'
stringifyTime
'
,
function
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment