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
00a7de0f
Commit
00a7de0f
authored
6 years ago
by
tina
Browse files
Options
Downloads
Patches
Plain Diff
little improvements and test for array of one element
parent
9664984e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/period-length.js
+12
-9
12 additions, 9 deletions
lib/period-length.js
test/periode-length.spec.js
+13
-1
13 additions, 1 deletion
test/periode-length.spec.js
with
25 additions
and
10 deletions
lib/period-length.js
+
12
−
9
View file @
00a7de0f
...
...
@@ -9,25 +9,28 @@ export default function getPeriodLengthStats(cycleLengths) {
periodLengthStats
.
minimum
=
sortedCycleLengths
[
0
]
periodLengthStats
.
maximum
=
sortedCycleLengths
[
cycleLengths
.
length
-
1
]
periodLengthStats
.
mean
=
Math
.
round
(
cycleLengths
.
reduce
(
getSum
)
/
cycleLengths
.
length
*
100
)
/
100
cycleLengths
.
reduce
(
getSum
)
/
cycleLengths
.
length
*
100
)
/
100
// median
if
(
cycleLengths
.
length
%
2
==
1
)
{
periodLengthStats
.
median
=
sortedCycleLengths
[
(
cycleLengths
.
length
+
1
)
/
2
-
1
]
}
else
{
periodLengthStats
.
median
=
sortedCycleLengths
[
(
cycleLengths
.
length
+
1
)
/
2
-
1
]
}
else
{
const
middle
=
cycleLengths
.
length
/
2
periodLengthStats
.
median
=
(
sortedCycleLengths
[
middle
-
1
]
+
sortedCycleLengths
[
middle
])
/
2
}
// corrected standard deviation (based on unbiased sample variance)
periodLengthStats
.
stdDeviation
=
null
// for case t
if
(
cycleLengths
.
length
>
1
)
{
const
sumOfSquares
=
cycleLengths
.
map
(
cycleLength
=>
{
return
Math
.
pow
(
cycleLength
-
periodLengthStats
.
mean
,
2
)
}).
reduce
(
getSum
)
periodLengthStats
.
stdDeviation
=
Math
.
round
(
Math
.
sqrt
(
sumOfSquares
/
(
cycleLengths
.
length
-
1
))
*
100
)
/
100
Math
.
sqrt
(
sumOfSquares
/
(
cycleLengths
.
length
-
1
))
*
100
)
/
100
}
else
{
periodLengthStats
.
stdDeviation
=
null
}
return
periodLengthStats
}
...
...
@@ -37,10 +40,10 @@ function getSum(total, num) {
}
function
throwIfArgsAreNotInRequiredFormat
(
cycleLengths
)
{
assert
.
ok
(
Array
.
isArray
(
cycleLengths
),
'
Function requires input to
be an array.
'
)
assert
.
ok
(
Array
.
isArray
(
cycleLengths
),
'
Input should
be an array.
'
)
assert
.
ok
(
cycleLengths
.
length
>
0
,
'
Input array should not be empty.
'
)
cycleLengths
.
forEach
(
cycleLength
=>
{
assert
.
equal
(
typeof
cycleLength
,
'
number
'
,
'
Elements in the array should be of type number.
'
)
assert
.
ok
(
!
isNaN
(
cycleLength
),
'
Elements of
A
rray should not be NaN.
'
)
assert
.
ok
(
!
isNaN
(
cycleLength
),
'
Elements of
a
rray should not be NaN.
'
)
})
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
test/periode-length.spec.js
+
13
−
1
View file @
00a7de0f
...
...
@@ -5,7 +5,7 @@ import periodInfo from '../lib/period-length'
const
expect
=
chai
.
expect
describe
(
'
getPeriodLengthStats
'
,
()
=>
{
describe
.
only
(
'
getPeriodLengthStats
'
,
()
=>
{
it
(
'
works for a simple odd-numbered array
'
,
()
=>
{
const
periodLengths
=
[
99
,
5
,
1
,
2
,
100
]
const
result
=
periodInfo
(
periodLengths
)
...
...
@@ -31,6 +31,18 @@ describe('getPeriodLengthStats', () => {
}
expect
(
result
).
to
.
eql
(
expectedResult
)
})
it
(
'
works for an one-element array
'
,
()
=>
{
const
periodLengths
=
[
42
]
const
result
=
periodInfo
(
periodLengths
)
const
expectedResult
=
{
minimum
:
42
,
maximum
:
42
,
mean
:
42
,
median
:
42
,
stdDeviation
:
null
}
expect
(
result
).
to
.
eql
(
expectedResult
)
})
describe
(
'
when args are wrong
'
,
()
=>
{
it
(
'
throws when arg object is an empty array
'
,
()
=>
{
const
periodLengths
=
[]
...
...
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