all files / firebase/utils/ ReferenceBase.js

100% Statements 4/4
100% Branches 8/8
100% Functions 2/2
100% Lines 4/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30              1346× 1334×         12×                     504×          
/**
 * @flow
 */
export default class ReferenceBase {
  path: string;
 
  constructor(path: string) {
    if (path) {
      this.path =
        path.length > 1 && path.endsWith('/')
          ? path.substring(0, path.length - 1)
          : path;
    } else {
      this.path = '/';
    }
  }
 
  /**
   * The last part of a Reference's path (after the last '/')
   * The key of a root Reference is null.
   * @type {String}
   * {@link https://firebase.google.com/docs/reference/js/firebase.database.Reference#key}
   */
  get key(): string | null {
    return this.path === '/'
      ? null
      : this.path.substring(this.path.lastIndexOf('/') + 1);
  }
}