Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Drip
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
backup
Drip
Commits
3b2beabb
Commit
3b2beabb
authored
6 years ago
by
Julia Friesel
Browse files
Options
Downloads
Patches
Plain Diff
Get cycle day number for several happy paths
parent
6d37d451
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
get-cycle-day-number.js
+46
-0
46 additions, 0 deletions
get-cycle-day-number.js
test/app.spec.js
+0
-5
0 additions, 5 deletions
test/app.spec.js
test/get-cycle-day.spec.js
+136
-0
136 additions, 0 deletions
test/get-cycle-day.spec.js
with
182 additions
and
5 deletions
get-cycle-day-number.js
0 → 100644
+
46
−
0
View file @
3b2beabb
export
default
function
(
cycleDays
,
targetDay
)
{
const
lastFirstBleedingday
=
findLastFirstBleedingDay
(
cycleDays
)
if
(
!
lastFirstBleedingday
)
return
const
diffInDays
=
targetDay
.
diff
(
lastFirstBleedingday
.
date
,
'
days
'
)
// cycle starts at day 1
return
diffInDays
+
1
}
function
findLastFirstBleedingDay
(
cycleDays
)
{
// sort the cycle days in descending order
// so we travel into the past as we iterate
// over the array
cycleDays
.
sort
((
a
,
b
)
=>
b
.
date
-
a
.
date
)
let
sawBleeding
=
false
for
(
let
i
=
0
;
i
<
cycleDays
.
length
;
i
++
)
{
const
cycleDay
=
cycleDays
[
i
]
// we have detected the day before the beginning
// of a bleeding period, so the previous cycle day in the array
// was the first day of bleeding
if
(
sawBleeding
&&
(
!
isBleedingDay
(
cycleDay
)))
{
return
cycleDays
[
i
-
1
]
}
// if we get to the earliest cycle day and there was
// bleeding on that day, it's the first bleeding day for us
if
(
i
===
cycleDays
.
length
-
1
&&
isBleedingDay
(
cycleDay
))
{
return
cycleDay
}
if
(
isBleedingDay
(
cycleDay
))
{
sawBleeding
=
true
}
}
}
function
isBleedingDay
(
cycleDay
)
{
return
cycleDay
.
bleeding
&&
cycleDay
.
bleeding
.
value
&&
!
cycleDay
.
exclude
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test/app.spec.js
deleted
100644 → 0
+
0
−
5
View file @
6d37d451
import
{
expect
}
from
'
chai
'
it
(
'
says hello world
'
,
function
()
{
expect
(
1
+
5
).
to
.
eql
(
6
)
})
This diff is collapsed.
Click to expand it.
test/get-cycle-day.spec.js
0 → 100644
+
136
−
0
View file @
3b2beabb
import
{
expect
}
from
'
chai
'
import
moment
from
'
moment
'
import
getCycleDayNumber
from
'
../get-cycle-day-number
'
describe
(
'
getCycleDay returns the cycle day
'
,
()
=>
{
it
(
'
if the last data entered is a bleeding day
'
,
function
()
{
const
cycleDays
=
[{
date
:
moment
([
2018
,
5
,
2
])
},
{
date
:
moment
([
2018
,
5
,
3
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
4
])
},
{
date
:
moment
([
2018
,
5
,
9
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
10
]),
bleeding
:
{
value
:
2
}
}]
const
targetDate
=
moment
([
2018
,
5
,
17
])
const
result
=
getCycleDayNumber
(
cycleDays
,
targetDate
)
expect
(
result
).
to
.
eql
(
9
)
})
it
(
'
if the last data entered is a non-bleeding day
'
,
function
()
{
const
cycleDays
=
[{
date
:
moment
([
2018
,
5
,
2
])
},
{
date
:
moment
([
2018
,
5
,
3
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
4
])
},
{
date
:
moment
([
2018
,
5
,
9
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
10
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
13
])
},
{
date
:
moment
([
2018
,
5
,
14
])
}]
const
targetDate
=
moment
([
2018
,
5
,
17
])
const
result
=
getCycleDayNumber
(
cycleDays
,
targetDate
)
expect
(
result
).
to
.
eql
(
9
)
})
it
(
'
works if the cycle days are not sorted by date
'
,
function
()
{
const
cycleDays
=
[{
date
:
moment
([
2018
,
5
,
13
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
9
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
3
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
4
])
},
{
date
:
moment
([
2018
,
5
,
10
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
2
])
}]
const
targetDate
=
moment
([
2018
,
5
,
17
])
const
result
=
getCycleDayNumber
(
cycleDays
,
targetDate
)
expect
(
result
).
to
.
eql
(
9
)
})
it
(
'
if there are only bleeding days
'
,
function
()
{
const
cycleDays
=
[{
date
:
moment
([
2018
,
5
,
9
]),
bleeding
:
{
value
:
2
}
},
{
date
:
moment
([
2018
,
5
,
10
]),
bleeding
:
{
value
:
2
}
}]
const
targetDate
=
moment
([
2018
,
5
,
17
])
const
result
=
getCycleDayNumber
(
cycleDays
,
targetDate
)
expect
(
result
).
to
.
eql
(
9
)
})
})
describe
(
'
getCycleDay returns undefined
'
,
()
=>
{
it
(
'
if there are no bleeding days
'
,
function
()
{
const
cycleDays
=
[{
date
:
moment
([
2018
,
5
,
2
])
},
{
date
:
moment
([
2018
,
5
,
4
])
},
{
date
:
moment
([
2018
,
5
,
9
]),
},
{
date
:
moment
([
2018
,
5
,
10
]),
}]
const
targetDate
=
moment
([
2018
,
5
,
17
])
const
result
=
getCycleDayNumber
(
cycleDays
,
targetDate
)
expect
(
result
).
to
.
be
.
undefined
})
it
(
'
if there are no cycle days
'
,
function
()
{
const
cycleDays
=
[]
const
targetDate
=
moment
([
2018
,
5
,
17
])
const
result
=
getCycleDayNumber
(
cycleDays
,
targetDate
)
expect
(
result
).
to
.
be
.
undefined
})
})
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment