Skip to content

Commit 354faf5

Browse files
committed
Add tool to resolve an id from a URI
1 parent cf26ca8 commit 354faf5

11 files changed

Lines changed: 673 additions & 0 deletions

File tree

tools/links/uri2id/README.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
# uri2id
2+
3+
> Return the id corresponding to a provided URI.
4+
5+
6+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
7+
8+
<section class="intro">
9+
10+
</section>
11+
12+
<!-- /.intro -->
13+
14+
<!-- Package usage documentation. -->
15+
16+
<section class="usage">
17+
18+
## Usage
19+
20+
``` javascript
21+
var uri2id = require( '@stdlib/tools/links/uri2id' );
22+
```
23+
24+
<a name="async"></a>
25+
26+
#### uri2id( uri, \[options,\] clbk )
27+
28+
Returns the `id` corresponding to a provided `URI`.
29+
30+
``` javascript
31+
uri2id( 'http://www.bibtex.org/', clbk );
32+
33+
function clbk( error, id ) {
34+
if ( error ) {
35+
throw error;
36+
}
37+
console.log( id );
38+
// => bibtex
39+
}
40+
```
41+
42+
The function accepts the following `options`:
43+
44+
* __database__: path to a link database.
45+
46+
To use an alternative link database, set the `database` option.
47+
48+
``` javascript
49+
var opts = {
50+
'database': './path/to/link/database/json'
51+
};
52+
53+
uri2id( 'http://www.bibtex.org/', opts, clbk );
54+
55+
function clbk( error, id ) {
56+
if ( error ) {
57+
throw error;
58+
}
59+
console.log( id );
60+
}
61+
```
62+
63+
64+
#### uri2id.sync( uri\[, options\] )
65+
66+
Synchronously returns the `id` which corresponds to a provided `URI`.
67+
68+
``` javascript
69+
var id = uri2id.sync( 'http://www.bibtex.org/' );
70+
if ( id instanceof Error ) {
71+
throw id;
72+
}
73+
console.log( id );
74+
// => bibtex
75+
```
76+
77+
The method accepts the same `options` as [`uri2id()`](#async) above.
78+
79+
</section>
80+
81+
<!-- /.usage -->
82+
83+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
84+
85+
<section class="notes">
86+
87+
## Notes
88+
89+
* If a function is unable to resolve an `id`, the function returns `null`.
90+
* A link database is resolved relative to the current working directory of the calling process and should be a [JSON][json] file.
91+
92+
</section>
93+
94+
<!-- /.notes -->
95+
96+
<!-- Package usage examples. -->
97+
98+
<section class="examples">
99+
100+
## Examples
101+
102+
``` javascript
103+
var uri2id = require( '@stdlib/tools/links/uri2id' );
104+
105+
uri2id( 'https://github.com/stdlib-js/stdlib', clbk );
106+
107+
function clbk( error, id ) {
108+
if ( error ) {
109+
throw error;
110+
}
111+
console.log( id );
112+
}
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- Section for describing a command-line interface. -->
120+
121+
---
122+
123+
<section class="cli">
124+
125+
## CLI
126+
127+
<!-- CLI usage documentation. -->
128+
129+
<section class="usage">
130+
131+
### Usage
132+
133+
``` bash
134+
Usage: uri2id [options] [<uri>]
135+
136+
Options:
137+
138+
-h, --help Print this message.
139+
-V, --version Print the package version.
140+
--database filepath Database filepath.
141+
```
142+
143+
</section>
144+
145+
<!-- /.usage -->
146+
147+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="notes">
150+
151+
### Notes
152+
153+
* If invoked without a URI, the CLI will interactively prompt for argument input.
154+
155+
</section>
156+
157+
<!-- /.notes -->
158+
159+
<!-- CLI usage examples. -->
160+
161+
<section class="examples">
162+
163+
### Examples
164+
165+
``` bash
166+
$ uri2id http://www.bibtex.org/
167+
```
168+
169+
</section>
170+
171+
<!-- /.examples -->
172+
173+
</section>
174+
175+
<!-- /.cli -->
176+
177+
178+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
179+
180+
<section class="references">
181+
182+
</section>
183+
184+
<!-- /.references -->
185+
186+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
187+
188+
<section class="links">
189+
190+
[json]: http://www.json.org/
191+
192+
</section>
193+
194+
<!-- /.links -->

tools/links/uri2id/bin/cli

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
// MODULES //
5+
6+
var join = require( 'path' ).join;
7+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
8+
var CLI = require( '@stdlib/tools/cli' );
9+
var readline = require( 'readline' );
10+
var uri2id = require( './../lib' );
11+
12+
13+
// FUNCTIONS //
14+
15+
/**
16+
* Callback invoked upon completion.
17+
*
18+
* @private
19+
* @param {Error} [error] - error object
20+
* @param {(string|null)} id - id
21+
* @returns {void}
22+
*/
23+
function done( error, id ) {
24+
/* eslint-disable no-console, no-process-exit */
25+
if ( error ) {
26+
console.error( 'Error: %s', error.message );
27+
return process.exit( 1 );
28+
}
29+
if ( id ) {
30+
console.log( id );
31+
} else {
32+
console.log( 'Unable to resolve id.' );
33+
}
34+
process.exit( 0 );
35+
} // end FUNCTION done()
36+
37+
/**
38+
* Callback invoked upon receiving a SIGINT (e.g., ctrl+C).
39+
*
40+
* @private
41+
*/
42+
function onSIGINT() {
43+
console.error( '' );
44+
process.exit( 1 );
45+
} // end FUNCTION onSIGINT()
46+
47+
48+
// MAIN //
49+
50+
/**
51+
* Main execution sequence.
52+
*
53+
* @private
54+
* @returns {void}
55+
*/
56+
function main() {
57+
/* eslint-disable no-console, no-process-exit */
58+
var flags;
59+
var args;
60+
var opts;
61+
var cli;
62+
var uri;
63+
var rl;
64+
65+
// Create a command-line interface:
66+
cli = new CLI({
67+
'pkg': require( './../package.json' ),
68+
'options': require( './opts.json' ),
69+
'help': readFileSync( join( __dirname, 'usage.txt' ), {
70+
'encoding': 'utf8'
71+
})
72+
});
73+
// Get any provided command-line arguments:
74+
args = cli.args();
75+
76+
// Get any provided command-line flags:
77+
flags = cli.flags();
78+
79+
// If we are provided a URI, we can proceed to resolve an id...
80+
opts = {};
81+
if ( args.length ) {
82+
if ( flags.database ) {
83+
opts.database = flags.database;
84+
}
85+
return uri2id( args[ 0 ], opts, done );
86+
}
87+
// Provide a series of prompts for a user to provide arguments...
88+
rl = readline.createInterface({
89+
'input': process.stdin,
90+
'output': process.stdout
91+
});
92+
rl.on( 'close', onClose );
93+
rl.on( 'SIGINT', onSIGINT );
94+
95+
console.log( '\nPlease provide the following information: \n' );
96+
rl.question( 'URI: ', onURI );
97+
98+
/**
99+
* Callback invoked upon receiving a URI.
100+
*
101+
* @private
102+
* @param {string} answer - user input
103+
* @returns {void}
104+
*/
105+
function onURI( answer ) {
106+
uri = answer;
107+
if ( flags.database ) {
108+
return database( flags.database );
109+
}
110+
rl.question( 'database filepath (optional): ', database );
111+
} // end FUNCTION onURI()
112+
113+
/**
114+
* Callback invoked upon receiving a database filepath.
115+
*
116+
* @private
117+
* @param {string} answer - user input
118+
*/
119+
function database( answer ) {
120+
if ( answer ) {
121+
opts.database = answer;
122+
}
123+
rl.close();
124+
} // end FUNCTION database()
125+
126+
/**
127+
* Callback invoked once a readline interface closes.
128+
*
129+
* @private
130+
*/
131+
function onClose() {
132+
uri2id( uri, opts, done );
133+
} // end FUNCTION onClose()
134+
} // end FUNCTION main()
135+
136+
main();

tools/links/uri2id/bin/opts.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"string": [
3+
"database"
4+
],
5+
"boolean": [
6+
"help",
7+
"version"
8+
],
9+
"alias": {
10+
"help": [
11+
"h"
12+
],
13+
"version": [
14+
"V"
15+
]
16+
}
17+
}

tools/links/uri2id/bin/usage.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
Usage: uri2id [options] [<uri>]
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
--database filepath Database filepath.
9+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
var uri2id = require( './../lib' );
4+
5+
uri2id( 'https://github.com/stdlib-js/stdlib', clbk );
6+
7+
function clbk( error, id ) {
8+
if ( error ) {
9+
throw error;
10+
}
11+
console.log( id );
12+
}

0 commit comments

Comments
 (0)