Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
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
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SmartDataLab
public
applications
renovate
Commits
0d6a8882
Unverified
Commit
0d6a8882
authored
2 years ago
by
Sergei Zharinov
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
feat: Add `filterMap` utility for fast array transforms (#23252)
parent
54dcdf01
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/util/filter-map.spec.ts
+17
-0
17 additions, 0 deletions
lib/util/filter-map.spec.ts
lib/util/filter-map.ts
+26
-0
26 additions, 0 deletions
lib/util/filter-map.ts
with
43 additions
and
0 deletions
lib/util/filter-map.spec.ts
0 → 100644
+
17
−
0
View file @
0d6a8882
import
{
filterMap
}
from
'
./filter-map
'
;
describe
(
'
util/filter-map
'
,
()
=>
{
it
(
'
should return an empty array when given an empty array
'
,
()
=>
{
const
input
:
unknown
[]
=
[];
const
output
=
filterMap
(
input
,
()
=>
42
);
expect
(
output
).
toBe
(
input
);
expect
(
output
).
toBeEmpty
();
});
it
(
'
should return an array with only the mapped values that pass the filter
'
,
()
=>
{
const
input
=
[
0
,
1
,
2
,
3
,
4
];
const
output
=
filterMap
(
input
,
(
n
)
=>
n
*
n
);
expect
(
output
).
toBe
(
input
);
expect
(
output
).
toEqual
([
1
,
4
,
9
,
16
]);
});
});
This diff is collapsed.
Click to expand it.
lib/util/filter-map.ts
0 → 100644
+
26
−
0
View file @
0d6a8882
import
is
from
'
@sindresorhus/is
'
;
type
Falsy
=
false
|
''
|
0
|
0
n
|
null
|
undefined
;
/**
* Filter and map an array *in place* with single iteration.
*/
export
function
filterMap
<
T
,
U
>
(
array
:
T
[],
fn
:
(
item
:
T
)
=>
Falsy
|
U
):
U
[]
{
const
length
=
array
.
length
;
let
newIdx
=
0
;
for
(
let
oldIdx
=
0
;
oldIdx
<
length
;
oldIdx
+=
1
)
{
const
item
=
array
[
oldIdx
];
const
res
=
fn
(
item
);
if
(
is
.
truthy
(
res
))
{
array
[
newIdx
]
=
res
as
never
;
newIdx
+=
1
;
}
}
const
deletedCount
=
length
-
newIdx
;
if
(
deletedCount
)
{
array
.
length
=
length
-
deletedCount
;
}
return
array
as
never
;
}
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