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
Boxiang Sun
gitlab-ce
Commits
dd7a59bf
Commit
dd7a59bf
authored
Jun 07, 2018
by
Kushal Pandya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add timeframe helper methods
parent
760b12dc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
41 deletions
+59
-41
app/assets/javascripts/lib/utils/datetime_utility.js
app/assets/javascripts/lib/utils/datetime_utility.js
+48
-29
spec/javascripts/datetime_utility_spec.js
spec/javascripts/datetime_utility_spec.js
+11
-12
No files found.
app/assets/javascripts/lib/utils/datetime_utility.js
View file @
dd7a59bf
...
...
@@ -269,6 +269,17 @@ export const totalDaysInMonth = date => {
return
new
Date
(
date
.
getFullYear
(),
date
.
getMonth
()
+
1
,
0
).
getDate
();
};
/**
* Returns number of days in a quarter from provided
* months array.
*
* @param {Array} quarter
*/
export
const
totalDaysInQuarter
=
quarter
=>
quarter
.
reduce
(
(
acc
,
month
)
=>
acc
+
totalDaysInMonth
(
month
),
0
,
);
/**
* Returns list of Dates referring to Sundays of the month
* based on provided date
...
...
@@ -309,42 +320,27 @@ export const getSundays = date => {
};
/**
* Returns list of Dates representing a timeframe of Months from month of provided date (inclusive)
* up to provided length
*
* For eg;
* If current month is January 2018 and `length` provided is `6`
* Then this method will return list of Date objects as follows;
*
* [ October 2017, November 2017, December 2017, January 2018, February 2018, March 2018 ]
*
* If current month is March 2018 and `length` provided is `3`
* Then this method will return list of Date objects as follows;
*
* [ February 2018, March 2018, April 2018 ]
* Returns list of Dates representing a timeframe of months from startDate and length
*
* @param {Date} startDate
* @param {Number} length
* @param {Date} date
*/
export
const
getTimeframeWindow
=
(
length
,
date
)
=>
{
if
(
!
length
)
{
export
const
getTimeframeWindow
From
=
(
startDate
,
length
)
=>
{
if
(
!
(
startDate
instanceof
Date
)
||
!
length
)
{
return
[];
}
const
currentDate
=
date
instanceof
Date
?
date
:
new
Date
();
const
currentMonthIndex
=
Math
.
floor
(
length
/
2
);
const
timeframe
=
[];
// Move date object backward to the first month of timeframe
currentDate
.
setDate
(
1
);
currentDate
.
setMonth
(
currentDate
.
getMonth
()
-
currentMonthIndex
);
// Iterate and update date for the size of length
// Iterate and set date for the size of length
// and push date reference to timeframe list
for
(
let
i
=
0
;
i
<
length
;
i
+=
1
)
{
timeframe
.
push
(
new
Date
(
currentDate
.
getTime
()));
currentDate
.
setMonth
(
currentDate
.
getMonth
()
+
1
);
}
const
timeframe
=
new
Array
(
length
)
.
fill
()
.
map
(
(
val
,
i
)
=>
new
Date
(
startDate
.
getFullYear
(),
startDate
.
getMonth
()
+
i
,
1
,
),
);
// Change date of last timeframe item to last date of the month
timeframe
[
length
-
1
].
setDate
(
totalDaysInMonth
(
timeframe
[
length
-
1
]));
...
...
@@ -352,6 +348,29 @@ export const getTimeframeWindow = (length, date) => {
return
timeframe
;
};
/**
* Returns count of day within current quarter from provided date
* and array of months for the quarter
*
* Eg;
* If date is 15 Feb 2018
* and quarter is [Jan, Feb, Mar]
*
* Then 15th Feb is 46th day of the quarter
* Where 31 (days in Jan) + 15 (date of Feb).
*
* @param {Date} date
* @param {Array} quarter
*/
export
const
dayInQuarter
=
(
date
,
quarter
)
=>
quarter
.
reduce
((
acc
,
month
)
=>
{
if
(
date
.
getMonth
()
>
month
.
getMonth
())
{
return
acc
+
totalDaysInMonth
(
month
);
}
else
if
(
date
.
getMonth
()
===
month
.
getMonth
())
{
return
acc
+
date
.
getDate
();
}
return
acc
+
0
;
},
0
);
window
.
gl
=
window
.
gl
||
{};
window
.
gl
.
utils
=
{
...(
window
.
gl
.
utils
||
{}),
...
...
spec/javascripts/datetime_utility_spec.js
View file @
dd7a59bf
...
...
@@ -149,23 +149,22 @@ describe('getSundays', () => {
});
});
describe
(
'
getTimeframeWindow
'
,
()
=>
{
it
(
'
returns array of date
s representing a timeframe based on provided length and d
ate
'
,
()
=>
{
const
d
ate
=
new
Date
(
2018
,
0
,
1
);
describe
(
'
getTimeframeWindow
From
'
,
()
=>
{
it
(
'
returns array of date
objects upto provided length start with provided startD
ate
'
,
()
=>
{
const
startD
ate
=
new
Date
(
2018
,
0
,
1
);
const
mockTimeframe
=
[
new
Date
(
2017
,
9
,
1
),
new
Date
(
2017
,
10
,
1
),
new
Date
(
2017
,
11
,
1
),
new
Date
(
2018
,
0
,
1
),
new
Date
(
2018
,
1
,
1
),
new
Date
(
2018
,
2
,
31
),
new
Date
(
2018
,
2
,
1
),
new
Date
(
2018
,
3
,
1
),
new
Date
(
2018
,
4
,
31
),
];
const
timeframe
=
datetimeUtility
.
getTimeframeWindow
(
6
,
date
);
expect
(
timeframe
.
length
).
toBe
(
6
);
const
timeframe
=
datetimeUtility
.
getTimeframeWindowFrom
(
startDate
,
5
);
expect
(
timeframe
.
length
).
toBe
(
5
);
timeframe
.
forEach
((
timeframeItem
,
index
)
=>
{
expect
(
timeframeItem
.
getFullYear
()
===
mockTimeframe
[
index
].
getFullYear
()).
toBeTruthy
();
expect
(
timeframeItem
.
getMonth
()
===
mockTimeframe
[
index
].
getMonth
()).
toBeTruthy
();
console
.
log
(
timeframeItem
);
expect
(
timeframeItem
.
getFullYear
()
===
mockTimeframe
[
index
].
getFullYear
()).
toBe
(
true
);
expect
(
timeframeItem
.
getMonth
()
===
mockTimeframe
[
index
].
getMonth
()).
toBe
(
true
);
expect
(
timeframeItem
.
getDate
()
===
mockTimeframe
[
index
].
getDate
()).
toBeTruthy
();
});
});
...
...
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