svelte-common 6.0.1 → 6.1.1

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 +2 -2
  2. package/src/pagination.mjs +35 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-common",
3
- "version": "6.0.1",
3
+ "version": "6.1.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -48,7 +48,7 @@
48
48
  "ava": "^5.3.1",
49
49
  "c8": "^8.0.0",
50
50
  "documentation": "^14.0.2",
51
- "mf-styling": "^2.0.1",
51
+ "mf-styling": "^2.0.2",
52
52
  "npm-pkgbuild": "^11.8.14",
53
53
  "semantic-release": "^21.0.7",
54
54
  "stylelint": "^15.10.1",
@@ -1,3 +1,4 @@
1
+
1
2
  /**
2
3
  * Pagination support store.
3
4
  * Pages go from 1 ... numberOfPages
@@ -9,15 +10,26 @@ export class Pagination {
9
10
  #subscriptions = new Set();
10
11
  #data;
11
12
  #unsubscribeData;
12
-
13
+ #filter;
13
14
  #itemsPerPage = 20;
14
15
  #page = 1;
15
16
 
16
17
  constructor(data, options) {
17
18
  this.data = data;
18
- if (options?.itemsPerPage) {
19
- this.itemsPerPage = options.itemsPerPage;
20
- }
19
+
20
+ Object.assign(this, options);
21
+ }
22
+
23
+ set filter(filter)
24
+ {
25
+ this.#filter = filter;
26
+
27
+ this.#subscriptions.forEach(subscription => subscription(this));
28
+ }
29
+
30
+ get filter()
31
+ {
32
+ return this.#filter;
21
33
  }
22
34
 
23
35
  set data(data) {
@@ -73,10 +85,20 @@ export class Pagination {
73
85
  }
74
86
 
75
87
  get numberOfPages() {
76
- return Math.ceil(
77
- (Array.isArray(this.#data) ? this.#data.length : this.#data.size) /
78
- this.itemsPerPage
79
- );
88
+ let n;
89
+
90
+ if(this.filter) {
91
+ let data = Array.isArray(this.data)
92
+ ? this.#data
93
+ : [...this.#data.values()];
94
+ data = data.filter(this.filter);
95
+ n = data.length;
96
+ }
97
+ else {
98
+ n = Array.isArray(this.#data) ? this.#data.length : this.#data.size;
99
+ }
100
+
101
+ return Math.ceil(n / this.itemsPerPage);
80
102
  }
81
103
 
82
104
  get length() {
@@ -86,10 +108,14 @@ export class Pagination {
86
108
  *[Symbol.iterator]() {
87
109
  const n = this.page - 1;
88
110
 
89
- const data = Array.isArray(this.data)
111
+ let data = Array.isArray(this.data)
90
112
  ? this.#data
91
113
  : [...this.#data.values()];
92
114
 
115
+ if(this.filter) {
116
+ data = data.filter(this.filter);
117
+ }
118
+
93
119
  for (const item of data.slice(
94
120
  n * this.itemsPerPage,
95
121
  (n + 1) * this.itemsPerPage