Passed
Push — develop ( b91919...1ff2d0 )
by Endre
05:15
created

StyleUrlFormatter.calculatePageDepth   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
import {IPageData} from '@enbock/application-router/Router';
2
import {IObserver} from '@enbock/state-value-observer/Observer';
3
4
export interface IStyleUrlFormatter {
5
  format(url: string): string;
6
}
7
8
export default class StyleUrlFormatter {
9
  protected currentPage: IObserver<IPageData | null>;
10
11
  constructor(currentPage: IObserver<IPageData | null>) {
12 4
    this.currentPage = currentPage;
13
  }
14
15
  format(url: string): string {
16 3
    const currentPage: IPageData | null = this.currentPage.value;
17 3
    const depth: number = currentPage != null ? this.calculatePageDepth(currentPage) : 0;
18
19 3
    let pathOffset: string = './';
20 3
    if (depth > 0) {
21
      let index: number;
22 1
      pathOffset = '';
23 1
      for (index = 0; index < depth; index++) {
24 2
        pathOffset += '../';
25
      }
26
    }
27
28 3
    return pathOffset + 'Style/' + url + '.css';
29
  }
30
31
  private calculatePageDepth(page: IPageData): number {
32 2
    return page.baseUrl.replace(/[^\/]*/g, '').length - 1;
33
  }
34
}
35