@visulima/pagination 1.0.1 → 1.0.3

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## @visulima/pagination [1.0.3](https://github.com/visulima/visulima/compare/@visulima/pagination@1.0.2...@visulima/pagination@1.0.3) (2022-12-15)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **pagination:** updated readme with missing example, added missing types export ([37d7782](https://github.com/visulima/visulima/commit/37d77829cef00277f09b847a8b429a53a96c0522))
7
+
8
+ ## @visulima/pagination [1.0.2](https://github.com/visulima/visulima/compare/@visulima/pagination@1.0.1...@visulima/pagination@1.0.2) (2022-12-10)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * fixed wrong file name for module key in package.json ([2c90a2d](https://github.com/visulima/visulima/commit/2c90a2d75848a159bc33dc58f303af36ee127570))
14
+
1
15
  ## @visulima/pagination [1.0.1](https://github.com/visulima/visulima/compare/@visulima/pagination@1.0.0...@visulima/pagination@1.0.1) (2022-12-01)
2
16
 
3
17
 
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  <div align="center">
2
2
  <h3>Visulima pagination</h3>
3
3
  <p>
4
- Visulima pagination is ... .
4
+ Visulima pagination is a simple pagination for node.
5
5
 
6
6
  </p>
7
7
  </div>
@@ -45,7 +45,27 @@ pnpm add @visulima/pagination
45
45
  ## Usage
46
46
 
47
47
  ```ts
48
- import { createPagination } from "@visulima/pagination";
48
+ import { paginate } from "@visulima/pagination";
49
+
50
+ const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
51
+
52
+ const pagination = paginate(1, 5, items.length, items);
53
+
54
+ console.log(pagination);
55
+ // {
56
+ // items: [1, 2, 3, 4, 5],
57
+ // meta: {
58
+ // total: 10,
59
+ // perPage: 5,
60
+ // page: 1,
61
+ // lastPage: 2,
62
+ // firstPage: 1,
63
+ // firstPageUrl: "/?page=1",
64
+ // lastPageUrl: "/?page=2",
65
+ // nextPageUrl: "/?page=2",
66
+ // previousPageUrl: null,
67
+ // }
68
+ // }
49
69
 
50
70
  ```
51
71
 
