Skip to content

Commit bb4813c

Browse files
committed
refactor: abstract prefix-building to a reusable function for consistency
1 parent adcec53 commit bb4813c

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

src/reader-activation/store.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ function initializeSyncInterval( queue ) {
4242
}, 1000 );
4343
}
4444

45+
/**
46+
* Get store item key
47+
*
48+
* @param {boolean} internal Whether it's an internal (bookkeeping) prefix.
49+
*
50+
* @return {string} Store prefix string.
51+
*/
52+
function getStorePrefix( internal = false ) {
53+
const parts = [ config.storePrefix ];
54+
if ( internal ) {
55+
parts.push( '_' );
56+
}
57+
return parts.join( '' );
58+
}
59+
4560
/**
4661
* Get store item key
4762
*
@@ -54,12 +69,7 @@ export function getStoreItemKey( key, internal = false ) {
5469
if ( ! key ) {
5570
throw new Error( 'Key is required.' );
5671
}
57-
const parts = [ config.storePrefix ];
58-
if ( internal ) {
59-
parts.push( '_' );
60-
}
61-
parts.push( key );
62-
return parts.join( '' );
72+
return getStorePrefix( internal ) + key;
6373
}
6474

6575
/**
@@ -282,16 +292,16 @@ export default function Store() {
282292
*/
283293
getAll: () => {
284294
const data = {};
285-
const { storePrefix, storage } = config;
286-
const internalPrefix = storePrefix + '_';
287-
for ( let i = 0; i < storage.length; i++ ) {
288-
const storageKey = storage.key( i );
295+
const prefix = getStorePrefix( false );
296+
const internalPrefix = getStorePrefix( true );
297+
for ( let i = 0; i < config.storage.length; i++ ) {
298+
const storageKey = config.storage.key( i );
289299
if ( ! storageKey ) {
290300
continue;
291301
}
292-
if ( storageKey.startsWith( storePrefix ) && ! storageKey.startsWith( internalPrefix ) ) {
293-
const key = storageKey.slice( storePrefix.length );
294-
data[ key ] = decode( storage.getItem( storageKey ) );
302+
if ( storageKey.startsWith( prefix ) && ! storageKey.startsWith( internalPrefix ) ) {
303+
const key = storageKey.slice( prefix.length );
304+
data[ key ] = decode( config.storage.getItem( storageKey ) );
295305
}
296306
}
297307
return data;

0 commit comments

Comments
 (0)