Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

Alorel / personal-build-tools / 1844

23 Dec 2019 - 1:17 coverage increased (+0.3%) to 78.737%
1844

Pull #180

travis-ci-com

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
chore(package): update lockfile yarn.lock
Pull Request #180: Update nyc to the latest version 🚀

382 of 627 branches covered (60.93%)

Branch coverage included in aggregate %.

1488 of 1748 relevant lines covered (85.13%)

37.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.58
/src/lib/ObjectWriter.ts
1
import * as fs from 'fs-extra';
2
import {LazyGetter} from 'lazy-get-decorator';
8×
3
import {get, has, noop, PropertyPath, set} from 'lodash';
8×
4
import * as YAML from 'yamljs';
8×
5
import {AbstractReadWriter} from './AbstractReadWriter';
8×
6

8×
7
export const enum ObjectWriterFormat {
8×
8
  JSON,
8×
9
  YAML
10
}
162×
11

162×
12
interface Obj {
162×
13
  [k: string]: any;
14
}
15

16
export class ObjectWriter<T extends Obj = Obj> extends AbstractReadWriter {
156×
17
  private contents: T;
50×
18

106×
19
  public constructor(path: string, private readonly format: ObjectWriterFormat) {
20
    super(path);
21
    this.read();
22
  }
156×
23

24
  @LazyGetter()
50×
25
  private get parseFn(): (v: string) => T {
26
    //tslint:disable:no-unbound-method
50×
27
    switch (this.format) {
28
      case ObjectWriterFormat.YAML:
29
        return YAML.parse.bind(YAML);
30
      default:
31
        return JSON.parse;
32
    }
106×
33
    //tslint:enable:no-unbound-method
Branches [[2, 1]] missed.
34
  }
106×
35

36
  @LazyGetter()
1,700×
37
  private get stringifyFn(): (v: T) => string {
38
    //tslint:disable:no-unbound-method no-magic-numbers
39
    switch (this.format) {
40
      case ObjectWriterFormat.YAML:
41
        return (v$: T) => YAML.stringify(v$, Number.MAX_VALUE, 2);
156×
42
      default:
43
        return (v$: T) => JSON.stringify(v$, null, 2);
44
    }
156×
45
    //tslint:enable:no-unbound-method no-magic-numbers
46
  }
47

100×
48
  public get(p: PropertyPath): any {
100×
UNCOV
49
    return get(this.contents, p);
!
50
  }
51

52
  public has(p: PropertyPath): boolean {
Branches [[3, 1]] missed. 156×
53
    return has(this.contents, p);
2,198×
54
  }
55

56
  public read(): this {
2,198×
57
    noop(this.dirname);
58
    try {
59
      this.contents = this.parseFn(fs.readFileSync(this.file, 'utf8'));
2,198×
60
    } catch (e) {
61
      if (e.code === 'ENOENT') {
62
        this.contents = <any>{};
Branches [[6, 0], [6, 1]] missed. 156×
63
      } else {
2×
64
        throw e;
65
      }
2×
66
    }
67

68
    return this;
2×
69
  }
70

71
  public set(p: PropertyPath, v: any, override = true): this {
6×
72
    if (override || !this.has(p)) {
73
      set(this.contents, p, v);
74
    }
75

76
    return this;
6×
77
  }
78

79
  public toString(): string {
80
    return this.stringifyFn(this.contents) + '\n';
81
  }
6×
82
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc