All files json_file.ts

100% Statements 79/79
96.77% Branches 30/31
100% Functions 15/15
100% Lines 61/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 941x           1x 1x 17x   1x 88x   78x 78x 78x       1x 11x   11x   11x 16x 16x 10x   6x     5x     1x 3x   3x 5x 5x 1x 1x   4x 3x 3x   1x   3x     1x 5x 5x 5x 5x 5x 4x     5x 5x   1x 11x 11x   1x 3x 3x 3x   1x 3x 3x 3x   1x 2x   1x 3x 3x 3x   1x 2x 2x 2x   1x  
import {ParseableFile} from './parsable_file';
 
export type JSONValue = null | number | string | boolean | JSONValue[] | {[k: string]: undefined | JSONValue};
 
export type JSONObject = {[k: string]: undefined | JSONValue};
 
export class JSONFile<T extends JSONObject = JSONObject> extends ParseableFile<T> {
  stringify(content?: T): string {
    return JSON.stringify(content, null, 2) || '{}';
  }
  parse(content: string): T {
    return JSON.parse(content);
  }
  get parsedContent(): Promise<T> {
    return (async () => {
      return this.parse(await this.content);
    })();
  }
  /** @see https://stackoverflow.com/a/8817461/340688 */
  static getDeep(obj: JSONObject, key: string): undefined | JSONValue {
    const paths = key.split('.');
    // eslint-disable-next-line
    let current: any = obj;
    let i;
    for (i = 0; i < paths.length; ++i) {
      const m = current[paths[i]];
      if (m !== undefined && m !== null) {
        current = current[paths[i]];
      } else {
        return undefined;
      }
    }
    return current as JSONValue;
  }
  /** @see https://stackoverflow.com/a/18937118/340688 */
  static setDeep(obj: JSONObject, key: string, value: JSONValue): JSONObject {
    const path = key.split('.');
    // eslint-disable-next-line
    path.reduce((a, b, level): any => {
      const len = path.length - 1;
      if (typeof a[b] === 'undefined' && level !== len) {
        a[b] = {};
        return a[b];
      }
      if (level === len) {
        a[b] = value;
        return value;
      }
      return a[b];
    }, obj);
    return obj;
  }
  /** @see https://stackoverflow.com/a/48666737/340688 */
  static unsetDeep(obj: JSONObject, key: string) {
    const path = key.split('.');
    for (let i = 0; i < path.length - 1; i++) {
      const v = obj[path[i]];
      console.log(v);
      if (typeof v === 'object' && v && !Array.isArray(v)) {
        obj = v;
      }
    }
    const value = path.pop();
    if (value) delete obj[value];
  }
  async getDeep(prop: string) {
    const content = await this.parsedContent;
    return JSONFile.getDeep(content, prop);
  }
  async setDeep(prop: string, value: JSONValue) {
    const content = await this.parsedContent;
    JSONFile.setDeep(content, prop, value);
    return await this.update(content);
  }
  async unsetDeep(prop: string) {
    const content = await this.parsedContent;
    JSONFile.unsetDeep(content, prop);
    return await this.update(content);
  }
  async get(prop: keyof T) {
    return (await this.parsedContent)[prop];
  }
  async set(prop: keyof T, value: JSONValue) {
    const content = await this.parsedContent;
    Object.assign(content, {[prop]: value});
    return await this.update(content);
  }
  async unset(prop: keyof T) {
    const content = await this.parsedContent;
    delete content[prop];
    return await this.update(content);
  }
}