https://github.com/steelydylan/if-method-chain
https://github.com/steelydylan/if-method-chain
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/steelydylan/if-method-chain
- Owner: steelydylan
- License: mit
- Created: 2020-04-12T16:19:56.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-18T11:04:53.000Z (about 6 years ago)
- Last Synced: 2025-05-10T01:45:05.577Z (about 1 year ago)
- Language: TypeScript
- Size: 64.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# if-method-chain
## Installation
```sh
$ npm install if-method-chain
```
## Usage
```ts
import { ifMethodChain } from 'if-method-chain';
const array = [1, 2, 3, 4, 5];
const result = ifMethodChain(array, [
(item) => item.map(item => item * 2),
(item) => item.map(item => item - 1)
]);
console.log(array) // [1, 3, 5, 7, 9]
```
if false, it will skip the conditional function.
```ts
import { ifMethodChain } from 'if-method-chain';
const result = ifMethodChain(array, [
(item) => item.map(item => item * 2),
[(item) => item.map(item => item - 1), false]
]);
console.log(array) // [2, 4, 6, 8, 10]
```
if the result is false, it will skipe the conditional function.
```ts
import { ifMethodChain } from 'if-method-chain';
const array = [1, 2, 3, 4, 5];
const result = ifMethodChain(array, [
(item) => item.map(item => item * 2),
[(item) => item.map(item => item - 1), (items) => items.some(item => item === 100)]
]);
console.log(array) // [2, 4, 6, 8, 10]
```
## Use Case
with firebase
```js
import { ifMethodChain } from 'if-method-chain';
type ShapShot = firebase.firestore.QuerySnapshot;
type Query = firebase.firestore.Query;
const searchWithUid = true;
const useStartAfter = true;
const snapshot = await ifMethodChain>(
db.collection("blog"),
[
(item: Query) => item.orderBy("createdAt", "desc"),
[(item: Query) => item.where("authorId", "==", uid), searchWithUid],
[(item: Query) => item.startAfter(startAt), useStartAfter],
(item: Query) => item.limit(5),
(item: Query) => item.get()
]
);
```