22// See LICENSE in the project root for license information.
33
44/**
5- * Result object returned by { @link PackageName.tryParse}
5+ * A package name that has been separated into its scope and unscoped name.
66 *
77 * @public
88 */
9- export interface IParsePackageNameResult {
9+ export interface IParsedPackageName {
1010 /**
1111 * The parsed NPM scope, or an empty string if there was no scope. The scope value will
1212 * always include the at-sign.
@@ -21,10 +21,17 @@ export interface IParsePackageNameResult {
2121 * For example, if the parsed input was "\@scope/example", then the name would be "example".
2222 */
2323 unscopedName : string ;
24+ }
2425
26+ /**
27+ * Result object returned by {@link PackageName.tryParse}
28+ *
29+ * @public
30+ */
31+ export interface IParsedPackageNameOrError extends IParsedPackageName {
2532 /**
2633 * If the input string could not be parsed, then this string will contain a nonempty
27- * error message. Otherwise it will be an empty string.
34+ * error message. Otherwise it will be an empty string.
2835 */
2936 error : string ;
3037}
@@ -43,17 +50,19 @@ export class PackageName {
4350 /**
4451 * This attempts to parse a NPM package name that may optionally include a path component.
4552 * @remarks
46- * @returns an {@link IParsePackageNameResult} structure whose `error` property will be
53+ * This function will not throw an exception.
54+ *
55+ * @returns an {@link IParsedPackageNameOrError} structure whose `error` property will be
4756 * nonempty if the string could not be parsed.
4857 */
49- public static tryParse ( name : string ) : IParsePackageNameResult {
50- const result : IParsePackageNameResult = {
58+ public static tryParse ( packageName : string ) : IParsedPackageNameOrError {
59+ const result : IParsedPackageNameOrError = {
5160 scope : '' ,
5261 unscopedName : '' ,
5362 error : ''
5463 } ;
5564
56- let input : string = name ;
65+ let input : string = packageName ;
5766
5867 if ( input === null || input === undefined ) {
5968 result . error = 'The package name must not be null or undefined' ;
@@ -123,19 +132,35 @@ export class PackageName {
123132 * Same as {@link PackageName.tryParse}, except this throws an exception if the input
124133 * cannot be parsed
125134 */
126- public static parse ( name : string ) : IParsePackageNameResult {
127- const result : IParsePackageNameResult = PackageName . tryParse ( name ) ;
135+ public static parse ( packageName : string ) : IParsedPackageName {
136+ const result : IParsedPackageNameOrError = PackageName . tryParse ( packageName ) ;
128137 if ( result . error ) {
129138 throw new Error ( result . error ) ;
130139 }
131140 return result ;
132141 }
133142
134143 /**
135- * Returns true if the specified package name is valid.
144+ * {@inheritdoc IParsedPackageName.scope }
145+ */
146+ public static getScope ( packageName : string ) : string {
147+ return PackageName . parse ( packageName ) . scope ;
148+ }
149+
150+ /**
151+ * {@inheritdoc IParsedPackageName.unscopedName }
152+ */
153+ public static getUnscopedName ( packageName : string ) : string {
154+ return PackageName . parse ( packageName ) . unscopedName ;
155+ }
156+
157+ /**
158+ * Returns true if the specified package name is valid, or false otherwise.
159+ * @remarks
160+ * This function will not throw an exception.
136161 */
137162 public static isValidName ( packageName : string ) : boolean {
138- const result : IParsePackageNameResult = PackageName . tryParse ( packageName ) ;
163+ const result : IParsedPackageNameOrError = PackageName . tryParse ( packageName ) ;
139164 return ! result . error ;
140165 }
141166
0 commit comments