FieldRef.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. var FieldRef = module.exports = function FieldRef (){
  3. this._array = [];
  4. this._path = '';
  5. }, klass = FieldRef, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  6. proto.parse = function parse( str ) {
  7. this._array = str.split('.');
  8. this._path = this._array.join('.');
  9. };
  10. proto.setPart = function setPart( i, part ) {
  11. this._array[i] = part;
  12. this._path = this._array.join('.');
  13. };
  14. proto.getPart = function getPArt( i ) {
  15. return this._array[i];
  16. };
  17. proto.isPrefixOf = function isPrefixOf( other ) {
  18. return ( other._path.indexOf(this.path) === 0 );
  19. };
  20. proto.commonPrefixSize = function commonPrefixSize ( other ) {
  21. var i = 0;
  22. while(other._array[i] == this._array[i]) { i++; }
  23. return i;
  24. };
  25. proto.dottedField = function dottedField( ) {
  26. var offset = 0;
  27. if(arguments.length == 1){
  28. offset = arguments[0];
  29. }
  30. return this._array.slice( offset ).join('.');
  31. };
  32. proto.equalsDottedField = function equalsDottedField ( other ) {
  33. return this._path == other._path;
  34. };
  35. proto.compare = function compare( other ) {
  36. return (this._path < other._path ? -1 : this._path > other._path ? 1 : 0);
  37. };
  38. proto.clear = function clear() {
  39. this._path = '';
  40. this._array = [];
  41. };
  42. proto.numParts = function numParts() {
  43. return this._array.length;
  44. };
  45. proto.numReplaced = function numReplaced() {
  46. throw new Error('Why?');
  47. };