-
-
Notifications
You must be signed in to change notification settings - Fork 834
feat: add fs/resolve-parent-paths-by
#2897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gururaj1512
wants to merge
15
commits into
stdlib-js:develop
Choose a base branch
from
gururaj1512:resolve-parent-paths-by
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,767
−0
Open
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b7504ef
resolve-parent-paths-by
gururaj1512 c064247
Merge branch 'stdlib-js:develop' into resolve-parent-paths-by
gururaj1512 304c2a0
update: README.md
gururaj1512 8a5004a
update: README.md
gururaj1512 fe95b5b
Update lib/node_modules/@stdlib/fs/resolve-parent-paths-by/lib/index.js
gururaj1512 6a90b02
docs: update examples
gururaj1512 79ca3c6
update: files
gururaj1512 ef6f625
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into pr…
kgryte 130993c
chore: avoiding hardcoding number of paths and fix argument validation
kgryte 762de3d
refactor: rename arguments
kgryte 96d24f4
refactor: rename parameters
kgryte 4eb2bf5
fix: race conditions
gururaj1512 3910ca8
fix: lint errors
gururaj1512 6ec36f4
Merge remote-tracking branch 'upstream/develop' into resolve-parent-p…
stdlib-bot 4e129b7
Merge remote-tracking branch 'upstream/develop' into resolve-parent-p…
stdlib-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
230 changes: 230 additions & 0 deletions
230
lib/node_modules/@stdlib/fs/resolve-parent-paths-by/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2024 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# resolveParentPathsBy | ||
|
||
> Resolve paths from a set of paths according to a predicate function by walking parent directories. | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var resolveParentPathsBy = require( '@stdlib/fs/resolve-parent-paths-by' ); | ||
``` | ||
|
||
<a name="resolve-parent-paths-by"></a> | ||
|
||
#### resolveParentPathsBy( paths\[, options], predicate, clbk ) | ||
|
||
Asynchronously resolves paths from a set of paths according to a `predicate` function by walking parent directories. | ||
|
||
```javascript | ||
resolveParentPathsBy( [ 'package.json', 'package-lock.json' ], predicate, onPath ); | ||
|
||
function predicate( path, next ) { | ||
var bool = ( /\/test\//.test( path ) === false ); | ||
next( null, bool ); | ||
} | ||
|
||
function onPath( error, paths ) { | ||
if ( error ) { | ||
throw error; | ||
} | ||
console.log( paths ); | ||
// e.g., => [...] | ||
} | ||
``` | ||
|
||
The function accepts the following `options`: | ||
|
||
- **dir**: base directory from which to begin walking. May be either an absolute path or a path relative to the current working directory. | ||
|
||
- **mode**: mode of operation. The following modes are supported: | ||
|
||
- `first`: return the first resolved path. | ||
- `some`: return one or more paths resolved within the first directory level containing a match. | ||
- `all`: return all resolved paths within the first directory level containing matches for all provided paths. | ||
- `each`: independently return the first resolved path for each path at any directory level. | ||
|
||
Default: `'all'`. | ||
|
||
By default, the function begins walking from the current working directory. To specify an alternative directory, set the `dir` option. | ||
|
||
```javascript | ||
var opts = { | ||
'dir': __dirname | ||
}; | ||
resolveParentPathsBy( [ 'package.json' ], opts, predicate, onPath ); | ||
|
||
function predicate( path, next ) { | ||
var bool = ( /\/test\//.test( path ) === false ); | ||
next( null, bool ); | ||
} | ||
|
||
function onPath( error, paths ) { | ||
if ( error ) { | ||
throw error; | ||
} | ||
console.log( paths ); | ||
// e.g., => [...] | ||
} | ||
``` | ||
|
||
When invoked, the `predicate` function is provided two arguments: | ||
|
||
- `path`: a resolved path. | ||
- `next`: a callback which should be called once the `predicate` function has finished processing a resolved path. | ||
|
||
If a `predicate` function calls the `next` callback with a `truthy` second argument, the function proceeds to next function and returns the resolved path. | ||
|
||
If unable to resolve paths, as per each mode function returns as following: | ||
|
||
- `first`: empty array. | ||
- `some`: empty array. | ||
- `all`: empty array. | ||
- `each`: Array<`null`>. | ||
|
||
By default, the function requires that a directory contains matches for all provided paths before returning results. To specify an alternative operation mode, set the `mode` option. | ||
|
||
```javascript | ||
var opts = { | ||
gururaj1512 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'dir': __dirname, | ||
'mode': 'first' | ||
}; | ||
resolveParentPathsBy( [ 'package.json', 'package-lock.json' ], opts, predicate, onPaths ); | ||
|
||
function predicate( path, next ) { | ||
var bool = ( /\/test\//.test( path ) === false ); | ||
next( null, bool ); | ||
} | ||
|
||
function onPaths( error, paths ) { | ||
if ( error ) { | ||
throw error; | ||
} | ||
console.log( paths ); | ||
// => [...] | ||
} | ||
``` | ||
|
||
#### resolveParentPathsBy.sync( path\[, options], predicate ) | ||
|
||
Synchronously resolves paths from a set of paths according to a `predicate` function by walking parent directories. | ||
|
||
```javascript | ||
function predicate( path ) { | ||
return ( /\/test\//.test( path ) === false ); | ||
} | ||
|
||
var path = resolveParentPathsBy.sync( [ 'package.json', 'README.md' ], predicate ); | ||
// e.g., returns [...] | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
## Notes | ||
|
||
- In `some` mode, the return order of asynchronously resolved paths is not guaranteed. | ||
- This implementation is **not** similar in functionality to core [`path.resolve`][node-core-path-resolve]. The latter performs string manipulation to generate a full path. This implementation walks parent directories to perform a **search**, thereby touching the file system. Accordingly, this implementation resolves a _real_ absolute file path and is intended for use when a target's location in a parent directory is unknown relative to a child directory; e.g., when wanting to find a package root from deep within a package directory. | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
<!-- eslint-disable stdlib/no-dynamic-require --> | ||
|
||
```javascript | ||
var resolveParentPathsBy = require( '@stdlib/fs/resolve-parent-paths-by' ); | ||
|
||
var opts = { | ||
'dir': __dirname | ||
}; | ||
|
||
/* Sync */ | ||
|
||
function predicateSync( path ) { | ||
return true; | ||
} | ||
|
||
var out = resolveParentPathsBy.sync( [ 'package.json', 'README.md' ], opts, predicateSync ); | ||
// returns [...] | ||
|
||
out = resolveParentPathsBy.sync( [ 'non_existent_basename/package.json' ], opts, predicateSync ); | ||
// => [] | ||
|
||
opts.mode = 'first'; | ||
out = resolveParentPathsBy.sync( [ 'non_existent_basename/package.json', 'package.json' ], opts, predicateSync ); | ||
// => returns [...] | ||
|
||
/* Async */ | ||
|
||
function predicateAsync( path, next ) { | ||
setTimeout( onTimeout, 0 ); | ||
|
||
function onTimeout() { | ||
next( null, true ); | ||
} | ||
} | ||
|
||
function onPath( error, paths ) { | ||
if ( error ) { | ||
throw error; | ||
} | ||
console.log( paths ); | ||
} | ||
|
||
resolveParentPathsBy( [ 'package.json', 'README.md' ], opts, predicateAsync, onPath ); | ||
resolveParentPathsBy( [ './../non_existent_path/package.json' ], predicateAsync, onPath ); | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[node-core-path-resolve]: https://nodejs.org/api/path.html#path_path_resolve_paths | ||
|
||
<!-- </related-links> --> | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.