Skip to content

Commit

Permalink
Add pipes
Browse files Browse the repository at this point in the history
Remove async functions
Reorganise exports
  • Loading branch information
laurence79 committed Jan 16, 2025
1 parent b7e45bc commit b263988
Show file tree
Hide file tree
Showing 180 changed files with 2,281 additions and 1,841 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ on:
push:
branches:
- main
- next
- alpha
- beta

pull_request:
branches:
- main
- next
- alpha
- beta

concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
Expand Down
5 changes: 4 additions & 1 deletion .releaserc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ branches:
- '+([0-9])?(.{+([0-9]),x}).x'
- 'main'

- name: 'next'
- name: 'alpha'
prerelease: true

- name: 'beta'
prerelease: true

tagFormat: ${version}
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"cSpell.language": "en-GB",
"cSpell.words": [
"cume",
"pipeable",
"rivr",
"ttsc",
"ttypescript"
Expand Down
103 changes: 68 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,103 @@
# Typescript Array Extensions

A library that adds common array higher order function support beyond the built
in functions in js, e.g. `map`, `filter`, by extending the `Array` prototype.
A library that provides common array transformation functions beyond the built
in ones in js, e.g. `map`, `filter`.

Inspired by swift and LINQ.

## Installing

## Install
Use your package manager of choice
```sh
npm i ts-array-extensions
```
```sh
yarn add ts-array-extensions
```
```sh
pnpm i ts-array-extensions
```

## Using

You can either
1. Extend `Array.prototype` with utility imports.
2. Import the functions directly.
3. Use pipes.

### 1. Extending the `Array` prototype

You can `import` all the functions

```ts
import 'ts-array-extensions';
import 'ts-array-extensions/applyToPrototype';

[1, 2].cume(); // [1, 3]
```

or individual ones as you need them, then the rest can be shaken out of the
tree by your bundler

```ts
import 'ts-array-extensions/applyToPrototype/compactMap';

[1, 2, 3].compactMap(v => {
if (v % 2 !== 0) return v;
});
// [1, 3]
```

or individual ones as you need them
### 2. Importing the functions directly

You can `import` the functions and use them directly

```ts
import 'ts-array-extensions/compactMap';
import { cume, compactMap } from 'ts-array-extensions';

cume([1, 2]); // [1, 3]

compactMap(
[1, 2, 3],
v => {
if (v % 2 !== 0) return v;
}
);
// [1, 3]
```

## Array.prototype extensions
### 3. Use pipes

Avoid prototype pollution, but almost as easy to follow.

Will be even better when the javascript [pipeline operator proposal](https://github.com/tc39/proposal-pipeline-operator) lands in typescript

```ts
import { pipe, cume, compactMap } from 'ts-array-extensions/pipes';

pipe(
[1, 2, 3],
compactMap(v => {
if (v % 2 !== 0) return v;
}),
cume()
)
// [1, 4]
```

## Methods

The following examples use the `Array.prototype` extension mode, but all can also be used with the direct import and pipe modes.

- [any](#any)
- [compact](#compact)
- [compactMap](#compactmap)
- [compactMapAsync](#compactmapasync)
- [cume](#cume)
- [distinct](#distinct)
- [except](#except)
- [first](#first)
- [forEachAsync](#foreachasync)
- [groupBy](#groupby)
- [innerJoin](#innerjoin)
- [interleave](#interleave)
- [leftJoin](#leftjoin)
- [mapAsync](#mapasync)
- [max](#max)
- [min](#min)
- [none](#none)
Expand Down Expand Up @@ -163,17 +220,6 @@ Returns the first element of the array or `null` if empty.
// 'morning'
```

### forEachAsync

Same as [forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) but works with promises. Will await `async`
callbacks.

```ts
await [1, 2, 3].forEachAsync(async v => {
await /* something */
});
```

### groupBy

Returns an array of `Group<K, V>` extracted from the array using a callback, and
Expand Down Expand Up @@ -286,19 +332,6 @@ leftData.leftJoin(rightData, (l, r) => l.groupId === r.id);
// ];
```

### mapAsync

Same as [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) but works with promises. Will await `async`
callbacks.

```ts
await [1, 2, 3].map(async v => {
await /* something */
return v * 2;
});
// [2, 4, 6]
```

### max

Returns the highest element in the array
Expand Down
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default tseslint.config(
'import-x/no-named-as-default-member': 'off',
'@typescript-eslint/unified-signatures': 'off',
'@typescript-eslint/consistent-type-definitions': 'off',
'@typescript-eslint/no-unnecessary-type-parameters': 'off'
'@typescript-eslint/no-unnecessary-type-parameters': 'off',
'@typescript-eslint/unbound-method': 'off'
}
},

Expand Down
Loading

0 comments on commit b263988

Please sign in to comment.