Skip to content

Commit cfdeaca

Browse files
committed
Add support for resolving an alias from a pkg name in browser REPL
1 parent fe54606 commit cfdeaca

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

lib/node_modules/@stdlib/repl/scripts/browser.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
var getKeys = require( 'object-keys' ).shim();
66
var vdom2html = require( 'vdom-to-html' );
77
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
8+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
89
var deepGet = require( '@stdlib/utils/deep-get' );
910
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
1011
var namespace = require( '@stdlib/namespace' );
@@ -15,6 +16,7 @@ var docs = require( '@stdlib/repl/help' );
1516

1617
var NO_HELP_TEXT = 'No help information available.';
1718
var NO_PKG_TEXT = 'Unrecognized alias.';
19+
var NO_ALIAS_TEXT = 'Unrecognized package name.';
1820
var NAMESPACE = namespace();
1921
var DOCS = docs();
2022

@@ -90,6 +92,27 @@ function alias2pkg( alias ) {
9092
console.log( txt ); // eslint-disable-line no-console
9193
} // end FUNCTION alias2pkg()
9294

95+
/**
96+
* Prints the alias corresponding to a provided package name.
97+
*
98+
* @private
99+
* @param {string} pkg - package name
100+
* @throws {TypeError} must provide a string
101+
* @returns {void}
102+
*/
103+
function pkg2alias( pkg ) {
104+
var i;
105+
if ( !isString( pkg ) ) {
106+
throw new TypeError( 'invalid input argument. Must provide a string. Value: `'+pkg+'`.' );
107+
}
108+
for ( i = 0; i < NAMESPACE.length; i++ ) {
109+
if ( pkg === NAMESPACE[ i ].path ) {
110+
return console.log( NAMESPACE[ i ].alias ); // eslint-disable-line no-console
111+
}
112+
}
113+
console.log( NO_ALIAS_TEXT ); // eslint-disable-line no-console
114+
} // end FUNCTION pkg2alias()
115+
93116

94117
// MAIN //
95118

@@ -115,7 +138,8 @@ function main() {
115138
'_help': help, // Firefox devtools already aliases `help` (and `?`) to a read-only value :(
116139
'require': require,
117140
'namespace': ns,
118-
'alias2pkg': alias2pkg
141+
'alias2pkg': alias2pkg,
142+
'pkg2alias': pkg2alias
119143
};
120144
paths = [];
121145
for ( i = 0; i < NAMESPACE.length; i++ ) {

0 commit comments

Comments
 (0)