FieldRef.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. };
  9. proto.setPart = function setPart( i, part ) {
  10. this._array[i] = part;
  11. this._path = this._array.join('.');
  12. };
  13. proto.getPart = function getPArt( i ) {
  14. return this._array[i];
  15. };
  16. proto.isPrefixOf = function isPrefixOf( other ) {
  17. return ( other._path.indexOf(this.path) === 0 );
  18. };
  19. proto.commonPrefixSize = function commonPrefixSize ( other ) {
  20. var i = 0;
  21. while(other._array[i] == this._array[i]) { i++; }
  22. return i;
  23. };
  24. proto.dottedField = function dottedField( ) {
  25. var offset = 0;
  26. if(arguments.length == 1){
  27. offset = arguments[0];
  28. }
  29. return this._array.slice( offset ).join('.');
  30. };
  31. proto.equalsDottedField = function equalsDottedField ( other ) {
  32. return this._path == other._path;
  33. };
  34. proto.compare = function compare( other ) {
  35. return (this._path < other._path ? -1 : this._path > other._path ? 1 : 0);
  36. };
  37. proto.clear = function clear() {
  38. this._path = '';
  39. this._array = [];
  40. };
  41. proto.numParts = function numParts() {
  42. return this._array.length;
  43. };
  44. proto.numReplaced = function numReplaced() {
  45. throw new Error('Why?');
  46. };