package/dist/index.d.ts CHANGED
@@ -91,4 +91,4 @@ declare class Paginator<T = any> extends Array implements Paginator$1<T> {
91
91
 
92
92
  declare const paginate: <Result>(page: number, perPage: number, total: number, rows: Result[]) => Paginator$1<Result>;
93
93
 
94
- export { Paginator, Paginator$1 as PaginatorInterface, paginate };
94
+ export { PaginationMeta, PaginationResult, Paginator, Paginator$1 as PaginatorInterface, paginate };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/paginator.ts","../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,QAAQ;AAQf,IAAqB,YAArB,cAAgD,MAA+B;AAAA,EAiB3E,YAA6B,aAAqC,SAAwB,gBAAwB,MAAa;AAC3H,UAAM,GAAG,IAAI;AADY;AAAqC;AAAwB;AAGtF,SAAK,cAAc,OAAO,WAAW;AAErC,SAAK,OAAO;AACZ,SAAK,UAAU,KAAK,KAAK,WAAW;AAAA,EACxC;AAAA,EAvBQ,KAA6B,CAAC;AAAA,EAE9B,MAAc;AAAA,EAEL;AAAA,EAKD,YAAoB;AAAA,EAKpB;AAAA,EAkBhB,IAAI,WAAoB;AACpB,WAAO,KAAK,QAAQ;AAAA,EACxB;AAAA,EAKA,IAAI,eAAwB;AACxB,WAAO,KAAK,WAAW,KAAK;AAAA,EAChC;AAAA,EAKA,IAAI,WAAoB;AACpB,WAAO,KAAK,aAAa;AAAA,EAC7B;AAAA,EAKA,IAAI,WAAmB;AACnB,WAAO,KAAK,IAAI,KAAK,KAAK,KAAK,QAAQ,KAAK,OAAO,GAAG,CAAC;AAAA,EAC3D;AAAA,EAMA,IAAI,QAAgB;AAChB,WAAO,OAAO,KAAK,WAAW;AAAA,EAClC;AAAA,EAKO,MAAM;AACT,WAAO,KAAK;AAAA,EAChB;AAAA,EAKO,UAAU;AACb,WAAO;AAAA,M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A+C,IAAI,UAAU,OAAO,OAAO,OAAO,GAAG,OAAO,IAAI,GAAG,GAAG,IAAI","sourcesContent":["import qs from \"qs\";\n\nimport type { Paginator as IPaginator } from \"./types\";\n\n/**\n * Simple paginator works with the data set provided by the standard\n * `offset` and `limit` based pagination.\n */\nexport default class Paginator<T = any> extends Array implements IPaginator<T> {\n private qs: { [key: string]: any } = {};\n\n private url: string = \"/\";\n\n private readonly rows: any[];\n\n /**\n * The first page is always 1\n */\n public readonly firstPage: number = 1;\n\n /**\n * Find if results set is empty or not\n */\n public readonly isEmpty: boolean;\n\n constructor(private readonly totalNumber: number, public readonly perPage: number, public currentPage: number, ...rows: any[]) {\n super(...rows);\n\n this.totalNumber = Number(totalNumber);\n\n this.rows = rows;\n this.isEmpty = this.rows.length === 0;\n }\n\n /**\n * Find if there are total records or not. This is not same as\n * `isEmpty`.\n *\n * The `isEmpty` reports about the current set of results. However, `hasTotal`\n * reports about the total number of records, regardless of the current.\n */\n get hasTotal(): boolean {\n return this.total > 0;\n }\n\n /**\n * Find if there are more pages to come\n */\n get hasMorePages(): boolean {\n return this.lastPage > this.currentPage;\n }\n\n /**\n * Find if there are enough results to be paginated or not\n */\n get hasPages(): boolean {\n return this.lastPage !== 1;\n }\n\n /**\n * The Last page number\n */\n get lastPage(): number {\n return Math.max(Math.ceil(this.total / this.perPage), 1);\n }\n\n /**\n * Casting `total` to a number. Later, we can think of situations\n * to cast it to a bigint\n */\n get total(): number {\n return Number(this.totalNumber);\n }\n\n /**\n * A reference to the result rows\n */\n public all() {\n return this.rows;\n }\n\n /**\n * Returns JSON meta data\n */\n public getMeta() {\n return {\n total: this.total,\n perPage: this.perPage,\n page: this.currentPage,\n lastPage: this.lastPage,\n firstPage: this.firstPage,\n firstPageUrl: this.getUrl(1),\n lastPageUrl: this.getUrl(this.lastPage),\n nextPageUrl: this.getNextPageUrl(),\n previousPageUrl: this.getPreviousPageUrl(),\n };\n }\n\n /**\n * Returns JSON representation of the paginated\n * data\n */\n public toJSON() {\n return {\n meta: this.getMeta(),\n data: this.all(),\n };\n }\n\n /**\n * Define query string to be appended to the pagination links\n */\n public queryString(values: { [key: string]: any }): this {\n this.qs = values;\n\n return this;\n }\n\n /**\n * Define base url for making the pagination links\n */\n public baseUrl(url: string): this {\n this.url = url;\n\n return this;\n }\n\n /**\n * Returns url for a given page. Doesn't validate the integrity of the\n * page\n */\n public getUrl(page: number): string {\n const qstring = qs.stringify({ ...this.qs, page: page < 1 ? 1 : page });\n\n return `${this.url}?${qstring}`;\n }\n\n /**\n * Returns url for the next page\n */\n public getNextPageUrl(): string | null {\n if (this.hasMorePages) {\n return this.getUrl(this.currentPage + 1);\n }\n\n return null;\n }\n\n /**\n * Returns URL for the previous page\n */\n public getPreviousPageUrl(): string | null {\n if (this.currentPage > 1) {\n return this.getUrl(this.currentPage - 1);\n }\n\n return null;\n }\n\n /**\n * Returns an array of urls under a given range\n */\n public getUrlsForRange(start: number, end: number) {\n const urls: { url: string; page: number; isActive: boolean }[] = [];\n\n // eslint-disable-next-line no-plusplus\n for (let index = start; index <= end; index++) {\n urls.push({ url: this.getUrl(index), page: index, isActive: index === this.currentPage });\n }\n\n return urls;\n }\n}\n","import Paginator from \"./paginator\";\nimport type { Paginator as PaginatorInterface } from \"./types\";\n\nexport type { Paginator as PaginatorInterface } from \"./types.d\";\nexport { default as Paginator } from \"./paginator\";\n\n// eslint-disable-next-line max-len\nexport const paginate = <Result>(page: number, perPage: number, total: number, rows: Result[]): PaginatorInterface<Result> => new Paginator(total, Number(perPage), Number(page), ...rows);\n"]}
1
+ {"version":3,"sources":["../src/paginator.ts","../src/index.ts"],"names":[],"mappings":";AAAA,OAAO,QAAQ;AAQf,IAAqB,YAArB,cAAgD,MAA+B;AAAA,EAiB3E,YAA6B,aAAqC,SAAwB,gBAAwB,MAAa;AAC3H,UAAM,GAAG,IAAI;AADY;AAAqC;AAAwB;AAGtF,SAAK,cAAc,OAAO,WAAW;AAErC,SAAK,OAAO;AACZ,SAAK,UA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,WAAO,GAAG,KAAK,OAAO;AAAA,EAC1B;AAAA,EAKO,iBAAgC;AACnC,QAAI,KAAK,cAAc;AACnB,aAAO,KAAK,OAAO,KAAK,cAAc,CAAC;AAAA,IAC3C;AAEA,WAAO;AAAA,EACX;AAAA,EAKO,qBAAoC;AACvC,QAAI,KAAK,cAAc,GAAG;AACtB,aAAO,KAAK,OAAO,KAAK,cAAc,CAAC;AAAA,IAC3C;AAEA,WAAO;AAAA,EACX;AAAA,EAKO,gBAAgB,OAAe,KAAa;AAC/C,UAAM,OAA2D,CAAC;AAGlE,aAAS,QAAQ,OAAO,SAAS,KAAK,SAAS;AAC3C,WAAK,KAAK,EAAE,KAAK,KAAK,OAAO,KAAK,GAAG,MAAM,OAAO,UAAU,UAAU,KAAK,YAAY,CAAC;AAAA,IAC5F;AAEA,WAAO;AAAA,EACX;AACJ;;;ACrKO,IAAM,WAAW,CAAS,MAAc,SAAiB,OAAe,SAA+C,IAAI,UAAU,OAAO,OAAO,OAAO,GAAG,OAAO,IAAI,GAAG,GAAG,IAAI","sourcesContent":["import qs from \"qs\";\n\nimport type { Paginator as IPaginator } from \"./types\";\n\n/**\n * Simple paginator works with the data set provided by the standard\n * `offset` and `limit` based pagination.\n */\nexport default class Paginator<T = any> extends Array implements IPaginator<T> {\n private qs: { [key: string]: any } = {};\n\n private url: string = \"/\";\n\n private readonly rows: any[];\n\n /**\n * The first page is always 1\n */\n public readonly firstPage: number = 1;\n\n /**\n * Find if results set is empty or not\n */\n public readonly isEmpty: boolean;\n\n constructor(private readonly totalNumber: number, public readonly perPage: number, public currentPage: number, ...rows: any[]) {\n super(...rows);\n\n this.totalNumber = Number(totalNumber);\n\n this.rows = rows;\n this.isEmpty = this.rows.length === 0;\n }\n\n /**\n * Find if there are total records or not. This is not same as\n * `isEmpty`.\n *\n * The `isEmpty` reports about the current set of results. However, `hasTotal`\n * reports about the total number of records, regardless of the current.\n */\n get hasTotal(): boolean {\n return this.total > 0;\n }\n\n /**\n * Find if there are more pages to come\n */\n get hasMorePages(): boolean {\n return this.lastPage > this.currentPage;\n }\n\n /**\n * Find if there are enough results to be paginated or not\n */\n get hasPages(): boolean {\n return this.lastPage !== 1;\n }\n\n /**\n * The Last page number\n */\n get lastPage(): number {\n return Math.max(Math.ceil(this.total / this.perPage), 1);\n }\n\n /**\n * Casting `total` to a number. Later, we can think of situations\n * to cast it to a bigint\n */\n get total(): number {\n return Number(this.totalNumber);\n }\n\n /**\n * A reference to the result rows\n */\n public all() {\n return this.rows;\n }\n\n /**\n * Returns JSON meta data\n */\n public getMeta() {\n return {\n total: this.total,\n perPage: this.perPage,\n page: this.currentPage,\n lastPage: this.lastPage,\n firstPage: this.firstPage,\n firstPageUrl: this.getUrl(1),\n lastPageUrl: this.getUrl(this.lastPage),\n nextPageUrl: this.getNextPageUrl(),\n previousPageUrl: this.getPreviousPageUrl(),\n };\n }\n\n /**\n * Returns JSON representation of the paginated\n * data\n */\n public toJSON() {\n return {\n meta: this.getMeta(),\n data: this.all(),\n };\n }\n\n /**\n * Define query string to be appended to the pagination links\n */\n public queryString(values: { [key: string]: any }): this {\n this.qs = values;\n\n return this;\n }\n\n /**\n * Define base url for making the pagination links\n */\n public baseUrl(url: string): this {\n this.url = url;\n\n return this;\n }\n\n /**\n * Returns url for a given page. Doesn't validate the integrity of the\n * page\n */\n public getUrl(page: number): string {\n const qstring = qs.stringify({ ...this.qs, page: page < 1 ? 1 : page });\n\n return `${this.url}?${qstring}`;\n }\n\n /**\n * Returns url for the next page\n */\n public getNextPageUrl(): string | null {\n if (this.hasMorePages) {\n return this.getUrl(this.currentPage + 1);\n }\n\n return null;\n }\n\n /**\n * Returns URL for the previous page\n */\n public getPreviousPageUrl(): string | null {\n if (this.currentPage > 1) {\n return this.getUrl(this.currentPage - 1);\n }\n\n return null;\n }\n\n /**\n * Returns an array of urls under a given range\n */\n public getUrlsForRange(start: number, end: number) {\n const urls: { url: string; page: number; isActive: boolean }[] = [];\n\n // eslint-disable-next-line no-plusplus\n for (let index = start; index <= end; index++) {\n urls.push({ url: this.getUrl(index), page: index, isActive: index === this.currentPage });\n }\n\n return urls;\n }\n}\n","import Paginator from \"./paginator\";\nimport type { Paginator as PaginatorInterface } from \"./types\";\n\nexport type { Paginator as PaginatorInterface, PaginationResult, PaginationMeta } from \"./types.d\";\nexport { default as Paginator } from \"./paginator\";\n\n// eslint-disable-next-line max-len\nexport const paginate = <Result>(page: number, perPage: number, total: number, rows: Result[]): PaginatorInterface<Result> => new Paginator(total, Number(perPage), Number(page), ...rows);\n"]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/paginator.ts","../src/index.ts"],"sourcesContent":["import qs from \"qs\";\n\nimport type { Paginator as IPaginator } from \"./types\";\n\n/**\n * Simple paginator works with the data set provided by the standard\n * `offset` and `limit` based pagination.\n */\nexport default class Paginator<T = any> extends Array implements IPaginator<T> {\n private qs: { [key: string]: any } = {};\n\n private url: string = \"/\";\n\n private readonly rows: any[];\n\n /**\n * The first page is always 1\n */\n public readonly firstPage: number = 1;\n\n /**\n * Find if results set is empty or not\n */\n public readonly isEmpty: boolean;\n\n constructor(private readonly totalNumber: number, public readonly perPage: number, public currentPage: number, ...rows: any[]) {\n super(...rows);\n\n this.totalNumber = Number(totalNumber);\n\n this.rows = rows;\n this.isEmpty = this.rows.length === 0;\n }\n\n /**\n * Find if there are total records or not. This is not same as\n * `isEmpty`.\n *\n * The `isEmpty` reports about the current set of results. However, `hasTotal`\n * reports about the total number of records, regardless of the current.\n */\n get hasTotal(): boolean {\n return this.total > 0;\n }\n\n /**\n * Find if there are more pages to come\n */\n get hasMorePages(): boolean {\n return this.lastPage > this.currentPage;\n }\n\n /**\n * Find if there are enough results to be paginated or not\n */\n get hasPages(): boolean {\n return this.lastPage !== 1;\n }\n\n /**\n * The Last page number\n */\n get lastPage(): number {\n return Math.max(Math.ceil(this.total / this.perPage), 1);\n }\n\n /**\n * Casting `total` to a number. Later, we can think of situations\n * to cast it to a bigint\n */\n get total(): number {\n return Number(this.totalNumber);\n }\n\n /**\n * A reference to the result rows\n */\n public all() {\n return this.rows;\n }\n\n /**\n * Returns JSON meta data\n */\n public getMeta() {\n return {\n total: this.total,\n perPage: this.perPage,\n page: this.currentPage,\n lastPage: this.lastPage,\n firstPage: this.firstPage,\n firstPageUrl: this.getUrl(1),\n lastPageUrl: this.getUrl(this.lastPage),\n nextPageUrl: this.getNextPageUrl(),\n previousPageUrl: this.getPreviousPageUrl(),\n };\n }\n\n /**\n * Returns JSON representation of the paginated\n * data\n */\n public toJSON() {\n return {\n meta: this.getMeta(),\n data: this.all(),\n };\n }\n\n /**\n * Define query string to be appended to the pagination links\n */\n public queryString(values: { [key: string]: any }): this {\n this.qs = values;\n\n return this;\n }\n\n /**\n * Define base url for making the pagination links\n */\n public baseUrl(url: string): this {\n this.url = url;\n\n return this;\n }\n\n /**\n * Returns url for a given page. Doesn't validate the integrity of the\n * page\n */\n public getUrl(page: number): string {\n const qstring = qs.stringify({ ...this.qs, page: page < 1 ? 1 : page });\n\n return `${this.url}?${qstring}`;\n }\n\n /**\n * Returns url for the next page\n */\n public getNextPageUrl(): string | null {\n if (this.hasMorePages) {\n return this.getUrl(this.currentPage + 1);\n }\n\n return null;\n }\n\n /**\n * Returns URL for the previous page\n */\n public getPreviousPageUrl(): string | null {\n if (this.currentPage > 1) {\n return this.getUrl(this.currentPage - 1);\n }\n\n return null;\n }\n\n /**\n * Returns an array of urls under a given range\n */\n public getUrlsForRange(start: number, end: number) {\n const urls: { url: string; page: number; isActive: boolean }[] = [];\n\n // eslint-disable-next-line no-plusplus\n for (let index = start; index <= end; index++) {\n urls.push({ url: this.getUrl(index), page: index, isActive: index === this.currentPage });\n }\n\n return urls;\n }\n}\n","import Paginator from \"./paginator\";\nimport type { Paginator as PaginatorInterface } from \"./types\";\n\nexport type { Paginator as PaginatorInterface } from \"./types.d\";\nexport { default as Paginator } from \"./paginator\";\n\n// eslint-disable-next-line max-len\nexport const paginate = <Result>(page: number, perPage: number, total: number, rows: Result[]):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;AAAA,EACJ;AAAA,EAKO,YAAY,QAAsC;AACrD,SAAK,KAAK;AAEV,WAAO;AAAA,EACX;AAAA,EAKO,QAAQ,KAAmB;AAC9B,SAAK,MAAM;AAEX,WAAO;AAAA,EACX;AAAA,EAMO,OAAO,MAAsB;AAChC,UAAM,UAAU,GAAG,UAAU,EAAE,GAAG,KAAK,IAAI,MAAM,OAAO,IAAI,IAAI,KAAK,CAAC;AAEtE,WAAO,GAAG,KAAK,OAAO;AAAA,EAC1B;AAAA,EAKO,iBAAgC;AACnC,QAAI,KAAK,cAAc;AACnB,aAAO,KAAK,OAAO,KAAK,cAAc,CAAC;AAAA,IAC3C;AAEA,WAAO;AAAA,EACX;AAAA,EAKO,qBAAoC;AACvC,QAAI,KAAK,cAAc,GAAG;AACtB,aAAO,KAAK,OAAO,KAAK,cAAc,CAAC;AAAA,IAC3C;AAEA,WAAO;AAAA,EACX;AAAA,EAKO,gBAAgB,OAAe,KAAa;AAC/C,UAAM,OAA2D,CAAC;AAGlE,aAAS,QAAQ,OAAO,SAAS,KAAK,SAAS;AAC3C,WAAK,KAAK,EAAE,KAAK,KAAK,OAAO,KAAK,GAAG,MAAM,OAAO,UAAU,UAAU,KAAK,YAAY,CAAC;AAAA,IAC5F;AAEA,WAAO;AAAA,EACX;AACJ;;;ACrKO,IAAM,WAAW,CAAS,MAAc,SAAiB,OAAe,SAA+C,IAAI,UAAU,OAAO,OAAO,OAAO,GAAG,OAAO,IAAI,GAAG,GAAG,IAAI;","names":[]}
1
+ {"version":3,"sources":["../src/paginator.ts","../src/index.ts"],"sourcesContent":["import qs from \"qs\";\n\nimport type { Paginator as IPaginator } from \"./types\";\n\n/**\n * Simple paginator works with the data set provided by the standard\n * `offset` and `limit` based pagination.\n */\nexport default class Paginator<T = any> extends Array implements IPaginator<T> {\n private qs: { [key: string]: any } = {};\n\n private url: string = \"/\";\n\n private readonly rows: any[];\n\n /**\n * The first page is always 1\n */\n public readonly firstPage: number = 1;\n\n /**\n * Find if results set is empty or not\n */\n public readonly isEmpty: boolean;\n\n constructor(private readonly totalNumber: number, public readonly perPage: number, public currentPage: number, ...rows: any[]) {\n super(...rows);\n\n this.totalNumber = Number(totalNumber);\n\n this.rows = rows;\n this.isEmpty = this.rows.length === 0;\n }\n\n /**\n * Find if there are total records or not. This is not same as\n * `isEmpty`.\n *\n * The `isEmpty` reports about the current set of results. However, `hasTotal`\n * reports about the total number of records, regardless of the current.\n */\n get hasTotal(): boolean {\n return this.total > 0;\n }\n\n /**\n * Find if there are more pages to come\n */\n get hasMorePages(): boolean {\n return this.lastPage > this.currentPage;\n }\n\n /**\n * Find if there are enough results to be paginated or not\n */\n get hasPages(): boolean {\n return this.lastPage !== 1;\n }\n\n /**\n * The Last page number\n */\n get lastPage(): number {\n return Math.max(Math.ceil(this.total / this.perPage), 1);\n }\n\n /**\n * Casting `total` to a number. Later, we can think of situations\n * to cast it to a bigint\n */\n get total(): number {\n return Number(this.totalNumber);\n }\n\n /**\n * A reference to the result rows\n */\n public all() {\n return this.rows;\n }\n\n /**\n * Returns JSON meta data\n */\n public getMeta() {\n return {\n total: this.total,\n perPage: this.perPage,\n page: this.currentPage,\n lastPage: this.lastPage,\n firstPage: this.firstPage,\n firstPageUrl: this.getUrl(1),\n lastPageUrl: this.getUrl(this.lastPage),\n nextPageUrl: this.getNextPageUrl(),\n previousPageUrl: this.getPreviousPageUrl(),\n };\n }\n\n /**\n * Returns JSON representation of the paginated\n * data\n */\n public toJSON() {\n return {\n meta: this.getMeta(),\n data: this.all(),\n };\n }\n\n /**\n * Define query string to be appended to the pagination links\n */\n public queryString(values: { [key: string]: any }): this {\n this.qs = values;\n\n return this;\n }\n\n /**\n * Define base url for making the pagination links\n */\n public baseUrl(url: string): this {\n this.url = url;\n\n return this;\n }\n\n /**\n * Returns url for a given page. Doesn't validate the integrity of the\n * page\n */\n public getUrl(page: number): string {\n const qstring = qs.stringify({ ...this.qs, page: page < 1 ? 1 : page });\n\n return `${this.url}?${qstring}`;\n }\n\n /**\n * Returns url for the next page\n */\n public getNextPageUrl(): string | null {\n if (this.hasMorePages) {\n return this.getUrl(this.currentPage + 1);\n }\n\n return null;\n }\n\n /**\n * Returns URL for the previous page\n */\n public getPreviousPageUrl(): string | null {\n if (this.currentPage > 1) {\n return this.getUrl(this.currentPage - 1);\n }\n\n return null;\n }\n\n /**\n * Returns an array of urls under a given range\n */\n public getUrlsForRange(start: number, end: number) {\n const urls: { url: string; page: number; isActive: boolean }[] = [];\n\n // eslint-disable-next-line no-plusplus\n for (let index = start; index <= end; index++) {\n urls.push({ url: this.getUrl(index), page: index, isActive: index === this.currentPage });\n }\n\n return urls;\n }\n}\n","import Paginator from \"./paginator\";\nimport type { Paginator as PaginatorInterface } from \"./types\";\n\nexport type { Paginator as PaginatorInterface, PaginationResult, PaginationMeta } from \"./types.d\";\nexport { default as Paginator } from \"./paginator\";\n\n// eslint-disable-next-line max-len\nexport const paginate = <Result>(page: number, perPage: number, total: number, rows: Result[]): PaginatorInterface<Result> => new Paginator(total, Number(perPage), Number(page), ...rows);\n"],"mappings":";AAAA,OAAO,QAAQ;AAQf,IAAqB,YAArB,cAAgD,MAA+B;AAAA,EAiB3E,YAA6B,aAAqC,SAAwB,gBAAwB,MAAa;AAC3H,UAAM,GAAG,IAAI;AADY;AAAqC;AAAwB;AAGtF,SAAK,cAAc,OAAO,WAAW;AAErC,SAAK,OAAO;AACZ,SAAK,UAAU,KAAK,KAAK,WAAW;AAAA,EACxC;AAAA,EAvBQ,KAA6B,CAAC;AAAA,EAE9B,MAAc;AAAA,EAEL;AAAA,EAKD,YAAoB;AAAA,EAKpB;AAAA,EAkBhB,IAAI,WAAoB;AACpB,WAAO,KAAK,QAAQ;AAAA,EACxB;AAAA,EAKA,IAAI,eAAwB;AACxB,WAAO,KAAK,WAAW,KAAK;AAAA,EAChC;AAAA,EAKA,IAAI,WAAoB;AACpB,WAAO,KAAK,aAAa;AAAA,EAC7B;AAAA,EAKA,IAAI,WAAmB;AACnB,WAAO,KAA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,QAAQ,OAAO,SAAS,KAAK,SAAS;AAC3C,WAAK,KAAK,EAAE,KAAK,KAAK,OAAO,KAAK,GAAG,MAAM,OAAO,UAAU,UAAU,KAAK,YAAY,CAAC;AAAA,IAC5F;AAEA,WAAO;AAAA,EACX;AACJ;;;ACrKO,IAAM,WAAW,CAAS,MAAc,SAAiB,OAAe,SAA+C,IAAI,UAAU,OAAO,OAAO,OAAO,GAAG,OAAO,IAAI,GAAG,GAAG,IAAI;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@visulima/pagination",
3
- "version": "1.0.1",
4
- "description": "Pagination for node.",
3
+ "version": "1.0.3",
4
+ "description": "Simple Pagination for Node.",
5
5
  "keywords": [
6
6
  "anolilab",
7
7
  "pagination",
@@ -39,7 +39,7 @@
39
39
  "./package.json": "./package.json"
40
40
  },
41
41
  "main": "dist/index.js",
42
- "module": "dist/index.module.mjs",
42
+ "module": "dist/index.mjs",
43
43
  "source": "src/index.ts",
44
44
  "types": "dist/index.d.ts",
45
45
  "files": [