Skip to content

Commit 05dd86c

Browse files
committed
Separate IParsePackageNameResult into IParsedPackageName and IParsedPackageNameOrError, add getScope() and getUnscopedName()
1 parent 99119c7 commit 05dd86c

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

common/reviews/api/node-core-library.api.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,16 @@ interface IPackageJsonTsdocConfiguration {
8282
}
8383

8484
// @public
85-
interface IParsePackageNameResult {
86-
error: string;
85+
interface IParsedPackageName {
8786
scope: string;
8887
unscopedName: string;
8988
}
9089

90+
// @public
91+
interface IParsedPackageNameOrError extends IParsedPackageName {
92+
error: string;
93+
}
94+
9195
// @public
9296
interface IProtectableMapParameters<K, V> {
9397
onClear?: (source: ProtectableMap<K, V>) => void;
@@ -139,9 +143,11 @@ class PackageJsonLookup {
139143
// @public
140144
class PackageName {
141145
static combineParts(scope: string, unscopedName: string): string;
146+
static getScope(packageName: string): string;
147+
static getUnscopedName(packageName: string): string;
142148
static isValidName(packageName: string): boolean;
143-
static parse(name: string): IParsePackageNameResult;
144-
static tryParse(name: string): IParsePackageNameResult;
149+
static parse(packageName: string): IParsedPackageName;
150+
static tryParse(packageName: string): IParsedPackageNameOrError;
145151
}
146152

147153
// @public

libraries/node-core-library/src/PackageName.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
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

libraries/node-core-library/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ export {
4040
IPackageJsonLookupParameters,
4141
PackageJsonLookup
4242
} from './PackageJsonLookup';
43-
export { PackageName, IParsePackageNameResult } from './PackageName';
43+
export { PackageName, IParsedPackageName, IParsedPackageNameOrError } from './PackageName';
4444
export { Path } from './Path';
4545
export { Text } from './Text';

0 commit comments

Comments
 (0)