Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2014 Mark Cavage, Inc. All rights reserved.
  2. // Copyright 2014 Patrick Mooney. All rights reserved.
  3. var assert = require('assert-plus');
  4. ///--- API
  5. /**
  6. * RFC 2254 Escaping of filter strings
  7. *
  8. * Raw Escaped
  9. * (o=Parens (R Us)) (o=Parens \28R Us\29)
  10. * (cn=star*) (cn=star\2A)
  11. * (filename=C:\MyFile) (filename=C:\5cMyFile)
  12. *
  13. * Use substr_filter to avoid having * ecsaped.
  14. *
  15. * @author [Austin King](https://github.com/ozten)
  16. */
  17. function _escape(inp) {
  18. if (typeof (inp) === 'string') {
  19. var esc = '';
  20. for (var i = 0; i < inp.length; i++) {
  21. switch (inp[i]) {
  22. case '*':
  23. esc += '\\2a';
  24. break;
  25. case '(':
  26. esc += '\\28';
  27. break;
  28. case ')':
  29. esc += '\\29';
  30. break;
  31. case '\\':
  32. esc += '\\5c';
  33. break;
  34. case '\0':
  35. esc += '\\00';
  36. break;
  37. default:
  38. esc += inp[i];
  39. break;
  40. }
  41. }
  42. return esc;
  43. } else {
  44. return inp;
  45. }
  46. }
  47. /**
  48. * Check value or array with test function.
  49. *
  50. * @param {Function} rule test function.
  51. * @param value value or array of values.
  52. * @param {Boolean} allMatch require all array values to match. default: false
  53. */
  54. function testValues(rule, value, allMatch) {
  55. if (Array.isArray(value)) {
  56. var i;
  57. if (allMatch) {
  58. // Do all entries match rule?
  59. for (i = 0; i < value.length; i++) {
  60. if (!rule(value[i])) {
  61. return false;
  62. }
  63. }
  64. return true;
  65. } else {
  66. // Do any entries match rule?
  67. for (i = 0; i < value.length; i++) {
  68. if (rule(value[i])) {
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. } else {
  75. return rule(value);
  76. }
  77. }
  78. /**
  79. * Fetch value for named object attribute.
  80. *
  81. * @param {Object} obj object to fetch value from
  82. * @param {String} attr name of attribute to fetch
  83. * @param {Boolean} strictCase attribute name is case-sensitive. default: false
  84. */
  85. function getAttrValue(obj, attr, strictCase) {
  86. assert.object(obj);
  87. assert.string(attr);
  88. // Check for exact case match first
  89. if (obj.hasOwnProperty(attr)) {
  90. return obj[attr];
  91. } else if (strictCase) {
  92. return undefined;
  93. }
  94. // Perform case-insensitive enumeration after that
  95. var lower = attr.toLowerCase();
  96. var result;
  97. Object.getOwnPropertyNames(obj).some(function (name) {
  98. if (name.toLowerCase() === lower) {
  99. result = obj[name];
  100. return true;
  101. }
  102. return false;
  103. });
  104. return result;
  105. }
  106. /**
  107. * Filter base class
  108. */
  109. function Filter() {
  110. }
  111. /**
  112. * Depth-first filter traversal
  113. */
  114. Filter.prototype.forEach = function forEach(cb) {
  115. if (this.filter) {
  116. // not
  117. this.filter.forEach(cb);
  118. } else if (this.filters) {
  119. // and/or
  120. this.filters.forEach(function (item) {
  121. item.forEach(cb);
  122. });
  123. }
  124. cb(this);
  125. };
  126. /**
  127. * Depth-first map traversal.
  128. */
  129. Filter.prototype.map = function map(cb) {
  130. var child;
  131. if (this.filter) {
  132. child = this.filter.map(cb);
  133. if (child === null) {
  134. // empty NOT not allowed
  135. return null;
  136. } else {
  137. this.filter = child;
  138. }
  139. } else if (this.filters) {
  140. child = this.filters.map(function (item) {
  141. return item.map(cb);
  142. }).filter(function (item) {
  143. return (item !== null);
  144. });
  145. if (child.length === 0) {
  146. // empty and/or not allowed
  147. return null;
  148. } else {
  149. this.filters = child;
  150. }
  151. }
  152. return cb(this);
  153. };
  154. ///--- Exports
  155. module.exports = {
  156. escape: _escape,
  157. testValues: testValues,
  158. getAttrValue: getAttrValue,
  159. Filter: Filter
  160. };