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.

substr_filter.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2014 Mark Cavage, Inc. All rights reserved.
  2. // Copyright 2014 Patrick Mooney. All rights reserved.
  3. var util = require('util');
  4. var assert = require('assert-plus');
  5. var helpers = require('./helpers');
  6. ///--- Helpers
  7. function escapeRegExp(str) {
  8. /* JSSTYLED */
  9. return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
  10. }
  11. ///--- API
  12. function SubstringFilter(options) {
  13. if (typeof (options) === 'object') {
  14. assert.string(options.attribute, 'options.attribute');
  15. this.attribute = options.attribute;
  16. this.initial = options.initial;
  17. this.any = options.any ? options.any.slice(0) : [];
  18. this.final = options.final;
  19. } else {
  20. this.any = [];
  21. }
  22. var self = this;
  23. this.__defineGetter__('type', function () { return 'substring'; });
  24. this.__defineGetter__('json', function () {
  25. return {
  26. type: 'SubstringMatch',
  27. initial: self.initial,
  28. any: self.any,
  29. final: self.final
  30. };
  31. });
  32. }
  33. util.inherits(SubstringFilter, helpers.Filter);
  34. SubstringFilter.prototype.toString = function () {
  35. var str = '(' + helpers.escape(this.attribute) + '=';
  36. if (this.initial)
  37. str += helpers.escape(this.initial);
  38. str += '*';
  39. this.any.forEach(function (s) {
  40. str += helpers.escape(s) + '*';
  41. });
  42. if (this.final)
  43. str += helpers.escape(this.final);
  44. str += ')';
  45. return str;
  46. };
  47. SubstringFilter.prototype.matches = function (target, strictAttrCase) {
  48. assert.object(target, 'target');
  49. var tv = helpers.getAttrValue(target, this.attribute, strictAttrCase);
  50. if (tv !== undefined && tv !== null) {
  51. var re = '';
  52. if (this.initial)
  53. re += '^' + escapeRegExp(this.initial) + '.*';
  54. this.any.forEach(function (s) {
  55. re += escapeRegExp(s) + '.*';
  56. });
  57. if (this.final)
  58. re += escapeRegExp(this.final) + '$';
  59. var matcher = new RegExp(re);
  60. return helpers.testValues(function (v) {
  61. return matcher.test(v);
  62. }, tv);
  63. }
  64. return false;
  65. };
  66. ///--- Exports
  67. module.exports = SubstringFilter;