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.

equality_filter.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. ///--- API
  7. function EqualityFilter(options) {
  8. if (typeof (options) === 'object') {
  9. assert.string(options.attribute, 'options.attribute');
  10. this.attribute = options.attribute;
  11. // Prefer Buffers over strings to make filter cloning easier
  12. if (options.raw) {
  13. this.raw = options.raw;
  14. } else {
  15. this.raw = new Buffer(options.value);
  16. }
  17. } else {
  18. this.raw = new Buffer(0);
  19. }
  20. var self = this;
  21. this.__defineGetter__('type', function () { return 'equal'; });
  22. this.__defineGetter__('value', function () {
  23. return (Buffer.isBuffer(self.raw)) ? self.raw.toString() : self.raw;
  24. });
  25. this.__defineSetter__('value', function (data) {
  26. if (typeof (data) === 'string') {
  27. self.raw = new Buffer(data);
  28. } else if (Buffer.isBuffer(data)) {
  29. self.raw = new Buffer(data.length);
  30. data.copy(self.raw);
  31. } else {
  32. self.raw = data;
  33. }
  34. });
  35. this.__defineGetter__('json', function () {
  36. return {
  37. type: 'EqualityMatch',
  38. attribute: self.attribute,
  39. value: self.value
  40. };
  41. });
  42. }
  43. util.inherits(EqualityFilter, helpers.Filter);
  44. EqualityFilter.prototype.toString = function () {
  45. return ('(' + helpers.escape(this.attribute) +
  46. '=' + helpers.escape(this.value) + ')');
  47. };
  48. EqualityFilter.prototype.matches = function (target, strictAttrCase) {
  49. assert.object(target, 'target');
  50. var tv = helpers.getAttrValue(target, this.attribute, strictAttrCase);
  51. var value = this.value;
  52. return helpers.testValues(function (v) {
  53. return value === v;
  54. }, tv);
  55. };
  56. ///--- Exports
  57. module.exports = EqualityFilter;