Skip to content

Commit

Permalink
add jsdoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Jul 25, 2023
1 parent a75bd81 commit b32bca7
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import {type Opt} from "./deep.ts";
import {base64Encode} from "./base64.ts";
import {utfEncode} from "./text.ts";

/**
* Auxiliary type for default export.
*/
export interface DefExp<T extends unknown>{
default: T;
}

/**
* Evaluate ESM JavaScript code in module scope.
* @example
* ```ts
* const {default: data} = await evaluateESM<DefExp<string>>("export default 'hello!';");
* ```
*/
export async function evaluateESM<T extends Opt<T>>(code:string):Promise<T>{
return await import(`data:text/javascript;base64,${base64Encode(utfEncode(code))}`);
}

0 comments on commit b32bca7

Please sign in to comment.