Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
525ac637
Commit
525ac637
authored
May 25, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
May 25, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate collections module to TS
parent
fc3c29a4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
22 deletions
+9
-22
collections.ts
src/sidebar/util/collections.ts
+9
-22
No files found.
src/sidebar/util/collections.
j
s
→
src/sidebar/util/collections.
t
s
View file @
525ac637
...
...
@@ -4,13 +4,8 @@
/**
* Return the number of elements in `ary` for which `predicate` returns true.
*
* @template T
* @param {T[]} ary
* @param {(item: T) => boolean} predicate
* @return {number}
*/
export
function
countIf
(
ary
,
predicate
)
{
export
function
countIf
<
T
>
(
ary
:
T
[],
predicate
:
(
item
:
T
)
=>
boolean
):
number
{
return
ary
.
reduce
((
count
,
item
)
=>
{
return
predicate
(
item
)
?
count
+
1
:
count
;
},
0
);
...
...
@@ -19,24 +14,18 @@ export function countIf(ary, predicate) {
/**
* Convert an array of strings into an object mapping each array entry
* to `true`.
*
* @param {string[]} arr
* @return {Record<string,true>}
*/
export
function
toTrueMap
(
arr
)
{
const
obj
=
/** @type {Record<string,true>} */
({})
;
export
function
toTrueMap
(
arr
:
string
[]):
Record
<
string
,
true
>
{
const
obj
:
Record
<
string
,
true
>
=
{}
;
arr
.
forEach
(
key
=>
(
obj
[
key
]
=
true
));
return
obj
;
}
/**
* Utility function that returns all
of
the properties of an object whose
* Utility function that returns all the properties of an object whose
* value is `true`.
*
* @param {Record<string, boolean>} obj
* @return {string[]}
*/
export
function
trueKeys
(
obj
)
{
export
function
trueKeys
(
obj
:
Record
<
string
,
boolean
>
):
string
[]
{
return
Object
.
keys
(
obj
).
filter
(
key
=>
obj
[
key
]
===
true
);
}
...
...
@@ -45,11 +34,9 @@ export function trueKeys(obj) {
* `Record<Key, Value>`.
*
* Unlike `Object.entries`, this preserves the type of the key.
*
* @template {string|number|symbol} Key
* @template Value
* @param {Record<Key, Value>} object
*/
export
function
entries
(
object
)
{
return
/** @type {[Key, Value][]} */
(
Object
.
entries
(
object
));
export
function
entries
<
Key
extends
string
|
number
|
symbol
,
Value
>
(
object
:
Record
<
Key
,
Value
>
):
[
Key
,
Value
][]
{
return
Object
.
entries
(
object
)
as
[
Key
,
Value
][];
}
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