# extra-lazy

> Yet another lazy evaluation library.

Latest version **2.0.2** (published 2023-06-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install extra-lazy
pnpm add extra-lazy
yarn add extra-lazy
bun add extra-lazy
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2023-06-10 |
| First published | 2021-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | BlackGlory |
| Maintainers | black_glory |

## Links

- npm: https://www.npmjs.com/package/extra-lazy
- Repository: https://github.com/BlackGlory/extra-lazy
- Homepage: https://github.com/BlackGlory/extra-lazy#readme
- Issues: https://github.com/BlackGlory/extra-lazy/issues
- npm.io page: https://npm.io/package/extra-lazy

## Dependencies (1)

- [extra-utils](https://npm.io/package/extra-utils.md) ^5.1.0

## Recent versions

- 2.0.2 (latest) — 2023-06-10
- 2.0.1 — 2023-03-19
- 2.0.0 — 2023-01-25
- 1.3.1 — 2022-08-11
- 1.3.0 — 2022-08-10
- 1.2.0 — 2022-08-10
- 1.1.2 — 2022-08-01
- 1.1.1 — 2022-04-08
- 1.1.0 — 2022-04-07
- 1.0.1 — 2022-03-23
- 1.0.0 — 2022-03-05
- 0.1.0 — 2021-09-25

## README

# extra-lazy
Yet another lazy evaluation library.

## Install
```sh
npm install --save extra-lazy
# or
yarn add extra-lazy
```

## Usage
```ts
import { lazy } from 'extra-lazy'

const getValue = lazy(() => {
  // ...
  return value
})
const value = getValue()
```

## API
### lazy
```ts
function lazy<T>(getter: () => T): () => T
```

Create a value lazily.

which implicitly has memoization,
because the evaluation will only be performed once.

### weakLazy
```ts
function weakLazy<T extends object>(getter: () => T): () => T
```

### lazyFunction
```ts
function lazyFunction<Result, Args extends any[]>(
  getter: () => (...args: Args) => Result
): (...args: Args) => Result
```

Create a function lazily.

### lazyAsyncFunction
```ts
function lazyAsyncFunction<Result, Args extends any[]>(
  getter: () => PromiseLike<(...args: Args) => Result>
): (...args: Args) => Promise<Result>
```

Create a async function lazily.

### lazyStatic
```ts
function lazyStatic<T>(
  getter: () => T
, deps?: unknown[] = []
): T

/**
 * @param fn
 * The function must satisfy the following conditions, it's like React hooks very much:
 * - The function should not be an async function,
 *   it is impossible to ensure that `lazyStatic` works correctly in asynchronous flows.
 * - `lazyStatic` calls should not be in loops or branches.
 */
function withLazyStatic<Result, Args extends any[]>(
  fn: (...args: Args) => Result
): (...args: Args) => Result
```

Example:
```ts
const fn = withLazyStatic((text: string) => lazyStatic(() => text))

fn('hello') // 'hello'
fn('world') // 'hello'
```

#### Best practices
##### Loop
```ts
// bad
withLazyStatic(() => {
  while (condition) {
    const value = lazyStatic(() => {
      // ...
    })
    // ...
  }
})

// good
withLazyStatic(() => {
  const value = lazyStatic(() => {
    // ...
  })

  while (condition) {
    // ...
  }
})
```

##### Branch
```ts
// bad
withLazyStatic(() => {
  if (condition) {
    const value = lazyStatic(() => {
      // ...
    })
    // ...
  } else {
    // ...
  }
})

// good
withLazyStatic(() => {
  const value = lazyStatic(() => {
    // ...
  })

  if (condition) {
    // ...
  } else {
    // ...
  }
})
```

###### Assertion/Validation
``` ts
// bad
withLazyStatic((form: IForm) => {
  if (validate(form)) {
    const value = lazyStatic(() => {
      // ...
    })
    // ...
  } else {
    const value = lazyStatic(() => {
      // ...
    })
    return null // or throw an error
  }
})

// okay
withLazyStatic((form: IForm) => {
  if (validate(form)) {
    const value = lazyStatic(() => {
      // ...
    })
    // ...
  } else {
    return null // or throw an error
  }
})

// good, `lazyStatic` can always be called after guards
withLazyStatic((form: IForm) => {
  if (!validate(form)) return null // or throw an error

  const value = lazyStatic(() => {
    // ...
  })
  // ...
})
```

---
_Source: https://npm.io/package/extra-lazy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
