Skip to content

Commit d38a0aa

Browse files
fix(maybe): switch to dynamic import for rxjs in Maybe methods (#206)
Replaced `require` calls with dynamic `import` for `rxjs` within the `Maybe` class. This change modernizes the code by leveraging ES module syntax, enhancing module loading efficiency and ensuring compatibility with environments that prefer or enforce ES modules over CommonJS. Additionally, cleaned up imports in the test file `maybe.spec.ts` by removing redundant object destructuring.
1 parent e0b8a12 commit d38a0aa

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

src/maybe/maybe.spec.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { maybe, none } from './public_api'
22
import { Maybe } from './maybe'
3+
import { EMPTY, of, throwError } from 'rxjs'
34

45
describe('Maybe', () => {
56
describe('when returning a value with possible throw', () => {
@@ -721,7 +722,6 @@ describe('Maybe', () => {
721722

722723
describe('flatMapObservable', () => {
723724
it('should handle a Some value with emitting observable', (done) => {
724-
const { of } = require('rxjs')
725725
const hasSome = maybe(42)
726726
hasSome.flatMapObservable(value => of(`Value: ${value}`))
727727
.then(result => {
@@ -732,7 +732,6 @@ describe('Maybe', () => {
732732
})
733733

734734
it('should handle a Some value with empty observable', (done) => {
735-
const { EMPTY } = require('rxjs')
736735
const hasSome = maybe(42)
737736
hasSome.flatMapObservable(() => EMPTY)
738737
.then(result => {
@@ -742,7 +741,6 @@ describe('Maybe', () => {
742741
})
743742

744743
it('should handle a Some value with erroring observable', (done) => {
745-
const { throwError } = require('rxjs')
746744
const hasSome = maybe(42)
747745
hasSome.flatMapObservable(() => throwError(() => new Error('test error')))
748746
.then(result => {
@@ -752,7 +750,6 @@ describe('Maybe', () => {
752750
})
753751

754752
it('should handle a None value', (done) => {
755-
const { of } = require('rxjs')
756753
const hasNone = maybe<number>()
757754
hasNone.flatMapObservable(value => of(`Value: ${value}`))
758755
.then(result => {
@@ -781,7 +778,6 @@ describe('Maybe', () => {
781778
})
782779

783780
it('should create a Maybe from Observable with static fromObservable', (done) => {
784-
const { of } = require('rxjs')
785781
Maybe.fromObservable(of(42))
786782
.then(result => {
787783
expect(result.isSome()).toBe(true)
@@ -791,7 +787,6 @@ describe('Maybe', () => {
791787
})
792788

793789
it('should create a Maybe from empty Observable with static fromObservable', (done) => {
794-
const { EMPTY } = require('rxjs')
795790
Maybe.fromObservable(EMPTY)
796791
.then(result => {
797792
expect(result.isNone()).toBe(true)
@@ -800,7 +795,6 @@ describe('Maybe', () => {
800795
})
801796

802797
it('should create a Maybe from erroring Observable with static fromObservable', (done) => {
803-
const { throwError } = require('rxjs')
804798
Maybe.fromObservable(throwError(() => new Error('test error')))
805799
.then(result => {
806800
expect(result.isNone()).toBe(true)

src/maybe/maybe.ts

+16-21
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { IResult } from '../result/result.interface'
22
import type { IMaybePattern, IMaybe } from './maybe.interface'
33
import { FailResult, OkResult } from '../result/result'
44

5-
export class Maybe<T> implements IMaybe<T> {
5+
export class Maybe<T> implements IMaybe<T> {
66
constructor(private readonly value?: T | null) { }
7-
7+
88
public of(value: T): IMaybe<T> {
99
return new Maybe<T>(value)
1010
}
@@ -98,19 +98,18 @@ export class Maybe<T> implements IMaybe<T> {
9898
* .then(name => displayUserName(name));
9999
*/
100100
public static fromObservable<T>(observable: import('rxjs').Observable<T>): Promise<IMaybe<NonNullable<T>>> {
101-
const { firstValueFrom, EMPTY } = require('rxjs')
102-
const { take, map, catchError } = require('rxjs/operators')
103-
104-
return firstValueFrom(
105-
observable.pipe(
106-
take(1),
107-
map((value: unknown) => new Maybe<NonNullable<T>>(value as NonNullable<T>)),
108-
catchError(() => EMPTY)
101+
return import('rxjs').then(({ firstValueFrom, EMPTY, take, map, catchError }) => {
102+
return firstValueFrom(
103+
observable.pipe(
104+
take(1),
105+
map((value) => new Maybe<NonNullable<T>>(value as NonNullable<T>)),
106+
catchError(() => EMPTY)
107+
)
108+
).then(
109+
(maybeValue: IMaybe<NonNullable<T>>) => maybeValue,
110+
() => new Maybe<NonNullable<T>>()
109111
)
110-
).then(
111-
(maybeValue: IMaybe<NonNullable<T>>) => maybeValue,
112-
() => new Maybe<NonNullable<T>>()
113-
)
112+
})
114113
}
115114

116115
/**
@@ -271,7 +270,7 @@ export class Maybe<T> implements IMaybe<T> {
271270
if (this.isNone()) {
272271
return Promise.resolve(new Maybe<NonNullable<R>>())
273272
}
274-
273+
275274
return fn(this.value as NonNullable<T>)
276275
.then((value: R) => new Maybe<NonNullable<R>>(value as NonNullable<R>))
277276
.catch(() => new Maybe<NonNullable<R>>())
@@ -281,11 +280,7 @@ export class Maybe<T> implements IMaybe<T> {
281280
if (this.isNone()) {
282281
return Promise.resolve(new Maybe<NonNullable<R>>())
283282
}
284-
285-
const { firstValueFrom, EMPTY } = require('rxjs')
286-
const { take, map, catchError } = require('rxjs/operators')
287-
288-
return firstValueFrom(
283+
return import('rxjs').then(({ firstValueFrom, EMPTY, take, map, catchError }) => firstValueFrom(
289284
fn(this.value as NonNullable<T>).pipe(
290285
take(1),
291286
map((value: unknown) => new Maybe<NonNullable<R>>(value as NonNullable<R>)),
@@ -294,7 +289,7 @@ export class Maybe<T> implements IMaybe<T> {
294289
).then(
295290
(maybeValue: IMaybe<NonNullable<R>>) => maybeValue,
296291
() => new Maybe<NonNullable<R>>()
297-
)
292+
))
298293
}
299294

300295
public flatMapMany<R>(fn: (val: NonNullable<T>) => Promise<R>[]): Promise<IMaybe<NonNullable<R>[]>> {

0 commit comments

Comments
 (0)