11import { QuickPick , window } from 'vscode' ;
22
3- import { DirectoryEntry , PackageData , ValidKeyword } from './types' ;
3+ import { APIResponseData , DirectoryEntry , PackageData , ValidKeyword } from './types' ;
44import { BASE_API_URL , STRINGS } from './constants' ;
55
66export const numberFormatter = new Intl . NumberFormat ( 'en-EN' , { notation : 'compact' } ) ;
@@ -37,7 +37,12 @@ export function getCommandToRun({ dev, npmPkg }: DirectoryEntry, preferredManage
3737 }
3838}
3939
40- export async function fetchData ( query ?: string , keywords ?: ValidKeyword [ ] ) : Promise < DirectoryEntry [ ] > {
40+ const EMPTY_RESULT = {
41+ libraries : [ ] ,
42+ total : 0
43+ } ;
44+
45+ export async function fetchData ( query ?: string , keywords ?: ValidKeyword [ ] ) : Promise < APIResponseData > {
4146 try {
4247 const apiUrl = new URL ( BASE_API_URL ) ;
4348
@@ -53,26 +58,29 @@ export async function fetchData(query?: string, keywords?: ValidKeyword[]): Prom
5358 const response = await fetch ( apiUrl . href ) ;
5459
5560 if ( response . ok ) {
56- const data = ( await response . json ( ) ) as object ;
61+ const data = ( await response . json ( ) ) as APIResponseData ;
5762
5863 if ( 'libraries' in data && Array . isArray ( data . libraries ) ) {
59- return data . libraries . map ( ( item : PackageData ) => ( {
60- label : item . npmPkg ,
61- description : item . github . description ?? 'No description' ,
62- detail : getDetailLabel ( item ) ,
63- alwaysShow : true ,
64- ...item
65- } ) ) ;
64+ return {
65+ libraries : data . libraries . map ( ( item : PackageData ) => ( {
66+ label : item . npmPkg ,
67+ description : item . github . description ?? 'No description' ,
68+ detail : getDetailLabel ( item ) ,
69+ alwaysShow : true ,
70+ ...item
71+ } ) ) ,
72+ total : data . total ?? 0
73+ } ;
6674 }
6775 window . showErrorMessage ( `Invalid React Native Directory API response content` ) ;
68- return [ ] ;
76+ return EMPTY_RESULT ;
6977 }
7078 window . showErrorMessage ( `Invalid React Native Directory API response: ${ response . status } ` ) ;
71- return [ ] ;
79+ return EMPTY_RESULT ;
7280 } catch ( error ) {
7381 console . error ( error ) ;
7482 window . showErrorMessage ( 'Failed to fetch data from React Native Directory API' ) ;
75- return [ ] ;
83+ return EMPTY_RESULT ;
7684 }
7785}
7886
@@ -122,9 +130,10 @@ export async function openListWithSearch(
122130 }
123131
124132 packagesPick . show ( ) ;
125- packagesPick . items = await fetchData ( query ) ;
133+ packagesPick . items = ( await fetchData ( query ) ) . libraries ;
126134
127135 packagesPick . placeholder = STRINGS . PACKAGES_PLACEHOLDER ;
136+ packagesPick . title = STRINGS . DEFAULT_TITLE ;
128137 packagesPick . busy = false ;
129138}
130139
@@ -137,6 +146,26 @@ export function getEntryTypeLabel(entry: DirectoryEntry): string {
137146 return 'library' ;
138147}
139148
149+ export function getMatchesCountLabel ( count : number = 0 ) : string {
150+ return `${ numberFormatter . format ( count ) } ${ pluralize ( 'match' , count ) } ` ;
151+ }
152+
140153export function invertObject ( obj : Record < string , string > ) : Record < string , string > {
141154 return Object . fromEntries ( Object . entries ( obj ) . map ( ( [ k , v ] ) => [ v , k ] ) ) ;
142155}
156+
157+ export function pluralize ( word : string , count : number ) {
158+ if ( count === 1 ) {
159+ return word ;
160+ }
161+
162+ if ( / [ ^ a e i o u ] y $ / i. test ( word ) ) {
163+ return word . replace ( / y $ / i, 'ies' ) ;
164+ }
165+
166+ if ( / ( s | s h | c h | x | z ) $ / i. test ( word ) ) {
167+ return `${ word } es` ;
168+ }
169+
170+ return `${ word } s` ;
171+ }
0 commit comments