From b47f5287648f0839ec7cfa4e28f584a967d3b37e Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 10 Aug 2020 22:00:51 -0700 Subject: [PATCH] feat: add `deno` entry (#4) * feat: add `deno` entry * chore: rename `index.ts` -> `async.ts` * feat: add `deno/sync` entry * chore: add jsdocs * chore: add "deno" section to readme --- deno/async.ts | 29 +++++++++++++++++++++++++++++ deno/sync.ts | 28 ++++++++++++++++++++++++++++ readme.md | 22 ++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 deno/async.ts create mode 100644 deno/sync.ts diff --git a/deno/async.ts b/deno/async.ts new file mode 100644 index 0000000..78046cb --- /dev/null +++ b/deno/async.ts @@ -0,0 +1,29 @@ +import { dirname, resolve } from 'https://deno.land/std/path/mod.ts' + +type Promisable = T | Promise; +export type Callback = (directory: string, files: string[]) => Promisable + +async function toItems(dir: string) { + let list = []; + for await (let tmp of Deno.readDir(dir)) { + list.push(tmp.name); + } + return list; +} + +/** Requires `allow-read` permission. */ +export default async function (start: string, callback: Callback) { + let dir = resolve('.', start); + let stats = await Deno.stat(dir); + + if (!stats.isDirectory) { + dir = dirname(dir); + } + + while (true) { + let tmp = await callback(dir, await toItems(dir)); + if (tmp) return resolve(dir, tmp); + dir = dirname(tmp = dir); + if (tmp === dir) break; + } +} diff --git a/deno/sync.ts b/deno/sync.ts new file mode 100644 index 0000000..4e1190b --- /dev/null +++ b/deno/sync.ts @@ -0,0 +1,28 @@ +import { dirname, resolve } from 'https://deno.land/std/path/mod.ts' + +export type Callback = (directory: string, files: string[]) => string | false | void; + +function toItems(dir: string) { + let list = []; + for (let tmp of Deno.readDirSync(dir)) { + list.push(tmp.name); + } + return list; +} + +/** Requires `allow-read` permission. */ +export default function (start: string, callback: Callback) { + let dir = resolve('.', start); + let stats = Deno.statSync(dir); + + if (!stats.isDirectory) { + dir = dirname(dir); + } + + while (true) { + let tmp = callback(dir, toItems(dir)); + if (tmp) return resolve(dir, tmp); + dir = dirname(tmp = dir); + if (tmp === dir) break; + } +} diff --git a/readme.md b/readme.md index e47a34a..4e2195c 100644 --- a/readme.md +++ b/readme.md @@ -9,6 +9,12 @@ With [escalade](https://en.wikipedia.org/wiki/Escalade), you can scale parent di > **Important:**
Please note that `escalade` only deals with direct ancestry – it will not dive into parents' sibling directories. +--- + +**Notice:** As of v3.1.0, `escalade` now includes [Deno support](http://deno.land/x/escalade)! Please see [Deno Usage](#deno) below. + +--- + ## Install ``` @@ -177,6 +183,22 @@ If the string is an absolute path, then it's left as is. Otherwise, the string i escalade/sync x 1,248 ops/sec ± 0.50% (93 runs sampled) ``` +## Deno + +As of v3.1.0, `escalade` is available on the Deno registry. + +Please note that the [API](#api) is identical and that there are still [two modes](#modes) from which to choose: + +```ts +// Choose "async" mode +import escalade from 'https://deno.land/escalade/async.ts'; + +// Choose "sync" mode +import escalade from 'https://deno.land/escalade/sync.ts'; +``` + +> **Important:** The `allow-read` permission is required! + ## Related