Skip to content

Commit

Permalink
feat: adds mapfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Mar 29, 2022
1 parent b448864 commit 27f95bd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
31 changes: 24 additions & 7 deletions docs/src/routes/docs/datatypes/Array.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import DocItemTitle from "$lib/DocItemTitle.svelte";
import DocTitle from '$lib/DocTitle.svelte';
import DocTitle from "$lib/DocTitle.svelte";
import PageTitle from "$lib/PageTitle.svelte";
import PrismJs from "$lib/PrismJS.svelte";
import SinceVersion from "$lib/SinceVersion.svelte";
Expand Down Expand Up @@ -203,7 +203,9 @@ console.log(Array.get(2)(['a'])) // Error('Index 2 is out of bounds for length 1
<article>
<DocItemTitle title="get" />
<p>
Fills an array <code>b</code> with <code>a</code> from index <code>start</code> to <code>end</code>.
Fills an array <code>b</code> with <code>a</code> from index
<code>start</code>
to <code>end</code>.
</p>
<SinceVersion>3.3.0</SinceVersion>
<PrismJs
Expand Down Expand Up @@ -297,7 +299,8 @@ console.log(firstor([])) // 'not found'
<article>
<DocItemTitle title="filtermap" />
<p>
Maps an array <code>a</code> by removing all elements that do not satisfy the predicate <code>p</code>.
Maps an array <code>a</code> by removing all elements that do not satisfy
the predicate <code>p</code>.
</p>
<SinceVersion>3.0.0</SinceVersion>
<PrismJs
Expand Down Expand Up @@ -366,7 +369,8 @@ console.log(Array.fromfunctions([Num.add(1), Str.upper])([1, 'a'])) // ['2', 'A'
<article>
<DocItemTitle title="includes" />
<p>
Determines whether an array includes a certain element, returning true or false as appropriate.
Determines whether an array includes a certain element, returning true or
false as appropriate.
</p>
<SinceVersion>3.2.0</SinceVersion>
<PrismJs
Expand Down Expand Up @@ -458,9 +462,7 @@ console.log(Array.join('-')(['a', 'b', 'c'])) // 'a-b-c'

<article>
<DocItemTitle title="join" />
<p>
Creates a new array of a given size.
</p>
<p>Creates a new array of a given size.</p>
<SinceVersion>3.2.0</SinceVersion>
<PrismJs
code={`
Expand Down Expand Up @@ -491,6 +493,21 @@ console.log(Array.map(String.uconcat('!'))(['a', 'b', 'c'])) // ['a!', 'b!', 'c!
/>
</article>

<article>
<DocItemTitle title="mapfilter" />
<SinceVersion>3.6.0</SinceVersion>
<PrismJs
code={`
import { Array, Number } 'tiinvo';
const fm = Array.filtermap(Number.umul(2), Number.gt(3));
fm([1, 2, 3]) // [4, 6]
`}
language="ts"
/>
</article>

<article>
<DocItemTitle title="none" />
<p>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiinvo",
"version": "3.5.4",
"version": "3.6.0",
"author": "Paolo Roth <octod>",
"license": "MIT",
"description": "A lib of types and utilities for your TypeScript and JavaScript projects",
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ describe(`array`, () => {
expect(expect.arrayContaining(a.map(inc)([1, 2, 3]))).toEqual([2, 3, 4]);
});

test(a.mapfilter.name, () => {
const fm = a.mapfilter(n.umul(2), n.gt(3));

expect(expect.arrayContaining(fm([1, 2, 3]))).toEqual([4, 6]);
})

test(a.none.name, () => {
const p = n.isEven;

Expand Down
32 changes: 32 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export const first = <a>(a: a[]) => a[0] as option<a>
export const firstOr = <a>(a: a) => (b: a[]) => b[0] ?? a;
/**
* Maps an array `a` by removing all elements that do not satisfy the predicate `p`.
* The filter occurs before mapping the elements.
*
* ```typescript
* import { Array, Number } 'tiinvo';
Expand Down Expand Up @@ -466,6 +467,37 @@ export const make = <a = undefined>(size = 0, dv = undefined as unknown as a): a
* @since 3.0.0
*/
export const map = <a, b>(f: f.map<a, b>) => (a: a[]) => a.map(f);
/**
* Maps an array `a` by removing all elements that do not satisfy the predicate `p`.
* The filter occurs after mapping the elements.
*
* ```typescript
* import { Array, Number } 'tiinvo';
*
* const fm = Array.filtermap(Number.umul(2), Number.gt(3));
*
* fm([1, 2, 3]) // [4, 6]
* ```
*
* @param f
* @param p
* @returns
* @since 3.6.0
*/
export const mapfilter = <a, b>(f: f.map<a, b>, p: f.predicateE<b>) => (a: a[]) => {
const r = [] as b[];

for (let i = 0; i < a.length; i++) {
const v = a[i];
const m = f(v);

if (p(m)) {
r.push(m);
}
}

return r;
}
/**
* Returns true if all elements of `a` do not meet the condition specified in the predicate `p`.
*
Expand Down

1 comment on commit 27f95bd

@vercel
Copy link

@vercel vercel bot commented on 27f95bd Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.