UNPKG

@coding-blocks/jsonapi-server

Version:

A config driven NodeJS framework implementing json:api

171 lines (170 loc) 6.38 kB
<!doctype html> <html lang="en"> <head> <title>Code coverage report for lib/postProcessing/sort.js</title> <meta charset="utf-8" /> <link rel="stylesheet" href="../../prettify.css" /> <link rel="stylesheet" href="../../base.css" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type='text/css'> .coverage-summary .sorter { background-image: url(../../sort-arrow-sprite.png); } </style> </head> <body> <div class='wrapper'> <div class='pad1'> <h1> <a href="../../index.html">All files</a> / <a href="index.html">lib/postProcessing</a> sort.js </h1> <div class='clearfix'> <div class='fl pad1y space-right2'> <span class="strong">10.53% </span> <span class="quiet">Statements</span> <span class='fraction'>2/19</span> </div> <div class='fl pad1y space-right2'> <span class="strong">0% </span> <span class="quiet">Branches</span> <span class='fraction'>0/12</span> </div> <div class='fl pad1y space-right2'> <span class="strong">0% </span> <span class="quiet">Functions</span> <span class='fraction'>0/2</span> </div> <div class='fl pad1y space-right2'> <span class="strong">11.11% </span> <span class="quiet">Lines</span> <span class='fraction'>2/18</span> </div> </div> </div> <div class='status-line low'></div> <pre><table class="coverage"> <tr><td class="line-count quiet">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36</td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">'use strict' const sort = module.exports = { } &nbsp; sort.action = <span class="fstat-no" title="function not covered" >(r</span>equest, response, callback) =&gt; { let attribute = <span class="cstat-no" title="statement not covered" >request.params.sort</span> let ascending = <span class="cstat-no" title="statement not covered" >1</span> <span class="cstat-no" title="statement not covered" > if (!attribute) <span class="cstat-no" title="statement not covered" >return callback()</span></span> <span class="cstat-no" title="statement not covered" > attribute = (`${attribute}`)</span> <span class="cstat-no" title="statement not covered" > if (attribute[0] === '-') {</span> <span class="cstat-no" title="statement not covered" > ascending = -1</span> <span class="cstat-no" title="statement not covered" > attribute = attribute.substring(1, attribute.length)</span> } &nbsp; <span class="cstat-no" title="statement not covered" > if (!request.resourceConfig.attributes[attribute]) {</span> <span class="cstat-no" title="statement not covered" > return callback({ // eslint-disable-line standard/no-callback-literal</span> status: '403', code: 'EFORBIDDEN', title: 'Invalid sort', detail: `${request.resourceConfig.resource} do not have property ${attribute}` }) } &nbsp; // todo: consider using a stable sort algerith (e.g. lodash.sortBy) <span class="cstat-no" title="statement not covered" > response.data = response.data.sort(<span class="fstat-no" title="function not covered" >(a</span>, b) =&gt; {</span> <span class="cstat-no" title="statement not covered" > if (typeof a.attributes[attribute] === 'string') {</span> <span class="cstat-no" title="statement not covered" > return a.attributes[attribute].localeCompare(b.attributes[attribute]) * ascending</span> } else <span class="cstat-no" title="statement not covered" >if (typeof a.attributes[attribute] === 'number' || a.attributes[attribute] instanceof Date) {</span> <span class="cstat-no" title="statement not covered" > return (a.attributes[attribute] - b.attributes[attribute]) * ascending</span> } else { <span class="cstat-no" title="statement not covered" > return 0</span> } }) &nbsp; <span class="cstat-no" title="statement not covered" > return callback()</span> } &nbsp;</pre></td></tr> </table></pre> <div class='push'></div><!-- for sticky footer --> </div><!-- /wrapper --> <div class='footer quiet pad2 space-top1 center small'> Code coverage generated by <a href="https://istanbul.js.org/" target="_blank">istanbul</a> at Sat Jan 27 2018 19:59:16 GMT+0530 (IST) </div> </div> <script src="../../prettify.js"></script> <script> window.onload = function () { if (typeof prettyPrint === 'function') { prettyPrint(); } }; </script> <script src="../../sorter.js"></script> </body> </html>