Skip to content

Commit

Permalink
Merge pull request #34 from VKTRenokh/develop
Browse files Browse the repository at this point in the history
minor
  • Loading branch information
VKTRenokh committed Apr 1, 2024
2 parents 0c6bf0c + d599520 commit dc811d2
Show file tree
Hide file tree
Showing 69 changed files with 4,199 additions and 252 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-falcons-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@victorenokh/maybe.ts": minor
---

Implemented Task, Task Either, Task Maybe monads, implemented prisms, multiple function, namespace autoimport
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
8 changes: 0 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,4 @@ module.exports = {
],
'block-spacing': ['warn', 'always'],
},
overrides: [
{
files: ['*.ts', '*.tsx'],
rules: {
'@typescript-eslint/indent': ['error', 2],
},
},
],
}
12 changes: 11 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@
"tabWidth": 2,
"singleQuote": true,
"printWidth": 60,
"bracketSpacing": true
"bracketSpacing": true,
"importOrder": [
"^-->$",
"^->t/(.*)$",
"^->/(.*)$",
"^[./]"
],
"importOrderSortSpecifiers": true,
"plugins": [
"@trivago/prettier-plugin-sort-imports"
]
}
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,43 @@
To install the latest version:
`npm i @victorenokh/maybe.ts`

## Namespaces auto import
Install ts-namespace-import-plugin:
`npm i -D @unsplash/ts-namespace-import-plugin`

Extend from namespaces tsconfig:
```json
// tsconfig.json
{
"extends": "./node_modules/@victorenokh/maybe.ts/tsconfig.namespaces.json"
}
```

Use project typescript version in vscode press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>p</kbd> then running **TypeScript: Select TypeScript Version** command, and selectin **Use Workspace Version** refering to [this](https://github.com/unsplash/ts-namespace-import-plugin/issues/12#issuecomment-1836965622)

# Examples

- Imagine you have a piece of text that represents data in JSON format (like a configuration file or an API response).
- The parse function takes this text as input.
- Inside the function, it tries to convert the text into a JavaScript object using JSON.parse.
- If successful, it returns the either `right` parsed object.
- If there’s an issue (for example, the text isn’t valid JSON), it returns either `left` containing Error instead.
- We are using toError function that takes anything then stringifies it and
wraps in `Error` to handle catch block
- Why Use This?
- The tryCatch approach is useful because it gracefully handles potential errors without crashing our program using `Either` monad.
It’s like saying, “Hey, let’s try to parse this JSON, and if anything goes wrong, we’ll handle it gracefully.”
```ts
import { tryCatch, toError } from '@victorenokh/maybe.ts/either'

const parse = (text: string) => {
return tryCatch(
() => JSON.parse(text),
toError
)
}
```

- [Maybe](https://maybets.duckdns.org/examples/maybe.html)
- [State](https://maybets.duckdns.org/examples/state.html)
- [Either](https://maybets.duckdns.org/examples/either.html)
Expand All @@ -19,6 +55,7 @@ To install the latest version:
- [ReaderT](https://maybets.duckdns.org/examples/readerT.html)
- [Lens](https://maybets.duckdns.org/examples/lens.html)
- [Prism](https://maybets.duckdns.org/examples/prisms.html)
- [Task Either](https://maybets.duckdns.org/examples/task-either.html)

# License
The MIT License (MIT)
30 changes: 30 additions & 0 deletions esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const esbuild = require('esbuild')
const { replaceTscAliasPaths } = require('tsc-alias')
/**
* @type {esbuild.BuildOptions}
*/
const baseOptions = {
platform: 'node',
outdir: 'dist',
plugins: [],
entryPoints: ['src/*.ts', 'src/**/*.ts'],
}

esbuild.build({
...baseOptions,
outExtension: { '.js': '.mjs' },
format: 'esm',
})

esbuild.build({
...baseOptions,
format: 'cjs',
})

replaceTscAliasPaths({
configFile: 'tsconfig.json',
watch: false,
outDir: 'dist',
declarationDir: 'dist',
fileExtensions: '.mjs',
})
4 changes: 2 additions & 2 deletions examples/observable.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
## .map()
```ts
const observable = O.of(42)
observable.map((num) => num.toString()).observe(console.log()) // Output: "42"
observable.map((num) => num.toString()).observe(console.log) // Output: "42"
```

## .flatMap()
```ts
const observable = O.of(42)
observable.flatMap(num => O.of(num + 1)).observe(console.log()) // Output: 43
observable.flatMap(num => O.of(num + 1)).observe(console.log) // Output: 43
```


Expand Down
29 changes: 29 additions & 0 deletions examples/task-either.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Task Either

You can think of it like a function that return promise of either.

## tryCatch()
```ts
import { toError } from "@victorenokh/maybe.ts/either"
import { tryCatch } from "@victorenokh/maybe.ts/task-either"

const parseOrThrow = (res: Response) => {
if (!res.ok) {
throw new Error(res.statusText)
}

return res.text()
}

const fetchString = (url: string) =>
tryCatch(() => fetch(url).then(parseOrThrow), toError)

fetchString('https://example.com').fold(
console.error,
console.log,
) // Output: html string of example.com site
fetchString('https://example.com/non/existing/path').fold(
console.error,
console.log,
) // Ouput: Error: internal server error
```
5 changes: 5 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ const config: Config.InitialOptions = {
preset: 'ts-jest',
verbose: true,
testEnvironment: 'node',
moduleNameMapper: {
'->/(.*)': '<rootDir>/src/$1',
'-->': '<rootDir>/src/index.ts',
'->t/(.*)': '<rootDir/types/$1>',
},
}
export default config

0 comments on commit dc811d2

Please sign in to comment.