Skip to content

Commit

Permalink
feat(types): Lazy, LazyPromise, LazyNullablePromise
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Aug 2, 2024
1 parent 183d4a8 commit fd50c9e
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 336 deletions.
2 changes: 1 addition & 1 deletion src/decorators/memo.decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('MEMO_DROP_CACHE', () => {
// second call
a.a(2, 3)

expect(a.func).toBeCalledTimes(2)
expect(a.func).toHaveBeenCalledTimes(2)
})

test('memo unsupported', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/decorators/memoSimple.decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test('MEMO_DROP_CACHE', () => {
// second call
a.a(2, 3)

expect(a.func).toBeCalledTimes(2)
expect(a.func).toHaveBeenCalledTimes(2)
})

test('memo unsupported', () => {
Expand Down
12 changes: 7 additions & 5 deletions src/define.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { expectTypeOf } from 'expect-type'
import {
_defineLazyProperty,
_defineLazyProps,
_defineProperty,
_defineProps,
_lazyValue,
} from './define'
import { AnyObject } from './types'
import { AnyObject, Lazy } from './types'

test('_lazyValue', () => {
const fn = jest.fn(() => 42)

const value = _lazyValue(fn)
expectTypeOf(value).toEqualTypeOf<Lazy<number>>()
expect(value()).toBe(42)
expect(value()).toBe(42)
expect(value()).toBe(42)

expect(fn).toBeCalledTimes(1)
expect(fn).toHaveBeenCalledTimes(1)
})

interface Obj {
Expand All @@ -33,7 +35,7 @@ test('_defineLazyProperty', () => {
expect(obj.v).toBe(42)
expect(obj.v).toBe(42)

expect(fn).toBeCalledTimes(1)
expect(fn).toHaveBeenCalledTimes(1)

// Another way to declare an object:

Expand All @@ -60,12 +62,12 @@ test('_defineLazyProps', () => {
expect(obj.v1).toBe(42)
expect(obj.v1).toBe(42)
expect(obj.v1).toBe(42)
expect(fn1).toBeCalledTimes(1)
expect(fn1).toHaveBeenCalledTimes(1)

expect(obj.v2).toBe(48)
expect(obj.v2).toBe(48)
expect(obj.v2).toBe(48)
expect(fn2).toBeCalledTimes(1)
expect(fn2).toHaveBeenCalledTimes(1)
})

test('_defineProperty', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/define.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _mapObject, _mapValues } from './object/object.util'
import type { AnyFunction, AnyObject } from './types'
import type { AnyFunction, AnyObject, Lazy } from './types'
import { SKIP } from './types'

/**
Expand All @@ -11,7 +11,7 @@ import { SKIP } from './types'
*
* Based on: https://github.com/sindresorhus/lazy-value
*/
export function _lazyValue<T extends AnyFunction>(fn: T): T {
export function _lazyValue<T>(fn: () => T): Lazy<T> {
let isCalled = false
let result: any

Expand Down
2 changes: 1 addition & 1 deletion src/http/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ test('should not mutate console', () => {
// So, it'll be called 0 times
// Here we're expecting that it's called 1 time
console.log('yo')
expect(consoleSpy).toBeCalledTimes(1)
expect(consoleSpy).toHaveBeenCalledTimes(1)
})

test('mocking fetch', async () => {
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ export type AnyAsyncFunction<T = any> = (...args: any[]) => Promise<T>
export type AsyncFunction<T = any> = () => Promise<T>
export type AnyPromisableFunction<T = any> = (...args: any[]) => Promisable<T>
export type PromisableFunction<T = any> = () => Promisable<T>
/**
* A function that lazily calculates something.
*/
export type Lazy<T> = () => T
/**
* A function that lazily calculates something async (returns a Promise).
*/
export type LazyPromise<T> = () => Promise<T>
/**
* A function that lazily calculates something async, that can return null.
*/
export type LazyNullablePromise<T> = () => Promise<T | null>

/**
* Symbol to indicate END of Sequence.
Expand Down
Loading

0 comments on commit fd50c9e

Please sign in to comment.