Skip to content

Commit

Permalink
feat: add len method to expose store length (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoumitaM authored Apr 17, 2024
1 parent b4951c2 commit f3c7e09
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ store.clear(); // Clean all key/data
store.keys(); // Returns an array of all the keys
store.forEach(callback); // Loop traversal, return false to end traversal
store.search(string); // Search method
store.len(); // Returns store length

store.has(key); //⇒ Determine if there is a return true/false

Expand Down Expand Up @@ -160,6 +161,14 @@ store.remove('w1'); // Delete w1 and return the value of w1
store('w1', false) // So also delete w1
```

### len

Returns the length of the store `store.len()`

```js
store.len(); //⇒ 10
```

### forEach

Loop traversal, return `false` to end the traversal
Expand Down
5 changes: 4 additions & 1 deletion dist/store.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* storejs v2.0.7
* Local storage localstorage package provides a simple API
*
* Copyright (c) 2023 kenny wang <wowohoo@qq.com>
* Copyright (c) 2024 kenny wang <wowohoo@qq.com>
* https://jaywcjlove.github.io/store.js/
*
* Licensed under the MIT license.
Expand Down Expand Up @@ -144,6 +144,9 @@ Store.prototype = {
if (arr[i].indexOf(str) > -1) dt[arr[i]] = this.get(arr[i]);
}
return dt;
},
len: function len() {
return storage.length;
}
};
var _Store = null;
Expand Down
5 changes: 4 additions & 1 deletion dist/store.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* storejs v2.0.7
* Local storage localstorage package provides a simple API
*
* Copyright (c) 2023 kenny wang <wowohoo@qq.com>
* Copyright (c) 2024 kenny wang <wowohoo@qq.com>
* https://jaywcjlove.github.io/store.js/
*
* Licensed under the MIT license.
Expand Down Expand Up @@ -142,6 +142,9 @@ Store.prototype = {
if (arr[i].indexOf(str) > -1) dt[arr[i]] = this.get(arr[i]);
}
return dt;
},
len: function len() {
return storage.length;
}
};
var _Store = null;
Expand Down
5 changes: 4 additions & 1 deletion dist/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* storejs v2.0.7
* Local storage localstorage package provides a simple API
*
* Copyright (c) 2023 kenny wang <wowohoo@qq.com>
* Copyright (c) 2024 kenny wang <wowohoo@qq.com>
* https://jaywcjlove.github.io/store.js/
*
* Licensed under the MIT license.
Expand Down Expand Up @@ -148,6 +148,9 @@
if (arr[i].indexOf(str) > -1) dt[arr[i]] = this.get(arr[i]);
}
return dt;
},
len: function len() {
return storage.length;
}
};
var _Store = null;
Expand Down
4 changes: 2 additions & 2 deletions dist/store.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/__test__/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,12 @@ test('Test Change localstorage', () => {
})).toEqual(store);
expect(store.get()).toEqual({ "name6": 123, "name6,name7": "keytest", "test": [3, 4, 5] });
});

test('Get store length', () => {
store.clear()
store.set('name6', 'value6')
store.set('name7', 'value7')
expect(store.len()).toEqual(2);
store.clear()
expect(store.len()).toEqual(0);
});
3 changes: 3 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ Store.prototype = {
if (arr[i].indexOf(str) > -1) dt[arr[i]] = this.get(arr[i]);
}
return dt;
},
len: function () {
return storage.length;
}
}

Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ declare class Store {
search(keyword: string): Record<string, any>

clear(): Store

len(): number
}

declare let s: typeof store & Store
Expand Down

0 comments on commit f3c7e09

Please sign in to comment.