Code coverage report for src/segments.js

Statements: 92% (23 / 25)      Branches: 100% (2 / 2)      Functions: 84.21% (16 / 19)      Lines: 92% (23 / 25)      Ignored: none     

All files » src/ » segments.js
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 981         1                                       1 29 29       29 29 174 174         29       15         1 20       20       20       11 11         1 6       6       6       2 2                                
const specials = [
  '/', '.', '*', '+', '?', '|',
  '(', ')', '[', ']', '{', '}', '\\'
];
 
const escapeRegex = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
 
// A Segment represents a segment in the original route description.
// Each Segment type provides an `eachChar` and `regex` method.
//
// The `eachChar` method invokes the callback with one or more character
// specifications. A character specification consumes one or more input
// characters.
//
// The `regex` method returns a regex fragment for the segment. If the
// segment is a dynamic or star segment, the regex fragment also includes
// a capture.
//
// A character specification contains:
//
// * `validChars`: a String with a list of all valid characters, or
// * `invalidChars`: a String with a list of all invalid characters
// * `repeat`: true if the character specification can repeat
 
export class StaticSegment {
  constructor(string: string, caseSensitive: boolean) {
    this.string = string;
    this.caseSensitive = caseSensitive;
  }
 
  eachChar(callback: (spec: CharSpec) => void): void {
    let s = this.string;
    for (let i = 0, ii = s.length; i < ii; ++i) {
      let ch = s[i];
      callback({ validChars: this.caseSensitive ? ch : ch.toUpperCase() + ch.toLowerCase() });
    }
  }
 
  regex(): string {
    return this.string.replace(escapeRegex, '\\$1');
  }
 
  generate(): string {
    return this.string;
  }
}
 
export class DynamicSegment {
  constructor(name: string) {
    this.name = name;
  }
 
  eachChar(callback: (spec: CharSpec) => void): void {
    callback({ invalidChars: '/', repeat: true });
  }
 
  regex(): string {
    return '([^/]+)';
  }
 
  generate(params: Object, consumed: Object): string {
    consumed[this.name] = true;
    return params[this.name];
  }
}
 
export class StarSegment {
  constructor(name: string) {
    this.name = name;
  }
 
  eachChar(callback: (spec: CharSpec) => void): void {
    callback({ invalidChars: '', repeat: true });
  }
 
  regex(): string {
    return '(.+)';
  }
 
  generate(params: Object, consumed: Object): string {
    consumed[this.name] = true;
    return params[this.name];
  }
}
 
export class EpsilonSegment {
  eachChar(): void {
  }
 
  regex(): string {
    return '';
  }
 
  generate(): string {
    return '';
  }
}