Skip to content

Instantly share code, notes, and snippets.

@volkovasystems
Last active January 23, 2020 18:14
Show Gist options
  • Save volkovasystems/d478f9140e5f33c611ad505c12a76a2a to your computer and use it in GitHub Desktop.
Save volkovasystems/d478f9140e5f33c611ad505c12a76a2a to your computer and use it in GitHub Desktop.
resolve-page-data
"use strict";
const falze = require( "falze" );
const falzy = require( "falzy" );
const resolvePageData = function resolvePageData( pageData ){
/*;
@note:
pageData if string denotes pageIndex
@end-note
*/
if(
typeof pageData == "string"
&& pageData.length > 0
&& ( /^\d+$/ ).test( pageData )
){
pageData = parseInt( pageData );
}
/*;
@note:
pageData if number denotes pageIndex
@end-note
*/
if(
typeof pageData == "number"
){
const pageIndex = (
pageData
|| 1
);
pageData = {
"pageIndex": pageIndex,
"listCount": 0,
"pageSize": 0,
"pageCount": 0,
"lastPageSize": 0,
"startIndex": 0,
"endIndex": 0,
"currentPageSize": 0
};
}
if(
falze( pageData )
){
pageData = (
{
"pageIndex": 1,
"listCount": 0,
"pageSize": 0,
"pageCount": 0,
"lastPageSize": 0,
"startIndex": 0,
"endIndex": 0,
"currentPageSize": 0
}
);
}
const pageDataPropertyList = Object.keys( pageData );
const pageDataPropertyListLength = pageDataPropertyList.length;
let pageDataProperty = undefined;
for(
let pageDataPropertyIndex = 0;
pageDataPropertyIndex < pageDataPropertyListLength;
pageDataPropertyIndex++
){
pageDataProperty = pageDataPropertyList[ pageDataPropertyIndex ];
if(
typeof pageData[ pageDataProperty ] != "number"
&& typeof pageData[ pageDataProperty ] == "string"
&& ( /^\d+$/ ).test( pageData[ pageDataProperty ] )
){
pageData[ pageDataProperty ] = parseInt( pageData[ pageDataProperty ] );
}
if(
isNaN( pageData[ pageDataProperty ] ) === true
){
const pageDataPropertyValueError = (
new Error( "invalid page data property value" )
);
throw pageDataPropertyValueError;
}
}
/*;
@note:
pageIndex should always default to 1
and must not be less than 1
@end-note
*/
if(
falzy( pageData.pageIndex )
|| pageData.pageIndex <= 0
){
pageData.pageIndex = 1;
}
if(
falzy( pageData.listCount )
|| pageData.listCount < 0
){
pageData.listCount = 0;
}
if(
falzy( pageData.pageSize )
|| pageData.pageSize < 0
){
pageData.pageSize = 0;
}
if(
falzy( pageData.pageCount )
|| pageData.pageCount < 0
){
pageData.pageCount = 0;
}
if(
falzy( pageData.lastPageSize )
|| pageData.lastPageSize < 0
){
pageData.lastPageSize = 0;
}
if(
falzy( pageData.startIndex )
|| pageData.startIndex < 0
){
pageData.startIndex = 0;
}
if(
falzy( pageData.endIndex )
|| pageData.endIndex < 0
){
pageData.endIndex = 0;
}
pageData.currentPageSize = (
pageData.endIndex
- pageData.startIndex
);
return pageData;
};
module.exports = resolvePageData;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment