Skip to content

Commit

Permalink
Merge pull request #431 from Groww-OSS/develop
Browse files Browse the repository at this point in the history
Develop to Prod [8th July 2024]
  • Loading branch information
prashant-adhikari-groww authored Jul 8, 2024
2 parents e4f5222 + 3616407 commit ed6ebf5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/web-storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@groww-tech/web-storage",
"version": "0.0.4",
"version": "0.0.5",
"description": "Web Storage is a service used that exposes methods to get full control over storage mechanisms available via bucketing.",
"main": "dist/index.js",
"module": "dist/esm/index.js",
Expand All @@ -15,6 +15,7 @@
},
"scripts": {
"build": "tsup --env.NODE_ENV production",
"watch": "tsup --watch --env.NODE_ENV production",
"test" : "vitest run",
"pushTags": "PACKAGE_VERSION=$(cat package.json | grep \\\"version\\\" | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git tag -a web-storage-v$PACKAGE_VERSION -m \"@groww-tech/web-storage-v$PACKAGE_VERSION\" && git push --tags"
},
Expand Down
23 changes: 10 additions & 13 deletions packages/web-storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
getUserProvidedKeyFromStoredKey
} from './helpers';


/**
* Get Data from Storage
*
Expand Down Expand Up @@ -215,7 +214,6 @@ export function clearStorage(storageType: string) {
}
}


/**
* Clears the Local Storage based on the original exported method i.e; clearStorage();
*
Expand Down Expand Up @@ -286,27 +284,26 @@ function clearStorageCookies() {
*/

export function clearBucketStorage(bucket: string) {
const localStorageLength = localStorage.length;
const keysToRemove = [];

// PLEASE NOTE:-
//
// we are keeping reverse array because everytime if() is true, the original array gets altered
// and the keys move 1 index up. For example:- If LS length is initial 10 and iterator is at 0.
// If we remove the first key, LS length becomes 9 and iterator will be at 1. The key that will be
// initial at index 1 will be moving to index 0 now. And since the iterator is at 1, index 0 will not be deleted.
// this is why we are keeping the loop backwards startin from length - 1;
//
for (let index = localStorageLength - 1; index >= 0; index--) {
for (let index = 0; index < localStorage.length; index++) {
const key = localStorage.key(index) || '';

const userKey = getUserProvidedKeyFromStoredKey(key);

// we want to extract bucket name from original stored key.
// and remove the keys only that stores values not expiration time.
// they will automatically be cleared after the original key is removed.
if (getBucketNameFromKey(key) === bucket && !userKey.includes('-exptime')) {
clearKeyFromStorage(userKey, STORAGE_TYPE.LOCAL_STORAGE, bucket);
keysToRemove.push(userKey);
}
}

// we are removing the keys in a separate loop because if we remove the keys in the above loop
// the length of the localStorage will change and the index may be altered.
keysToRemove.forEach((userKey) => {
clearKeyFromStorage(userKey, STORAGE_TYPE.LOCAL_STORAGE, bucket);
});
}

/**
Expand Down

0 comments on commit ed6ebf5

Please sign in to comment.