UNPKG

@coding-blocks/jsonapi-server

Version:

A config driven NodeJS framework implementing json:api

363 lines (362 loc) 14.7 kB
<!doctype html> <html lang="en"> <head> <title>Code coverage report for lib/postProcessing/filter.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> filter.js </h1> <div class='clearfix'> <div class='fl pad1y space-right2'> <span class="strong">12.5% </span> <span class="quiet">Statements</span> <span class='fraction'>8/64</span> </div> <div class='fl pad1y space-right2'> <span class="strong">0% </span> <span class="quiet">Branches</span> <span class='fraction'>0/32</span> </div> <div class='fl pad1y space-right2'> <span class="strong">0% </span> <span class="quiet">Functions</span> <span class='fraction'>0/12</span> </div> <div class='fl pad1y space-right2'> <span class="strong">14.04% </span> <span class="quiet">Lines</span> <span class='fraction'>8/57</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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100</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-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <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-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-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-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-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</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-neutral">&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-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-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> <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-yes">1x</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-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-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-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</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-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> <span class="cline-any cline-yes">1x</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-no">&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-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-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">'use strict' const filter = module.exports = { } &nbsp; const _ = { assign: require('lodash.assign'), isEqual: require('lodash.isequal') } const debug = require('../debugging.js') &nbsp; filter.action = <span class="fstat-no" title="function not covered" >(r</span>equest, response, callback) =&gt; { const filters = <span class="cstat-no" title="statement not covered" >request.processedFilter</span> <span class="cstat-no" title="statement not covered" > if (!filters) <span class="cstat-no" title="statement not covered" >return callback()</span></span> &nbsp; <span class="cstat-no" title="statement not covered" > if (Array.isArray(response.data)) {</span> <span class="cstat-no" title="statement not covered" > for (let j = 0; j &lt; response.data.length; j++) {</span> <span class="cstat-no" title="statement not covered" > if (!filter._filterKeepObject(response.data[j], filters)) {</span> <span class="cstat-no" title="statement not covered" > debug.filter('removed', JSON.stringify(filters), JSON.stringify(response.data[j].attributes))</span> <span class="cstat-no" title="statement not covered" > response.data.splice(j, 1)</span> <span class="cstat-no" title="statement not covered" > j--</span> } } } else <span class="cstat-no" title="statement not covered" >if (response.data instanceof Object) {</span> <span class="cstat-no" title="statement not covered" > if (!filter._filterKeepObject(response.data, filters)) {</span> <span class="cstat-no" title="statement not covered" > debug.filter('removed', JSON.stringify(filters), JSON.stringify(response.data.attributes))</span> <span class="cstat-no" title="statement not covered" > response.data = null</span> } } &nbsp; <span class="cstat-no" title="statement not covered" > return callback()</span> } &nbsp; filter._filterMatches = <span class="fstat-no" title="function not covered" >(f</span>ilterElement, attributeValue) =&gt; { <span class="cstat-no" title="statement not covered" > if (!filterElement.operator) {</span> <span class="cstat-no" title="statement not covered" > return _.isEqual(attributeValue, filterElement.value)</span> } const filterFunction = <span class="cstat-no" title="statement not covered" >{</span> '&gt;': function <span class="fstat-no" title="function not covered" >filterGreaterThan </span>(attrValue, filterValue) { <span class="cstat-no" title="statement not covered" > return attrValue &gt; filterValue</span> }, '&lt;': function <span class="fstat-no" title="function not covered" >filterLessThan </span>(attrValue, filterValue) { <span class="cstat-no" title="statement not covered" > return attrValue &lt; filterValue</span> }, '~': function <span class="fstat-no" title="function not covered" >filterCaseInsensitiveEqual </span>(attrValue, filterValue) { <span class="cstat-no" title="statement not covered" > return attrValue.toLowerCase() === filterValue.toLowerCase()</span> }, ':': function <span class="fstat-no" title="function not covered" >filterCaseInsensitiveContains </span>(attrValue, filterValue) { <span class="cstat-no" title="statement not covered" > return attrValue.toLowerCase().indexOf(filterValue.toLowerCase()) !== -1</span> } }[filterElement.operator] const result = <span class="cstat-no" title="statement not covered" >filterFunction(attributeValue, filterElement.value)</span> <span class="cstat-no" title="statement not covered" > return result</span> } &nbsp; filter._filterKeepObject = <span class="fstat-no" title="function not covered" >(s</span>omeObject, filters) =&gt; { <span class="cstat-no" title="statement not covered" > for (const filterName in filters) {</span> const whitelist = <span class="cstat-no" title="statement not covered" >filters[filterName]</span> &nbsp; <span class="cstat-no" title="statement not covered" > if (someObject.attributes.hasOwnProperty(filterName) || (filterName === 'id')) {</span> let attributeValue = <span class="cstat-no" title="statement not covered" >someObject.attributes[filterName]</span> <span class="cstat-no" title="statement not covered" > if (filterName === 'id') <span class="cstat-no" title="statement not covered" >attributeValue = someObject.id</span></span> const attributeMatches = <span class="cstat-no" title="statement not covered" >filter._attributesMatchesOR(attributeValue, whitelist)</span> <span class="cstat-no" title="statement not covered" > if (!attributeMatches) <span class="cstat-no" title="statement not covered" >return false</span></span> } else <span class="cstat-no" title="statement not covered" >if (someObject.relationships.hasOwnProperty(filterName)) {</span> const relationships = <span class="cstat-no" title="statement not covered" >someObject.relationships[filterName]</span> const relationshipMatches = <span class="cstat-no" title="statement not covered" >filter._relationshipMatchesOR(relationships, whitelist)</span> <span class="cstat-no" title="statement not covered" > if (!relationshipMatches) <span class="cstat-no" title="statement not covered" >return false</span></span> } else { <span class="cstat-no" title="statement not covered" > return false</span> } } <span class="cstat-no" title="statement not covered" > return true</span> } &nbsp; filter._attributesMatchesOR = <span class="fstat-no" title="function not covered" >(a</span>ttributeValue, whitelist) =&gt; { let matchOR = <span class="cstat-no" title="statement not covered" >false</span> <span class="cstat-no" title="statement not covered" > whitelist.forEach(<span class="fstat-no" title="function not covered" >fi</span>lterElement =&gt; {</span> <span class="cstat-no" title="statement not covered" > if (filter._filterMatches(filterElement, attributeValue)) {</span> <span class="cstat-no" title="statement not covered" > matchOR = true</span> } }) <span class="cstat-no" title="statement not covered" > return matchOR</span> } &nbsp; filter._relationshipMatchesOR = <span class="fstat-no" title="function not covered" >(r</span>elationships, whitelist) =&gt; { let matchOR = <span class="cstat-no" title="statement not covered" >false</span> &nbsp; let data = <span class="cstat-no" title="statement not covered" >relationships.data</span> <span class="cstat-no" title="statement not covered" > if (!data) <span class="cstat-no" title="statement not covered" >return false</span></span> &nbsp; <span class="cstat-no" title="statement not covered" > if (!(Array.isArray(data))) <span class="cstat-no" title="statement not covered" >data = [ data ]</span></span> <span class="cstat-no" title="statement not covered" > data = data.map(<span class="fstat-no" title="function not covered" >re</span>lation =&gt; <span class="cstat-no" title="statement not covered" >relation.id)</span></span> &nbsp; <span class="cstat-no" title="statement not covered" > whitelist.forEach(<span class="fstat-no" title="function not covered" >fi</span>lterElement =&gt; {</span> <span class="cstat-no" title="statement not covered" > if (data.indexOf(filterElement.value) !== -1) {</span> <span class="cstat-no" title="statement not covered" > matchOR = true</span> } }) <span class="cstat-no" title="statement not covered" > return matchOR</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>