Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 1901991

Browse files
committed
feat: add support for all Array's methods
1 parent 9d3573e commit 1901991

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ directive (class property) using HTML5' LocalStorage.
77

88
Things that have been added in this fork:
99
- added `.save()` method on returned object, used in specific cases to force save object changes
10-
- support for `Array` methods that change its value (`push`, `pop` and `shift` to be exact)
10+
- support for all `Array` methods that change array object's value
1111
- now `WebStorageService.clear()` method removes items created by this repository only
1212
- storage key prefix (`angular2ws_` by default) can be customized by changing `WEBSTORAGE_CONFIG.prefix` property
1313

src/decorator/webstorage.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,17 @@ export let WebStorage = (webStorage: Storage, service: WebStorageServiceInterfac
4141

4242
// handle methods changing value of array
4343
if (Array.isArray(proxy)) {
44-
proxy.push = function(value) {
45-
let result = Array.prototype.push.apply(proxy, arguments);
46-
WebStorageUtility.set(webStorage, key, proxy);
47-
return result;
48-
};
49-
proxy.pop = function() {
50-
let result = Array.prototype.pop.apply(proxy, arguments);
51-
WebStorageUtility.set(webStorage, key, proxy);
52-
return result;
53-
};
54-
proxy.shift = function() {
55-
let result = Array.prototype.shift.apply(proxy, arguments);
56-
WebStorageUtility.set(webStorage, key, proxy);
57-
return result;
58-
};
44+
const methodsToOverwrite = [
45+
'join', 'pop', 'push', 'reverse', 'shift', 'unshift', 'splice',
46+
'filter', 'forEach', 'map', 'fill', 'sort', 'copyWithin'
47+
];
48+
for (let method of methodsToOverwrite) {
49+
proxy[method] = function(value) {
50+
let result = Array.prototype[method].apply(proxy, arguments);
51+
WebStorageUtility.set(webStorage, key, proxy);
52+
return result;
53+
}
54+
}
5955
}
6056
},
6157
});

0 commit comments

Comments
 (0)