diff options
author | Casper Beyer <caspervonb@pm.me> | 2021-02-02 19:05:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 12:05:46 +0100 |
commit | 6abf126c2a7a451cded8c6b5e6ddf1b69c84055d (patch) | |
tree | fd94c013a19fcb38954844085821ec1601c20e18 /std/examples/xeval.ts | |
parent | a2b5d44f1aa9d64f448a2a3cc2001272e2f60b98 (diff) |
chore: remove std directory (#9361)
This removes the std folder from the tree.
Various parts of the tests are pretty tightly dependent
on std (47 direct imports and 75 indirect imports, not
counting the cli tests that use them as fixtures) so I've
added std as a submodule for now.
Diffstat (limited to 'std/examples/xeval.ts')
-rw-r--r-- | std/examples/xeval.ts | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/std/examples/xeval.ts b/std/examples/xeval.ts deleted file mode 100644 index cff15edd0..000000000 --- a/std/examples/xeval.ts +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { parse } from "../flags/mod.ts"; -import { readStringDelim } from "../io/bufio.ts"; - -// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction. -const AsyncFunction = Object.getPrototypeOf(async function (): Promise<void> {}) - .constructor; - -const HELP_MSG = `xeval - -Run a script for each new-line or otherwise delimited chunk of standard input. - -Print all the usernames in /etc/passwd: - cat /etc/passwd | deno run -A https://deno.land/std/examples/xeval.ts "a = $.split(':'); if (a) console.log(a[0])" - -A complicated way to print the current git branch: - git branch | deno run -A https://deno.land/std/examples/xeval.ts -I 'line' "if (line.startsWith('*')) console.log(line.slice(2))" - -Demonstrates breaking the input up by space delimiter instead of by lines: - cat LICENSE | deno run -A https://deno.land/std/examples/xeval.ts -d " " "if ($ === 'MIT') console.log('MIT licensed')", - -USAGE: - deno run -A https://deno.land/std/examples/xeval.ts [OPTIONS] <code> -OPTIONS: - -d, --delim <delim> Set delimiter, defaults to newline - -I, --replvar <replvar> Set variable name to be used in eval, defaults to $ -ARGS: - <code>`; - -export type XevalFunc = (v: string) => void; - -export interface XevalOptions { - delimiter?: string; -} - -const DEFAULT_DELIMITER = "\n"; - -export async function xeval( - reader: Deno.Reader, - xevalFunc: XevalFunc, - { delimiter = DEFAULT_DELIMITER }: XevalOptions = {}, -): Promise<void> { - for await (const chunk of readStringDelim(reader, delimiter)) { - // Ignore empty chunks. - if (chunk.length > 0) { - await xevalFunc(chunk); - } - } -} - -async function main(): Promise<void> { - const parsedArgs = parse(Deno.args, { - boolean: ["help"], - string: ["delim", "replvar"], - alias: { - delim: ["d"], - replvar: ["I"], - help: ["h"], - }, - default: { - delim: DEFAULT_DELIMITER, - replvar: "$", - }, - }); - if (parsedArgs._.length != 1) { - console.error(HELP_MSG); - console.log(parsedArgs._); - Deno.exit(1); - } - if (parsedArgs.help) { - return console.log(HELP_MSG); - } - - const delimiter = parsedArgs.delim; - const replVar = parsedArgs.replvar; - const code = parsedArgs._[0]; - - // new AsyncFunction()'s error message for this particular case isn't great. - if (!replVar.match(/^[_$A-z][_$A-z0-9]*$/)) { - console.error(`Bad replvar identifier: "${replVar}"`); - Deno.exit(1); - } - - const xEvalFunc = new AsyncFunction(replVar, code); - - await xeval(Deno.stdin, xEvalFunc, { delimiter }); -} - -if (import.meta.main) { - main(); -} |