Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yarnaimo/lifts
⛵ LIghtweight Functional programming library for TypeScript
https://github.com/yarnaimo/lifts
functional-programming typescript
Last synced: about 1 month ago
JSON representation
⛵ LIghtweight Functional programming library for TypeScript
- Host: GitHub
- URL: https://github.com/yarnaimo/lifts
- Owner: yarnaimo
- License: mit
- Created: 2020-04-30T12:32:20.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-07-27T03:42:04.000Z (over 4 years ago)
- Last Synced: 2024-09-19T16:36:41.190Z (about 2 months ago)
- Topics: functional-programming, typescript
- Language: TypeScript
- Homepage:
- Size: 201 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lifts
**LI**ghtweight **F**unctional programming library for **T**ype**S**cript
## Install
```sh
yarn add lifts remeda
# or
npm i -S lifts remeda
```## Usage
### Do
Shorthand for Immediately Invoked Function Expression
```ts
const date = new Date('2020-04-17')const result: number | null = Do(() => {
// check date is valid
if (!isNaN(date)) {
return date.getDate()
} else {
return null
}
})
result // => 17// equivalent to
const result = (() => {
if (!isNaN(date)) {
return date.getDate()
} else {
return null
}
})()
```### Switch
Object-Style `switch`
```ts
const fn = (value: string) =>
Switch(value)(
{
a: () => 'String A',
b: () => 'String B',
},
() => null, // default value
)fn('a') // => 'string A'
fn('b') // => 'string B'
fn('c') // => null (default value)
```### Result
```ts
const parseDate = (dateStr: string): IResult => {
const date = new Date(dateStr)// check date is valid
if (!isNaN(date)) {
return Result.ok(date)
} else {
return Result.err(new Error('Invalid Date'))
}
}parseDate('2020-04-17')
// => { isOk: true, valueOrError: Date('2020-04-17') }parseDate('foo')
// => { isOk: false, valueOrError: Error('Invalid Date') }
```#### Result.switch
```ts
const result = parseDate('2020-04-17')Result.switch(result)({
ok: (value: Date) => {
// called if result is Ok
return value.getDate()
},
err: (error: Error) => {
// called if result is Err
return null
},
})
// => 17
``````ts
const result = parseDate('foo')Result.switch(result)({
ok: (value) => {
return value.getDate()
},
err: (error) => {
return null
},
})
// => null
```#### Result.wrap
Wraps Error with `Result.err()` if error caught, else wraps value with `Result.ok()`.
```ts
const result = Result.wrap(() => {
if (condition) {
return true
} else {
throw new Error()
}
})
```### MapAsync
### Pipe