|
sfcode
An Online Competing and Development Environment
|
A tiny (183B to 210B) and fast utility to ascend parent directories
With escalade, you can scale parent directories until you've found what you're looking for.
Given an input file or directory, escalade will continue executing your callback function until either:
1) the callback returns a truthy value 2) escalade has reached the system root directory (eg, /)
Important:
Please note thatescaladeonly deals with direct ancestry – it will not dive into parents' sibling directories.
Notice: As of v3.1.0, escalade now includes Deno support! Please see Deno Usage below.
There are two "versions" of escalade available:
Node.js: >= 8.x
Size (gzip): 210 bytes
Availability: CommonJS, ES Module
This is the primary/default mode. It makes use of async/await and util.promisify.
Node.js: >= 6.x
Size (gzip): 183 bytes
Availability: CommonJS, ES Module
This is the opt-in mode, ideal for scenarios where async usage cannot be supported.
Example Structure
Example Usage
Note: To run the above example with "sync" mode, import from
escalade/syncand remove theawaitkeyword.
Returns: string|void or Promise<string|void>
When your callback locates a file, escalade will resolve/return with an absolute path.
If your callback was never satisfied, then escalade will resolve/return with nothing (undefined).
Important:
Thesyncandasyncversions share the same API.
The only difference is thatsyncis not Promise-based.
Type: string
The path from which to start ascending.
This may be a file or a directory path.
However, when input is a file, escalade will begin with its parent directory.
Important: Unless given an absolute path,
inputwill be resolved fromprocess.cwd()location.
Type: Function
The callback to execute for each ancestry level. It always is given two arguments:
1) dir - an absolute path of the current parent directory 2) names - a list (string[]) of contents relative to the dir parent
Note: The
nameslist can contain names of files and directories.
When your callback returns a falsey value, then escalade will continue with dir's parent directory, re-invoking your callback with new argument values.
When your callback returns a string, then escalade stops iteration immediately.
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.
Important: Your
callbackcan be aPromise/AsyncFunctionwhen using the "async" version ofescalade.
Running on Node.js v10.13.0
As of v3.1.0, escalade is available on the Deno registry.
Please note that the API is identical and that there are still two modes from which to choose:
Important: The
allow-readpermission is required!
MIT © Luke Edwards