GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
6.7 KiB

2 months ago
  1. # escalade [![CI](https://github.com/lukeed/escalade/workflows/CI/badge.svg)](https://github.com/lukeed/escalade/actions) [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/escalade)](https://codecov.io/gh/lukeed/escalade)
  2. > A tiny (183B to 210B) and [fast](#benchmarks) utility to ascend parent directories
  3. With [escalade](https://en.wikipedia.org/wiki/Escalade), you can scale parent directories until you've found what you're looking for.<br>Given an input file or directory, `escalade` will continue executing your callback function until either:
  4. 1) the callback returns a truthy value
  5. 2) `escalade` has reached the system root directory (eg, `/`)
  6. > **Important:**<br>Please note that `escalade` only deals with direct ancestry – it will not dive into parents' sibling directories.
  7. ---
  8. **Notice:** As of v3.1.0, `escalade` now includes [Deno support](http://deno.land/x/escalade)! Please see [Deno Usage](#deno) below.
  9. ---
  10. ## Install
  11. ```
  12. $ npm install --save escalade
  13. ```
  14. ## Modes
  15. There are two "versions" of `escalade` available:
  16. #### "async"
  17. > **Node.js:** >= 8.x<br>
  18. > **Size (gzip):** 210 bytes<br>
  19. > **Availability:** [CommonJS](https://unpkg.com/escalade/dist/index.js), [ES Module](https://unpkg.com/escalade/dist/index.mjs)
  20. This is the primary/default mode. It makes use of `async`/`await` and [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original).
  21. #### "sync"
  22. > **Node.js:** >= 6.x<br>
  23. > **Size (gzip):** 183 bytes<br>
  24. > **Availability:** [CommonJS](https://unpkg.com/escalade/sync/index.js), [ES Module](https://unpkg.com/escalade/sync/index.mjs)
  25. This is the opt-in mode, ideal for scenarios where `async` usage cannot be supported.
  26. ## Usage
  27. ***Example Structure***
  28. ```
  29. /Users/lukeed
  30. └── oss
  31. ├── license
  32. └── escalade
  33. ├── package.json
  34. └── test
  35. └── fixtures
  36. ├── index.js
  37. └── foobar
  38. └── demo.js
  39. ```
  40. ***Example Usage***
  41. ```js
  42. //~> demo.js
  43. import { join } from 'path';
  44. import escalade from 'escalade';
  45. const input = join(__dirname, 'demo.js');
  46. // or: const input = __dirname;
  47. const pkg = await escalade(input, (dir, names) => {
  48. console.log('~> dir:', dir);
  49. console.log('~> names:', names);
  50. console.log('---');
  51. if (names.includes('package.json')) {
  52. // will be resolved into absolute
  53. return 'package.json';
  54. }
  55. });
  56. //~> dir: /Users/lukeed/oss/escalade/test/fixtures/foobar
  57. //~> names: ['demo.js']
  58. //---
  59. //~> dir: /Users/lukeed/oss/escalade/test/fixtures
  60. //~> names: ['index.js', 'foobar']
  61. //---
  62. //~> dir: /Users/lukeed/oss/escalade/test
  63. //~> names: ['fixtures']
  64. //---
  65. //~> dir: /Users/lukeed/oss/escalade
  66. //~> names: ['package.json', 'test']
  67. //---
  68. console.log(pkg);
  69. //=> /Users/lukeed/oss/escalade/package.json
  70. // Now search for "missing123.txt"
  71. // (Assume it doesn't exist anywhere!)
  72. const missing = await escalade(input, (dir, names) => {
  73. console.log('~> dir:', dir);
  74. return names.includes('missing123.txt') && 'missing123.txt';
  75. });
  76. //~> dir: /Users/lukeed/oss/escalade/test/fixtures/foobar
  77. //~> dir: /Users/lukeed/oss/escalade/test/fixtures
  78. //~> dir: /Users/lukeed/oss/escalade/test
  79. //~> dir: /Users/lukeed/oss/escalade
  80. //~> dir: /Users/lukeed/oss
  81. //~> dir: /Users/lukeed
  82. //~> dir: /Users
  83. //~> dir: /
  84. console.log(missing);
  85. //=> undefined
  86. ```
  87. > **Note:** To run the above example with "sync" mode, import from `escalade/sync` and remove the `await` keyword.
  88. ## API
  89. ### escalade(input, callback)
  90. Returns: `string|void` or `Promise<string|void>`
  91. When your `callback` locates a file, `escalade` will resolve/return with an absolute path.<br>
  92. If your `callback` was never satisfied, then `escalade` will resolve/return with nothing (undefined).
  93. > **Important:**<br>The `sync` and `async` versions share the same API.<br>The **only** difference is that `sync` is not Promise-based.
  94. #### input
  95. Type: `string`
  96. The path from which to start ascending.
  97. This may be a file or a directory path.<br>However, when `input` is a file, `escalade` will begin with its parent directory.
  98. > **Important:** Unless given an absolute path, `input` will be resolved from `process.cwd()` location.
  99. #### callback
  100. Type: `Function`
  101. The callback to execute for each ancestry level. It always is given two arguments:
  102. 1) `dir` - an absolute path of the current parent directory
  103. 2) `names` - a list (`string[]`) of contents _relative to_ the `dir` parent
  104. > **Note:** The `names` list can contain names of files _and_ directories.
  105. When your callback returns a _falsey_ value, then `escalade` will continue with `dir`'s parent directory, re-invoking your callback with new argument values.
  106. When your callback returns a string, then `escalade` stops iteration immediately.<br>
  107. If the string is an absolute path, then it's left as is. Otherwise, the string is resolved into an absolute path _from_ the `dir` that housed the satisfying condition.
  108. > **Important:** Your `callback` can be a `Promise/AsyncFunction` when using the "async" version of `escalade`.
  109. ## Benchmarks
  110. > Running on Node.js v10.13.0
  111. ```
  112. # Load Time
  113. find-up 3.891ms
  114. escalade 0.485ms
  115. escalade/sync 0.309ms
  116. # Levels: 6 (target = "foo.txt"):
  117. find-up x 24,856 ops/sec ±6.46% (55 runs sampled)
  118. escalade x 73,084 ops/sec ±4.23% (73 runs sampled)
  119. find-up.sync x 3,663 ops/sec ±1.12% (83 runs sampled)
  120. escalade/sync x 9,360 ops/sec ±0.62% (88 runs sampled)
  121. # Levels: 12 (target = "package.json"):
  122. find-up x 29,300 ops/sec ±10.68% (70 runs sampled)
  123. escalade x 73,685 ops/sec ± 5.66% (66 runs sampled)
  124. find-up.sync x 1,707 ops/sec ± 0.58% (91 runs sampled)
  125. escalade/sync x 4,667 ops/sec ± 0.68% (94 runs sampled)
  126. # Levels: 18 (target = "missing123.txt"):
  127. find-up x 21,818 ops/sec ±17.37% (14 runs sampled)
  128. escalade x 67,101 ops/sec ±21.60% (20 runs sampled)
  129. find-up.sync x 1,037 ops/sec ± 2.86% (88 runs sampled)
  130. escalade/sync x 1,248 ops/sec ± 0.50% (93 runs sampled)
  131. ```
  132. ## Deno
  133. As of v3.1.0, `escalade` is available on the Deno registry.
  134. Please note that the [API](#api) is identical and that there are still [two modes](#modes) from which to choose:
  135. ```ts
  136. // Choose "async" mode
  137. import escalade from 'https://deno.land/escalade/async.ts';
  138. // Choose "sync" mode
  139. import escalade from 'https://deno.land/escalade/sync.ts';
  140. ```
  141. > **Important:** The `allow-read` permission is required!
  142. ## Related
  143. - [premove](https://github.com/lukeed/premove) - A tiny (247B) utility to remove items recursively
  144. - [totalist](https://github.com/lukeed/totalist) - A tiny (195B to 224B) utility to recursively list all (total) files in a directory
  145. - [mk-dirs](https://github.com/lukeed/mk-dirs) - A tiny (420B) utility to make a directory and its parents, recursively
  146. ## License
  147. MIT © [Luke Edwards](https://lukeed.com)