@visulima/pagination 2.0.2 → 2.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.
- package/CHANGELOG.md +16 -0
- package/README.md +3 -5
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +27 -27
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
## @visulima/pagination [2.0.4](https://github.com/visulima/visulima/compare/@visulima/pagination@2.0.3...@visulima/pagination@2.0.4) (2023-06-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* Add npm package provenance, see https://docs.npmjs.com/generating-provenance-statements for more info ([9e7da04](https://github.com/visulima/visulima/commit/9e7da0491584e16a806fc7575c00080f192ec15e))
|
|
7
|
+
|
|
8
|
+
## @visulima/pagination [2.0.3](https://github.com/visulima/visulima/compare/@visulima/pagination@2.0.2...@visulima/pagination@2.0.3) (2023-05-25)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* chore updated the deps and changed the engine to allow node v20 ([c479f37](https://github.com/visulima/visulima/commit/c479f373550886c6c79c7d525a3aead82daf078b))
|
|
14
|
+
* fixed all calls to radar with sonarjs, removed radar eslint package ([001aecf](https://github.com/visulima/visulima/commit/001aecf78dde134bade44f382698d52eedbd3bbe))
|
|
15
|
+
* updated package deps ([5f605aa](https://github.com/visulima/visulima/commit/5f605aab74a7c1f4cbdfe4502363e36d57716921))
|
|
16
|
+
|
|
1
17
|
## @visulima/pagination [2.0.2](https://github.com/visulima/visulima/compare/@visulima/pagination@2.0.1...@visulima/pagination@2.0.2) (2023-01-25)
|
|
2
18
|
|
|
3
19
|
|
package/README.md
CHANGED
|
@@ -53,7 +53,7 @@ const pagination = paginate(1, 5, items.length, items);
|
|
|
53
53
|
|
|
54
54
|
console.log(pagination);
|
|
55
55
|
// {
|
|
56
|
-
//
|
|
56
|
+
// data: [1, 2, 3, 4, 5],
|
|
57
57
|
// meta: {
|
|
58
58
|
// total: 10,
|
|
59
59
|
// perPage: 5,
|
|
@@ -66,7 +66,6 @@ console.log(pagination);
|
|
|
66
66
|
// previousPageUrl: null,
|
|
67
67
|
// }
|
|
68
68
|
// }
|
|
69
|
-
|
|
70
69
|
```
|
|
71
70
|
|
|
72
71
|
## Supported Node.js Versions
|
|
@@ -94,6 +93,5 @@ The visulima pagination is open-sourced software licensed under the [MIT][licens
|
|
|
94
93
|
[typescript-url]: "typescript"
|
|
95
94
|
[license-image]: https://img.shields.io/npm/l/@visulima/pagination?color=blueviolet&style=for-the-badge
|
|
96
95
|
[license-url]: LICENSE.md "license"
|
|
97
|
-
[npm-image]: https://img.shields.io/npm/v/@visulima/pagination/
|
|
98
|
-
[npm-url]: https://www.npmjs.com/package/@visulima/pagination/v/
|
|
99
|
-
|
|
96
|
+
[npm-image]: https://img.shields.io/npm/v/@visulima/pagination/latest.svg?style=for-the-badge&logo=npm
|
|
97
|
+
[npm-url]: https://www.npmjs.com/package/@visulima/pagination/v/latest "npm"
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
var o = require('qs');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var o__default = /*#__PURE__*/_interopDefault(o);
|
|
8
|
+
|
|
9
|
+
var i=class extends Array{constructor(e,a,s,...r){super(...r);this.totalNumber=e;this.perPage=a;this.currentPage=s;this.qs={};this.url="/";this.firstPage=1;this.totalNumber=Number(e),this.rows=r,this.isEmpty=this.rows.length===0;}get hasTotal(){return this.total>0}get hasMorePages(){return this.lastPage>this.currentPage}get hasPages(){return this.lastPage!==1}get lastPage(){return Math.max(Math.ceil(this.total/this.perPage),1)}get total(){return Number(this.totalNumber)}all(){return this.rows}getMeta(){return {total:this.total,perPage:this.perPage,page:this.currentPage,lastPage:this.lastPage,firstPage:this.firstPage,firstPageUrl:this.getUrl(1),lastPageUrl:this.getUrl(this.lastPage),nextPageUrl:this.getNextPageUrl(),previousPageUrl:this.getPreviousPageUrl()}}toJSON(){return {meta:this.getMeta(),data:this.all()}}queryString(e){return this.qs=e,this}baseUrl(e){return this.url=e,this}getUrl(e){let a=o__default.default.stringify({...this.qs,page:e<1?1:e});return `${this.url}?${a}`}getNextPageUrl(){return this.hasMorePages?this.getUrl(this.currentPage+1):null}getPreviousPageUrl(){return this.currentPage>1?this.getUrl(this.currentPage-1):null}getUrlsForRange(e,a){let s=[];for(let r=e;r<=a;r++)s.push({url:this.getUrl(r),page:r,isActive:r===this.currentPage});return s}};var g=(t="PaginationData")=>({[t]:{type:"object",xml:{name:t},properties:{total:{type:"integer",minimum:0,description:"Holds the value for the total number of rows in the database"},perPage:{type:"integer",minimum:0,description:"Returns the value for the limit passed to the paginate method"},page:{type:"integer",minimum:1,description:"Current page number"},lastPage:{type:"integer",minimum:0,description:"Returns the value for the last page by taking the total of rows into account"},firstPage:{type:"integer",minimum:0,description:"Returns the number for the first page. It is always 1"},firstPageUrl:{type:"string",description:"The URL for the first page"},lastPageUrl:{type:"string",description:"The URL for the last page"},nextPageUrl:{type:"string",description:"The URL for the next page"},previousPageUrl:{type:"string",description:"The URL for the previous page"}}}}),l=(t,n,e="#/components/schemas/PaginationData")=>({[t]:{type:"object",xml:{name:t},properties:{data:{type:"array",xml:{name:"data",wrapped:!0},items:n},meta:{$ref:e}}}});var m=(t,n,e,a)=>new i(e,Number(n),Number(t),...a);
|
|
6
10
|
|
|
7
11
|
exports.Paginator = i;
|
|
8
12
|
exports.createPaginationMetaSchemaObject = g;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/paginator.ts","../src/swagger.ts","../src/index.ts"],"names":["qs","Paginator","totalNumber","perPage","currentPage","rows","values","url","page","qstring","start","end","urls","index","createPaginationMetaSchemaObject","name","createPaginationSchemaObject","items","metaReference","paginate","total"],"mappings":"AAAA,OAAOA,MAAQ,KAUf,IAAqBC,EAArB,cAAgD,KAA+B,CAiBpE,YAA6BC,EAAqCC,EAAwBC,KAAwBC,EAAa,CAClI,MAAM,GAAGA,CAAI,EADmB,iBAAAH,EAAqC,aAAAC,EAAwB,iBAAAC,EAhBjG,KAAQ,GAA6B,CAAC,EAEtC,KAAQ,IAAc,IAOtB,KAAgB,UAAoB,EAUhC,KAAK,YAAc,OAAOF,CAAW,EAErC,KAAK,KAAOG,EACZ,KAAK,QAAU,KAAK,KAAK,SAAW,CACxC,CASA,IAAW,UAAoB,CAC3B,OAAO,KAAK,MAAQ,CACxB,CAKA,IAAW,cAAwB,CAC/B,OAAO,KAAK,SAAW,KAAK,WAChC,CAKA,IAAW,UAAoB,CAC3B,OAAO,KAAK,WAAa,CAC7B,CAKA,IAAW,UAAmB,CAC1B,OAAO,KAAK,IAAI,KAAK,KAAK,KAAK,MAAQ,KAAK,OAAO,EAAG,CAAC,CAC3D,CAMA,IAAW,OAAgB,CACvB,OAAO,OAAO,KAAK,WAAW,CAClC,CAKO,KAAW,CACd,OAAO,KAAK,IAChB,CAKO,SAA0B,CAC7B,MAAO,CACH,MAAO,KAAK,MACZ,QAAS,KAAK,QACd,KAAM,KAAK,YACX,SAAU,KAAK,SACf,UAAW,KAAK,UAChB,aAAc,KAAK,OAAO,CAAC,EAC3B,YAAa,KAAK,OAAO,KAAK,QAAQ,EACtC,YAAa,KAAK,eAAe,EACjC,gBAAiB,KAAK,mBAAmB,CAC7C,CACJ,CAMO,QAA8B,CACjC,MAAO,CACH,KAAM,KAAK,QAAQ,EACnB,KAAM,KAAK,IAAI,CACnB,CACJ,CAKO,YAAYC,EAAsC,CACrD,YAAK,GAAKA,EAEH,IACX,CAKO,QAAQC,EAAmB,CAC9B,YAAK,IAAMA,EAEJ,IACX,CAMO,OAAOC,EAAsB,CAChC,IAAMC,EAAUT,E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E,CACJ,EACA,KAAM,CACF,KAAMC,CACV,CACJ,CACJ,CACJ,GC1EG,IAAMC,EAAW,CAASX,EAAcL,EAAiBiB,EAAef,IAA+C,IAAIJ,EAAUmB,EAAO,OAAOjB,CAAO,EAAG,OAAOK,CAAI,EAAG,GAAGH,CAAI","sourcesContent":["import qs from \"qs\";\n\nimport type { PaginationMeta, PaginationResult, Paginator as IPaginator } from \"./types\";\n\ntype UrlsForRange = {url: string; page: number; isActive: boolean}[];\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: T[];\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 public 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 public get hasTotal(): boolean {\n return this.total > 0;\n }\n\n /**\n * Find if there are more pages to come\n */\n public 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 public get hasPages(): boolean {\n return this.lastPage !== 1;\n }\n\n /**\n * The Last page number\n */\n public 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 public get total(): number {\n return Number(this.totalNumber);\n }\n\n /**\n * A reference to the result rows\n */\n public all(): T[] {\n return this.rows;\n }\n\n /**\n * Returns JSON meta data\n */\n public getMeta(): PaginationMeta {\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(): PaginationResult<T> {\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): UrlsForRange {\n const urls: UrlsForRange = [];\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 type { OpenAPIV3 } from \"openapi-types\";\n\nexport const createPaginationMetaSchemaObject = (name: string = \"PaginationData\"): { [key: string]: OpenAPIV3.SchemaObject } => {\n return {\n [name]: {\n type: \"object\",\n xml: {\n name,\n },\n properties: {\n total: {\n type: \"integer\",\n minimum: 0,\n description: \"Holds the value for the total number of rows in the database\",\n },\n perPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the value for the limit passed to the paginate method\",\n },\n page: {\n type: \"integer\",\n minimum: 1,\n description: \"Current page number\",\n },\n lastPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the value for the last page by taking the total of rows into account\",\n },\n firstPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the number for the first page. It is always 1\",\n },\n firstPageUrl: {\n type: \"string\",\n description: \"The URL for the first page\",\n },\n lastPageUrl: {\n type: \"string\",\n description: \"The URL for the last page\",\n },\n nextPageUrl: {\n type: \"string\",\n description: \"The URL for the next page\",\n },\n previousPageUrl: {\n type: \"string\",\n description: \"The URL for the previous page\",\n },\n },\n },\n };\n};\n\nexport const createPaginationSchemaObject = (\n name: string,\n items: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject,\n metaReference: string = \"#/components/schemas/PaginationData\",\n): { [key: string]: OpenAPIV3.SchemaObject } => {\n return {\n [name]: {\n type: \"object\",\n xml: {\n name,\n },\n properties: {\n data: {\n type: \"array\",\n xml: {\n name: \"data\",\n wrapped: true,\n },\n items,\n },\n meta: {\n $ref: metaReference,\n },\n },\n },\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\nexport { createPaginationSchemaObject, createPaginationMetaSchemaObject } from \"./swagger\";\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/paginator.ts","../src/swagger.ts","../src/index.ts"],"names":["qs","Paginator","totalNumber","perPage","currentPage","rows","values","url","page","qstring","start","end","urls","index","createPaginationMetaSchemaObject","name","createPaginationSchemaObject","items","metaReference","paginate","total"],"mappings":"AAAA,OAAOA,MAAQ,KAUf,IAAqBC,EAArB,cAAgD,KAA+B,CAiBpE,YAA6BC,EAAqCC,EAAwBC,KAAwBC,EAAa,CAClI,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AI,EAAG,GAAGH,CAAI","sourcesContent":["import qs from \"qs\";\n\nimport type { PaginationMeta, PaginationResult, Paginator as IPaginator } from \"./types\";\n\ntype UrlsForRange = { url: string; page: number; isActive: boolean }[];\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: T[];\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 public 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 public get hasTotal(): boolean {\n return this.total > 0;\n }\n\n /**\n * Find if there are more pages to come\n */\n public 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 public get hasPages(): boolean {\n return this.lastPage !== 1;\n }\n\n /**\n * The Last page number\n */\n public 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 public get total(): number {\n return Number(this.totalNumber);\n }\n\n /**\n * A reference to the result rows\n */\n public all(): T[] {\n return this.rows;\n }\n\n /**\n * Returns JSON meta data\n */\n public getMeta(): PaginationMeta {\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(): PaginationResult<T> {\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): UrlsForRange {\n const urls: UrlsForRange = [];\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 type { OpenAPIV3 } from \"openapi-types\";\n\nexport const createPaginationMetaSchemaObject = (name: string = \"PaginationData\"): { [key: string]: OpenAPIV3.SchemaObject } => {\n return {\n [name]: {\n type: \"object\",\n xml: {\n name,\n },\n properties: {\n total: {\n type: \"integer\",\n minimum: 0,\n description: \"Holds the value for the total number of rows in the database\",\n },\n perPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the value for the limit passed to the paginate method\",\n },\n page: {\n type: \"integer\",\n minimum: 1,\n description: \"Current page number\",\n },\n lastPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the value for the last page by taking the total of rows into account\",\n },\n firstPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the number for the first page. It is always 1\",\n },\n firstPageUrl: {\n type: \"string\",\n description: \"The URL for the first page\",\n },\n lastPageUrl: {\n type: \"string\",\n description: \"The URL for the last page\",\n },\n nextPageUrl: {\n type: \"string\",\n description: \"The URL for the next page\",\n },\n previousPageUrl: {\n type: \"string\",\n description: \"The URL for the previous page\",\n },\n },\n },\n };\n};\n\nexport const createPaginationSchemaObject = (\n name: string,\n items: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject,\n metaReference: string = \"#/components/schemas/PaginationData\",\n): { [key: string]: OpenAPIV3.SchemaObject } => {\n return {\n [name]: {\n type: \"object\",\n xml: {\n name,\n },\n properties: {\n data: {\n type: \"array\",\n xml: {\n name: \"data\",\n wrapped: true,\n },\n items,\n },\n meta: {\n $ref: metaReference,\n },\n },\n },\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\nexport { createPaginationSchemaObject, createPaginationMetaSchemaObject } from \"./swagger\";\n"]}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/paginator.ts","../src/swagger.ts","../src/index.ts"],"names":["qs","Paginator","totalNumber","perPage","currentPage","rows","values","url","page","qstring","start","end","urls","index","createPaginationMetaSchemaObject","name","createPaginationSchemaObject","items","metaReference","paginate","total"],"mappings":"AAAA,OAAOA,MAAQ,KAUf,IAAqBC,EAArB,cAAgD,KAA+B,CAiBpE,YAA6BC,EAAqCC,EAAwBC,KAAwBC,EAAa,CAClI,MAAM,GAAGA,CAAI,EADmB,iBAAAH,EAAqC,aAAAC,EAAwB,iBAAAC,EAhBjG,KAAQ,GAA6B,CAAC,EAEtC,KAAQ,IAAc,IAOtB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,EAAeC,EAA2B,CAC7D,IAAMC,EAAqB,CAAC,EAG5B,QAASC,EAAQH,EAAOG,GAASF,EAAKE,IAClCD,EAAK,KAAK,CAAE,IAAK,KAAK,OAAOC,CAAK,EAAG,KAAMA,EAAO,SAAUA,IAAU,KAAK,WAAY,CAAC,EAG5F,OAAOD,CACX,CACJ,EC5KO,IAAME,EAAmC,CAACC,EAAe,oBACrD,CACH,CAACA,GAAO,CACJ,KAAM,SACN,IAAK,CACD,KAAAA,CACJ,EACA,WAAY,CACR,MAAO,CACH,KAAM,UACN,QAAS,EACT,YAAa,8DACjB,EACA,QAAS,CACL,KAAM,UACN,QAAS,EACT,YAAa,+DACjB,EACA,KAAM,CACF,KAAM,UACN,QAAS,EACT,YAAa,qBACjB,EACA,SAAU,CACN,KAAM,UACN,QAAS,EACT,YAAa,8EACjB,EACA,UAAW,CACP,KAAM,UACN,QAAS,EACT,YAAa,uDACjB,EACA,aAAc,CACV,KAAM,SACN,YAAa,4BACjB,EACA,YAAa,CACT,KAAM,SACN,YAAa,2BACjB,EACA,YAAa,CACT,KAAM,SACN,YAAa,2BACjB,EACA,gBAAiB,CACb,KAAM,SACN,YAAa,+BACjB,CACJ,CACJ,CACJ,GAGSC,EAA+B,CACxCD,EACAE,EACAC,EAAwB,yCAEjB,CACH,CAACH,GAAO,CACJ,KAAM,SACN,IAAK,CACD,KAAAA,CACJ,EACA,WAAY,CACR,KAAM,CACF,KAAM,QACN,IAAK,CACD,KAAM,OACN,QAAS,EACb,EACA,MAAAE,CACJ,EACA,KAAM,CACF,KAAMC,CACV,CACJ,CACJ,CACJ,GC1EG,IAAMC,EAAW,CAASX,EAAcL,EAAiBiB,EAAef,IAA+C,IAAIJ,EAAUmB,EAAO,OAAOjB,CAAO,EAAG,OAAOK,CAAI,EAAG,GAAGH,CAAI","sourcesContent":["import qs from \"qs\";\n\nimport type { PaginationMeta, PaginationResult, Paginator as IPaginator } from \"./types\";\n\ntype UrlsForRange = {url: string; page: number; isActive: boolean}[];\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: T[];\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 public 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 public get hasTotal(): boolean {\n return this.total > 0;\n }\n\n /**\n * Find if there are more pages to come\n */\n public 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 public get hasPages(): boolean {\n return this.lastPage !== 1;\n }\n\n /**\n * The Last page number\n */\n public 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 public get total(): number {\n return Number(this.totalNumber);\n }\n\n /**\n * A reference to the result rows\n */\n public all(): T[] {\n return this.rows;\n }\n\n /**\n * Returns JSON meta data\n */\n public getMeta(): PaginationMeta {\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(): PaginationResult<T> {\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): UrlsForRange {\n const urls: UrlsForRange = [];\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 type { OpenAPIV3 } from \"openapi-types\";\n\nexport const createPaginationMetaSchemaObject = (name: string = \"PaginationData\"): { [key: string]: OpenAPIV3.SchemaObject } => {\n return {\n [name]: {\n type: \"object\",\n xml: {\n name,\n },\n properties: {\n total: {\n type: \"integer\",\n minimum: 0,\n description: \"Holds the value for the total number of rows in the database\",\n },\n perPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the value for the limit passed to the paginate method\",\n },\n page: {\n type: \"integer\",\n minimum: 1,\n description: \"Current page number\",\n },\n lastPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the value for the last page by taking the total of rows into account\",\n },\n firstPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the number for the first page. It is always 1\",\n },\n firstPageUrl: {\n type: \"string\",\n description: \"The URL for the first page\",\n },\n lastPageUrl: {\n type: \"string\",\n description: \"The URL for the last page\",\n },\n nextPageUrl: {\n type: \"string\",\n description: \"The URL for the next page\",\n },\n previousPageUrl: {\n type: \"string\",\n description: \"The URL for the previous page\",\n },\n },\n },\n };\n};\n\nexport const createPaginationSchemaObject = (\n name: string,\n items: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject,\n metaReference: string = \"#/components/schemas/PaginationData\",\n): { [key: string]: OpenAPIV3.SchemaObject } => {\n return {\n [name]: {\n type: \"object\",\n xml: {\n name,\n },\n properties: {\n data: {\n type: \"array\",\n xml: {\n name: \"data\",\n wrapped: true,\n },\n items,\n },\n meta: {\n $ref: metaReference,\n },\n },\n },\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\nexport { createPaginationSchemaObject, createPaginationMetaSchemaObject } from \"./swagger\";\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/paginator.ts","../src/swagger.ts","../src/index.ts"],"names":["qs","Paginator","totalNumber","perPage","currentPage","rows","values","url","page","qstring","start","end","urls","index","createPaginationMetaSchemaObject","name","createPaginationSchemaObject","items","metaReference","paginate","total"],"mappings":"AAAA,OAAOA,MAAQ,KAUf,IAAqBC,EAArB,cAAgD,KAA+B,CAiBpE,YAA6BC,EAAqCC,EAAwBC,KAAwBC,EAAa,CAClI,MAAM,GAAGA,CAAI,EADmB,iBAAAH,EAAqC,aAAAC,EAAwB,iBAAAC,EAhBjG,KAAQ,GAA6B,CAAC,EAEtC,KAAQ,IAAc,IAOtB,KAAgB,UAAoB,EAUhC,KAAK,YAAc,OAAOF,CAAW,EAErC,KAAK,KAAOG,EACZ,KAAK,QAAU,KAAK,KAAK,SAAW,CACxC,CASA,IAAW,UAAoB,CAC3B,OAAO,KAAK,MAAQ,CACxB,CAKA,IAAW,cAA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AY,CAAC,EAG5F,OAAOD,CACX,CACJ,EC5KO,IAAME,EAAmC,CAACC,EAAe,oBACrD,CACH,CAACA,CAAI,EAAG,CACJ,KAAM,SACN,IAAK,CACD,KAAAA,CACJ,EACA,WAAY,CACR,MAAO,CACH,KAAM,UACN,QAAS,EACT,YAAa,8DACjB,EACA,QAAS,CACL,KAAM,UACN,QAAS,EACT,YAAa,+DACjB,EACA,KAAM,CACF,KAAM,UACN,QAAS,EACT,YAAa,qBACjB,EACA,SAAU,CACN,KAAM,UACN,QAAS,EACT,YAAa,8EACjB,EACA,UAAW,CACP,KAAM,UACN,QAAS,EACT,YAAa,uDACjB,EACA,aAAc,CACV,KAAM,SACN,YAAa,4BACjB,EACA,YAAa,CACT,KAAM,SACN,YAAa,2BACjB,EACA,YAAa,CACT,KAAM,SACN,YAAa,2BACjB,EACA,gBAAiB,CACb,KAAM,SACN,YAAa,+BACjB,CACJ,CACJ,CACJ,GAGSC,EAA+B,CACxCD,EACAE,EACAC,EAAwB,yCAEjB,CACH,CAACH,CAAI,EAAG,CACJ,KAAM,SACN,IAAK,CACD,KAAAA,CACJ,EACA,WAAY,CACR,KAAM,CACF,KAAM,QACN,IAAK,CACD,KAAM,OACN,QAAS,EACb,EACA,MAAAE,CACJ,EACA,KAAM,CACF,KAAMC,CACV,CACJ,CACJ,CACJ,GC1EG,IAAMC,EAAW,CAASX,EAAcL,EAAiBiB,EAAef,IAA+C,IAAIJ,EAAUmB,EAAO,OAAOjB,CAAO,EAAG,OAAOK,CAAI,EAAG,GAAGH,CAAI","sourcesContent":["import qs from \"qs\";\n\nimport type { PaginationMeta, PaginationResult, Paginator as IPaginator } from \"./types\";\n\ntype UrlsForRange = { url: string; page: number; isActive: boolean }[];\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: T[];\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 public 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 public get hasTotal(): boolean {\n return this.total > 0;\n }\n\n /**\n * Find if there are more pages to come\n */\n public 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 public get hasPages(): boolean {\n return this.lastPage !== 1;\n }\n\n /**\n * The Last page number\n */\n public 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 public get total(): number {\n return Number(this.totalNumber);\n }\n\n /**\n * A reference to the result rows\n */\n public all(): T[] {\n return this.rows;\n }\n\n /**\n * Returns JSON meta data\n */\n public getMeta(): PaginationMeta {\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(): PaginationResult<T> {\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): UrlsForRange {\n const urls: UrlsForRange = [];\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 type { OpenAPIV3 } from \"openapi-types\";\n\nexport const createPaginationMetaSchemaObject = (name: string = \"PaginationData\"): { [key: string]: OpenAPIV3.SchemaObject } => {\n return {\n [name]: {\n type: \"object\",\n xml: {\n name,\n },\n properties: {\n total: {\n type: \"integer\",\n minimum: 0,\n description: \"Holds the value for the total number of rows in the database\",\n },\n perPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the value for the limit passed to the paginate method\",\n },\n page: {\n type: \"integer\",\n minimum: 1,\n description: \"Current page number\",\n },\n lastPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the value for the last page by taking the total of rows into account\",\n },\n firstPage: {\n type: \"integer\",\n minimum: 0,\n description: \"Returns the number for the first page. It is always 1\",\n },\n firstPageUrl: {\n type: \"string\",\n description: \"The URL for the first page\",\n },\n lastPageUrl: {\n type: \"string\",\n description: \"The URL for the last page\",\n },\n nextPageUrl: {\n type: \"string\",\n description: \"The URL for the next page\",\n },\n previousPageUrl: {\n type: \"string\",\n description: \"The URL for the previous page\",\n },\n },\n },\n };\n};\n\nexport const createPaginationSchemaObject = (\n name: string,\n items: OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject,\n metaReference: string = \"#/components/schemas/PaginationData\",\n): { [key: string]: OpenAPIV3.SchemaObject } => {\n return {\n [name]: {\n type: \"object\",\n xml: {\n name,\n },\n properties: {\n data: {\n type: \"array\",\n xml: {\n name: \"data\",\n wrapped: true,\n },\n items,\n },\n meta: {\n $ref: metaReference,\n },\n },\n },\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\nexport { createPaginationSchemaObject, createPaginationMetaSchemaObject } from \"./swagger\";\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@visulima/pagination",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "Simple Pagination for Node.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"anolilab",
|
|
@@ -53,31 +53,32 @@
|
|
|
53
53
|
"build:prod": "cross-env NODE_ENV=production tsup",
|
|
54
54
|
"clean": "rimraf node_modules dist .eslintcache",
|
|
55
55
|
"coverage": "vitest run --coverage",
|
|
56
|
-
"dev": "pnpm
|
|
56
|
+
"dev": "pnpm run build --watch",
|
|
57
57
|
"lint:eslint": "cross-env NO_LOGS=true eslint . --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.cjs --cache --cache-strategy content .",
|
|
58
58
|
"lint:eslint:fix": "pnpm run lint:eslint --fix",
|
|
59
59
|
"test": "vitest run",
|
|
60
60
|
"test:watch": "vitest"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"qs": "^6.11.
|
|
63
|
+
"qs": "^6.11.2"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@anolilab/eslint-config": "^5.0.
|
|
67
|
-
"@anolilab/semantic-release-preset": "^2.2.
|
|
68
|
-
"@rushstack/eslint-plugin-security": "^0.
|
|
66
|
+
"@anolilab/eslint-config": "^5.0.5",
|
|
67
|
+
"@anolilab/semantic-release-preset": "^2.2.1",
|
|
68
|
+
"@rushstack/eslint-plugin-security": "^0.6.0",
|
|
69
69
|
"@types/micromatch": "^4.0.2",
|
|
70
|
-
"@types/node": "
|
|
70
|
+
"@types/node": "18.16.16",
|
|
71
71
|
"@types/qs": "^6.9.7",
|
|
72
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
73
|
-
"@typescript-eslint/parser": "^5.
|
|
72
|
+
"@typescript-eslint/eslint-plugin": "^5.59.8",
|
|
73
|
+
"@typescript-eslint/parser": "^5.59.8",
|
|
74
|
+
"@vitest/coverage-c8": "^0.31.1",
|
|
74
75
|
"cross-env": "^7.0.3",
|
|
75
|
-
"eslint": "^8.
|
|
76
|
-
"eslint-plugin-compat": "^4.
|
|
76
|
+
"eslint": "^8.41.0",
|
|
77
|
+
"eslint-plugin-compat": "^4.1.4",
|
|
77
78
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
78
79
|
"eslint-plugin-import": "^2.27.5",
|
|
79
80
|
"eslint-plugin-json": "^3.1.0",
|
|
80
|
-
"eslint-plugin-jsonc": "^2.
|
|
81
|
+
"eslint-plugin-jsonc": "^2.8.0",
|
|
81
82
|
"eslint-plugin-jsx-a11y": "^6.7.1",
|
|
82
83
|
"eslint-plugin-markdown": "^3.0.0",
|
|
83
84
|
"eslint-plugin-material-ui": "^1.0.1",
|
|
@@ -86,28 +87,27 @@
|
|
|
86
87
|
"eslint-plugin-node": "^11.1.0",
|
|
87
88
|
"eslint-plugin-optimize-regex": "^1.2.1",
|
|
88
89
|
"eslint-plugin-promise": "^6.1.1",
|
|
89
|
-
"eslint-plugin-
|
|
90
|
-
"eslint-plugin-react": "7.32.1",
|
|
90
|
+
"eslint-plugin-react": "7.32.2",
|
|
91
91
|
"eslint-plugin-react-hooks": "4.6.0",
|
|
92
|
-
"eslint-plugin-simple-import-sort": "^
|
|
92
|
+
"eslint-plugin-simple-import-sort": "^10.0.0",
|
|
93
93
|
"eslint-plugin-sort-keys-fix": "^1.1.2",
|
|
94
|
-
"eslint-plugin-testing-library": "^5.
|
|
95
|
-
"eslint-plugin-unicorn": "^
|
|
94
|
+
"eslint-plugin-testing-library": "^5.11.0",
|
|
95
|
+
"eslint-plugin-unicorn": "^47.0.0",
|
|
96
96
|
"eslint-plugin-you-dont-need-lodash-underscore": "^6.12.0",
|
|
97
97
|
"eslint-plugin-you-dont-need-momentjs": "^1.6.0",
|
|
98
|
-
"openapi-types": "^12.1.
|
|
99
|
-
"prettier": "^2.8.
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
"
|
|
105
|
-
"vitest": "^0.28.2"
|
|
98
|
+
"openapi-types": "^12.1.3",
|
|
99
|
+
"prettier": "^2.8.8",
|
|
100
|
+
"rimraf": "^5.0.1",
|
|
101
|
+
"semantic-release": "^21.0.2",
|
|
102
|
+
"tsup": "^6.7.0",
|
|
103
|
+
"typescript": "^5.0.4",
|
|
104
|
+
"vitest": "^0.31.1"
|
|
106
105
|
},
|
|
107
106
|
"engines": {
|
|
108
|
-
"node": ">=16.18.0 <=
|
|
107
|
+
"node": ">=16.18.0 <=20.*"
|
|
109
108
|
},
|
|
110
109
|
"publishConfig": {
|
|
111
|
-
"access": "public"
|
|
110
|
+
"access": "public",
|
|
111
|
+
"provenance": true
|
|
112
112
|
}
|
|
113
113
|
}
|