Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Rex Dri
Rex Dri
Commits
04b08024
Verified
Commit
04b08024
authored
Apr 13, 2020
by
Florent Chehab
Browse files
feat(frontend utils): created counter class
* also added tests
parent
01265070
Changes
2
Hide whitespace changes
Inline
Side-by-side
frontend/src/utils/counter.js
0 → 100644
View file @
04b08024
/**
* Helper class just like the one in python
*/
class
Counter
{
/**
* @param {Iterable} elements
*/
constructor
(
elements
)
{
const
res
=
new
Map
();
elements
.
forEach
(
el
=>
{
if
(
!
res
.
has
(
el
))
{
res
.
set
(
el
,
0
);
}
res
.
set
(
el
,
res
.
get
(
el
)
+
1
);
});
/**
* @type {Map<any, number>}
*/
this
.
res
=
res
;
}
/**
* return the keys that have duplicates
* @returns {Array.<any>}
*/
getDuplicatedElements
()
{
return
[...
this
.
res
.
entries
()]
.
filter
(([,
count
])
=>
count
>
1
)
.
map
(([
key
])
=>
key
);
}
}
export
default
Counter
;
frontend/src/utils/counter.test.js
0 → 100644
View file @
04b08024
import
Counter
from
"
./counter
"
;
test
(
"
Count nothing empty string
"
,
()
=>
{
const
counter
=
new
Counter
([]);
expect
(
counter
.
res
).
toMatchObject
(
new
Map
());
});
test
(
"
Count one element
"
,
()
=>
{
const
counter
=
new
Counter
([
"
coucou
"
]);
expect
(
counter
.
res
).
toMatchObject
(
new
Map
([[
"
coucou
"
,
1
]]));
});
test
(
"
Count twice element
"
,
()
=>
{
const
counter
=
new
Counter
([
"
coucou
"
,
"
coucou
"
]);
expect
(
counter
.
res
).
toMatchObject
(
new
Map
([[
"
coucou
"
,
2
]]));
});
test
(
"
Count two elements diff
"
,
()
=>
{
const
counter
=
new
Counter
([
"
coucou
"
,
"
coucou
"
,
"
test
"
]);
expect
(
counter
.
res
).
toMatchObject
(
new
Map
([[
"
coucou
"
,
2
],
[
"
test
"
,
1
]]));
});
test
(
"
Test show duplicated
"
,
()
=>
{
const
counter
=
new
Counter
([
"
coucou
"
,
"
coucou
"
,
"
test
"
]);
expect
(
counter
.
getDuplicatedElements
()).
toMatchObject
([
"
coucou
"
]);
});
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment