@@ -332,6 +332,52 @@ export const truncate = (string, to = 100) => {
332
332
return `${ string . slice ( 0 , to ) } ...` ;
333
333
} ;
334
334
335
+ const collectionIdSeparator = '___' ;
336
+ /**
337
+ * Returns the name and version of a collection based on
338
+ * the collectionId used in elasticsearch indexing
339
+ *
340
+ * @param {string } collectionId - collectionId used in elasticsearch index
341
+ * @returns {Object } name and version as object
342
+ */
343
+ export const deconstructCollectionId = ( collectionId ) => {
344
+ let name ;
345
+ let version ;
346
+ try {
347
+ const last = collectionId . lastIndexOf ( collectionIdSeparator ) ;
348
+ name = collectionId . substring ( 0 , last ) ;
349
+ version = collectionId . substring ( last + collectionIdSeparator . length ) ;
350
+ if ( name && version ) {
351
+ return {
352
+ name,
353
+ version,
354
+ } ;
355
+ }
356
+ } catch ( error ) {
357
+ // do nothing error thrown below
358
+ }
359
+ if ( collectionId && collectionId . match ( ' / ' ) ) {
360
+ console . debug ( `deconstructCollectionId called with previously deconstructed ID ${ collectionId } ` ) ;
361
+ return collectionId ;
362
+ }
363
+ throw new Error ( `invalid collectionId: ${ JSON . stringify ( collectionId ) } ` ) ;
364
+ } ;
365
+
366
+ // "MYD13A1___006" => "MYD13A1 / 006"
367
+ // "trailing_____1.0" => "trailing__ / 1.0"
368
+ export const collectionName = ( collectionId ) => {
369
+ if ( ! collectionId ) return nullValue ;
370
+ const collection = deconstructCollectionId ( collectionId ) ;
371
+ return `${ collection . name } / ${ collection . version } ` ;
372
+ } ;
373
+
374
+ export const collectionNameVersion = ( collectionId ) => {
375
+ if ( ! collectionId ) return nullValue ;
376
+ return deconstructCollectionId ( collectionId ) ;
377
+ } ;
378
+
379
+ export const constructCollectionId = ( name , version ) => `${ name } ${ collectionIdSeparator } ${ version } ` ;
380
+
335
381
export const getFormattedCollectionId = ( collection ) => {
336
382
const collectionId = getCollectionId ( collection ) ;
337
383
return formatCollectionId ( collectionId ) ;
@@ -341,46 +387,17 @@ export const formatCollectionId = (collectionId) => (collectionId === undefined
341
387
342
388
export const getCollectionId = ( collection ) => {
343
389
if ( collection && collection . name && collection . version ) {
344
- return ` ${ collection . name } ___ ${ collection . version } ` ;
390
+ return constructCollectionId ( collection . name , collection . version ) ;
345
391
}
346
392
} ;
347
393
348
394
export const getEncodedCollectionId = ( collection ) => {
349
395
if ( collection && collection . name && collection . version ) {
350
- return ` ${ collection . name } ___ ${ encodeURIComponent ( collection . version ) } ` ;
396
+ return constructCollectionId ( collection . name , encodeURIComponent ( collection . version ) ) ;
351
397
}
352
398
return nullValue ;
353
399
} ;
354
400
355
- // "MYD13A1___006" => "MYD13A1 / 006"
356
- export const collectionName = ( collectionId ) => {
357
- if ( ! collectionId ) return nullValue ;
358
- return collectionId . split ( '___' ) . join ( ' / ' ) ;
359
- } ;
360
-
361
- export const collectionNameVersion = ( collectionId ) => {
362
- if ( ! collectionId ) return nullValue ;
363
- const [ name , version ] = collectionId . split ( '___' ) ;
364
- return { name, version } ;
365
- } ;
366
-
367
- export const constructCollectionNameVersion = ( name , version ) => `${ name } ___${ version } ` ;
368
-
369
- /**
370
- * Returns the name and version of a collection based on
371
- * the collectionId used in elasticsearch indexing
372
- *
373
- * @param {string } collectionId - collectionId used in elasticsearch index
374
- * @returns {Object } name and version as object
375
- */
376
- export const deconstructCollectionId = ( collectionId ) => {
377
- const [ name , version ] = collectionId . split ( '___' ) ;
378
- return {
379
- name,
380
- version,
381
- } ;
382
- } ;
383
-
384
401
export const collectionLink = ( collectionId ) => {
385
402
if ( ! collectionId || collectionId === nullValue ) return nullValue ;
386
403
return (
0 commit comments