Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hayd/deno-using
python-style with statements for deno
https://github.com/hayd/deno-using
Last synced: about 1 month ago
JSON representation
python-style with statements for deno
- Host: GitHub
- URL: https://github.com/hayd/deno-using
- Owner: hayd
- Created: 2019-02-18T02:55:02.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-10T07:10:57.000Z (over 4 years ago)
- Last Synced: 2024-10-19T03:23:24.143Z (about 2 months ago)
- Language: TypeScript
- Homepage:
- Size: 15.6 KB
- Stars: 11
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-deno - deno-using - Python样式语法。 (Uncategorized / Uncategorized)
- awesome-deno-cn - @hayd/deno-using
- awesome-deno - deno-using - An python-style with statements for deno.![GitHub stars](https://img.shields.io/github/stars/hayd/deno-using?style=plastic) (Modules / Online Playgrounds)
- awesome-deno - deno-using - An python-style with statements for deno. (Modules / Utils)
README
# deno-using
A python-style with statement for deno.
Comes with some `Using` types, e.g. `Open`, `ChDir`, `TempDir`, and `Timeout`.
## Example:
```ts
import { using, Open } from "htps://deno.land/x/using/mod.ts";const enc = new TextEncoder();
const dec = new TextDecoder();await using(new Open("file", "w"), async (f: Deno.File) => {
const data = enc.encode("Hello world!\n");
await f.write(data);
});
await using(new Open("file", "r"), async (f: Deno.File) => {
const data = new Uint8Array(20);
await f.read(data);
const text = dec.decode(data);
console.log(text);
});
```There is also a corresponding sync version `UsingSync`.
## Custom types
You can define your own `Using` types by creating a class which implements `Using`:
```ts
export class Open implements Using {
constructor(filename: string, mode?: Deno.OpenMode) {
this.filename = filename;
this.mode = mode;
}
public async _aenter() {
this.file = await Deno.open(this.filename, this.mode);
return this.file;
}
public async _aexit(e) {
this.file.close();
}
private file: Deno.File;
private filename: string;
private mode: Deno.OpenMode;
}
```