Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

python-style with statements for deno

NotificationsYou must be signed in to change notification settings

hayd/deno-using

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A python-style with statement for deno.

Comes with someUsing types, e.g.Open,ChDir,TempDir, andTimeout.

Example:

import{using,Open}from"htps://deno.land/x/using/mod.ts";constenc=newTextEncoder();constdec=newTextDecoder();await using(newOpen("file","w"),async(f:Deno.File)=>{constdata=enc.encode("Hello world!\n");awaitf.write(data);});await using(newOpen("file","r"),async(f:Deno.File)=>{constdata=newUint8Array(20);awaitf.read(data);consttext=dec.decode(data);console.log(text);});

There is also a corresponding sync versionUsingSync.

Custom types

You can define your ownUsing types by creating a class which implementsUsing:

exportclassOpenimplementsUsing<Deno.File>{constructor(filename:string,mode?:Deno.OpenMode){this.filename=filename;this.mode=mode;}publicasync_aenter(){this.file=awaitDeno.open(this.filename,this.mode);returnthis.file;}publicasync_aexit(e){this.file.close();}privatefile:Deno.File;privatefilename:string;privatemode:Deno.OpenMode;}

About

python-style with statements for deno

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp