@cspell/strong-weak-map
@cspell/strong-weak-map
npm i @cspell/strong-weak-map
@cspell/strong-weak-map

@cspell/strong-weak-map

A Spell Checker for Code!

by streetsidesoftware

6.16.0 (see all)License:MITTypeScript:Built-In
npm i @cspell/strong-weak-map
Readme

@cspell/strong-weak-map

A Map with weakly referenced values.

JavaScript WeakMap provides weak keys and strong values.

StrongWeakMap allows for strong keys and weak values while providing the same API as Map.

Install

npm install -S @cspell/strong-weak-map

Usage

TypeScript Example: Simple text file reader.

import { StrongWeakMap } from '@cspell/strong-weak-map';
import { promises as fs } from 'fs';

const cache = new StrongWeakMap<string, Promise<string>>();

export function readTextFile(filename: string, encoding: BufferEncoding = 'utf8'): Promise<string> {
  const cached = cache.get(filename);
  if (cached) return cached;

  const content = fs.readFile(filename, encoding);

  cache.set(filename, content);

  return content;
}

The above example will reuse the promise as long as it has not been cleaned up by the Garbage Collector.

API

See: Map

Downloads/wk

61.9K

GitHub Stars

662

LAST COMMIT

9mos ago

MAINTAINERS

1

CONTRIBUTORS

46

OPEN ISSUES

66

OPEN PRs

2
VersionTagPublished
6.16.0
latest
2d ago
No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

100
No reviews found
Be the first to rate