Last active
January 23, 2020 18:14
-
-
Save volkovasystems/d478f9140e5f33c611ad505c12a76a2a to your computer and use it in GitHub Desktop.
resolve-page-data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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