Skip to content

Commit

Permalink
feat: add deno entry (#4)
Browse files Browse the repository at this point in the history
* feat: add `deno` entry

* chore: rename `index.ts` -> `async.ts`

* feat: add `deno/sync` entry

* chore: add jsdocs

* chore: add "deno" section to readme
  • Loading branch information
lukeed authored Aug 11, 2020
1 parent 24efb4b commit b47f528
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
29 changes: 29 additions & 0 deletions deno/async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { dirname, resolve } from 'https://deno.land/std/path/mod.ts'

type Promisable<T> = T | Promise<T>;
export type Callback = (directory: string, files: string[]) => Promisable<string | false | void>

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;
}
}
28 changes: 28 additions & 0 deletions deno/sync.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 22 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ With [escalade](https://en.wikipedia.org/wiki/Escalade), you can scale parent di

> **Important:**<br>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

```
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit b47f528

Please sign in to comment.