svelte-common 5.0.3 → 5.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/pagination.mjs +29 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-common",
3
- "version": "5.0.3",
3
+ "version": "5.0.4",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -3,16 +3,30 @@
3
3
  * Pages go from 1 ... numberOfPages
4
4
  */
5
5
  export class Pagination {
6
- subscriptions = new Set();
7
- data;
8
- itemsPerPage;
6
+ #subscriptions = new Set();
7
+ #data;
8
+ #itemsPerPage = 1;
9
9
  #page = 1;
10
10
 
11
- constructor(source, itemsPerPage = 10) {
12
- this.data = [...source];
11
+ constructor(data, itemsPerPage = 10) {
12
+ this.#data = data;
13
13
  this.itemsPerPage = itemsPerPage;
14
14
  }
15
15
 
16
+ set data(data) {
17
+ this.#data = data;
18
+ this.#subscriptions.forEach(subscription => subscription(this));
19
+ }
20
+
21
+ get itemsPerPage() {
22
+ return this.#itemsPerPage;
23
+ }
24
+
25
+ set itemsPerPage(n) {
26
+ this.#itemsPerPage = n;
27
+ this.#subscriptions.forEach(subscription => subscription(this));
28
+ }
29
+
16
30
  /**
17
31
  * Set current page
18
32
  * @param {number} n
@@ -20,7 +34,7 @@ export class Pagination {
20
34
  set page(n) {
21
35
  if (this.#page !== n && n >= 1 && n <= this.numberOfPages) {
22
36
  this.#page = n;
23
- this.subscriptions.forEach(subscription => subscription(this));
37
+ this.#subscriptions.forEach(subscription => subscription(this));
24
38
  }
25
39
  }
26
40
 
@@ -32,21 +46,26 @@ export class Pagination {
32
46
  }
33
47
 
34
48
  subscribe(s) {
35
- this.subscriptions.add(s);
49
+ this.#subscriptions.add(s);
36
50
 
37
51
  s(this);
38
52
 
39
- return () => this.subscriptions.delete(s);
53
+ return () => this.#subscriptions.delete(s);
40
54
  }
41
55
 
42
56
  get numberOfPages() {
43
- return this.data.length / this.itemsPerPage;
57
+ return (
58
+ (Array.isArray(this.#data) ? this.#data.length : this.#data.size) /
59
+ this.itemsPerPage
60
+ );
44
61
  }
45
62
 
46
63
  *items() {
47
64
  const n = this.page - 1;
48
65
 
49
- for (const item of this.data.slice(
66
+ const data = Array.isArray(this.data) ? this.#data : [...this.#data.values()];
67
+
68
+ for (const item of data.slice(
50
69
  n * this.itemsPerPage,
51
70
  (n + 1) * this.itemsPerPage
52
71
  )) {