Skip to content

Commit

Permalink
Merge pull request #6 from seangwright/feat/pipeable-operators
Browse files Browse the repository at this point in the history
  • Loading branch information
seangwright committed Jan 11, 2022
2 parents ff85358 + 4baa057 commit bf9e148
Show file tree
Hide file tree
Showing 14 changed files with 1,826 additions and 1,393 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,36 @@ const maybe = Maybe.none<Food>()
}); // None!
```

#### Pipe

```typescript
// custom-operators.ts
import { logger, LogLevel } from 'logger';

export function log<TValue>(
messageCreator: FunctionOfTtoK<TValue, string>,
logLevel: LogLevel = 'debug'
): MaybeOpFn<TValue, TValue> {
return (maybe) => {
if (maybe.hasValue) {
logger.log(messageCreator(maybe.getValueOrThrow()), logLevel);
} else {
logger.error('No value found!');
}

return maybe;
};
}

// app.ts
import { log } from './custom-operators.ts';

const maybe = Maybe.some('apple')
.pipe(log((f) => `My fruit is ${f}`, 'information'))
.map((f) => `${f} and banana`)
.pipe(log((f) => `Now I have ${f}`));
```

### MaybeAsync

`MaybeAsync` represents a future value (`Promise`) that might or might not exist.
Expand Down
7 changes: 5 additions & 2 deletions jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
export default {
import { InitialOptionsTsJest } from 'ts-jest';

const config: InitialOptionsTsJest = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: {
Expand All @@ -14,3 +15,5 @@ export default {
setupFilesAfterEnv: ['./test/expectExtensions.ts'],
extensionsToTreatAsEsm: ['.ts'],
};

export default config;

0 comments on commit bf9e148

Please sign in to comment.