Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 158 additions & 94 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
"@types/jest": "^25.1.3",
"@types/node": "^13.7.4",
"codecov": "^3.6.5",
"fast-check": "^1.22.2",
"fs-extra": "^8.1.0",
"istanbul": "^0.4.5",
"jest": "25.1.0",
"jest-junit": "^10.0.0",
"rollup": "^1.31.1",
"rxjs": "^6.5.4",
"semantic-release": "^17.0.4",
"terser": "^4.6.3",
"ts-jest": "^25.2.1",
Expand All @@ -53,7 +55,8 @@
"typescript": "^3.7.5"
},
"peerDependencies": {
"tslib": "^1.11.0"
"tslib": "^1.11.0",
"rxjs": "^6.0"
},
"jest": {
"testURL": "http://localhost",
Expand Down
12 changes: 12 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,26 @@ export default [
name: 'monads',
file: `dist/${pkg.main}`,
format: 'umd',
globals: {
rxjs: 'rxjs',
'rxjs/operators': 'rxjs/operators'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of wondering if this is a valid way to provide the global symbol for rxjs operators, or if it should be shortened to operators or something.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results in this in the UMD output:

(global = global || self, factory(global.monads = {}, global.rxjs, global['rxjs/operators']));

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the answer to this one. I think the way you have it is good.

},
sourcemap: true
},
external: [
'rxjs',
'rxjs/operators'
]
},
{
input: 'src/index.ts',
output: [
{ file: `dist/${pkg.module}`, format: 'es', sourcemap: true },
{ file: `dist/${pkg.commonJs}`, format: 'cjs', sourcemap: true }
],
external: [
'rxjs',
'rxjs/operators'
],
plugins: [typescript()]
}]
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './monads/index'
export * from './interfaces/index'
export {
maybeToObservable
} from './util/index'
5 changes: 4 additions & 1 deletion src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ export {
curry5,
curry6,
curry7
} from './curry'
} from './curry'
export {
maybeToObservable
} from './maybe/maybe-to-observable'
4 changes: 2 additions & 2 deletions src/util/maybe-env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reader, maybe } from '..'
import { reader, maybe } from '../monads/index'

export interface GetFromEnvironmentReader {
readEnv(key: string): string | undefined
Expand All @@ -15,4 +15,4 @@ const maybeFromEnvReader = (key: string) => (reader: GetFromEnvironmentReader) =
export function maybeEnv(key: string, config?: GetFromEnvironmentReader) {
return reader(maybeFromEnvReader(key))
.run(maybe(config).valueOr(DEFAULT_NODE_ENV_READER))
}
}
28 changes: 28 additions & 0 deletions src/util/maybe/maybe-to-observable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { IMaybe } from '../../interfaces'
import { EMPTY, Observable, of } from 'rxjs'
import { take } from 'rxjs/operators'

/**
* Convert a Maybe into an observable
*
* If the Maybe is empty, the observable will immediately complete without emitting a value, otherwise it will emit
* the value contained and complete.
*
* @requires rxjs@^6.0
* @example
* of(maybe(5)).pipe(
* flatMap(maybeToObservable)
* ).subscribe(a => console.log(a))
* // prints 5 and completes
*
* of(maybe()).pipe(
* flatMap(maybeToObservable)
* ).subscribe(a => console.log(a))
* // immediately completes with no emitted value
*/
export const maybeToObservable = <A>(m: IMaybe<A>): Observable<A> => {
return m.isNone()
? EMPTY
: of(m.valueOrThrow('isNone returned false for empty IMaybe.'))
.pipe(take(1))
}
38 changes: 38 additions & 0 deletions test/util/maybe/maybe-to-observable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { assert, integer, property } from 'fast-check'
import { maybe } from '../../../src/monads'
import { maybeToObservable } from '../../../src/util'
import { merge, of } from 'rxjs'
import { count } from 'rxjs/operators'

describe('maybeToObservable', () => {
const numRuns = 100
it('emits a value when containing a value', () => {
expect.assertions(numRuns)
assert(
property(
integer(),
a => {
const m = maybe(a)
const o = maybeToObservable(m)
o.subscribe(val => expect(val).toBe(a))
}
), {
numRuns
}
)
})

it('immediately completes if there is no contained value', done => {

const m = maybe()
const o = maybeToObservable(m)
const c = of(1)

merge(o, c)
.pipe(count())
.subscribe(count => {
expect(count).toBe(1)
done()
})
})
})