All files / latest json.js

100% Statements 97/97
100% Branches 9/9
100% Functions 7/7
100% Lines 97/97

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 94 95 96 97 981x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 235x 235x 235x 235x 1x 1x 1x 1x 1x 1x 1x 1x 1x 459x 459x 459x 459x 459x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 43x 43x 43x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x  
const set = require('./src/handlers/set/set.js');
const setAll = require('./src/handlers/set/setAll.js');
const get = require('./src/handlers/get/get.js');
const chop = require('./src/handlers/basic/chop.js');
const getAll = require('./src/handlers/get/getAll.js');
const makeJson = require('./src/handlers/make/makeJson.js');
const makeObject = require('./src/handlers/make/makeObject.js');
const mergeFunctions = require('./src/helpers/mergeFunctions');
const loadDefaultSettings = require('./src/helpers/loadDefaultSettings');
const validateReponseAndPassDefault = require('./src/helpers/validateReponseAndPassDefault');
const unlink = require('./src/handlers/basic/unlink');
 
class Json {
  /**
   * Construct Json
   * @param {any} object - input bject/array
   * @param {Object} settings object, currently only support for fatalErrorOnCreate,
   * if true, an arror will be thrown on set if query is not met.
   * @param {Object} functions object of functions that can be called within query.
   */
  constructor(object, settings, functions) {
    this.settings = loadDefaultSettings(makeObject(settings));
    this.object = this.settings.unlinkInputObject ? unlink(makeJson(object)) : makeJson(object);
    this.functions = makeObject(functions);
  }
 
  /**
   * Retreives single value from objects specified path
   * @param {any} path - string or array representation of path to set.
   * @param {Object} functions object of functions that can be called within query.
   * @returns {any} returns value found at specified path, in case that multiple logical checks
   * satisfy the first element will be returned
   */
  get(path, functions) {
    const funcs = mergeFunctions(functions, this.functions);
    return validateReponseAndPassDefault(
      get(this.object, path, funcs), undefined, this.settings.defaultGetResponse,
    );
  }
 
  /**
   * Retreives all values from objects specified path
   * @param {any} path - string or array representation of path to set.
   * @param {Object} functions object of functions that can be called within query.
   * @returns {Array} returns array of values that match the specified path with logical checks
   */
  getAll(path, functions) {
    const funcs = mergeFunctions(functions, this.functions);
    return validateReponseAndPassDefault(
      getAll(this.object, path, funcs), [], this.settings.defaultGetAllResponse,
    );
  }
 
  /**
   * Sets single value on specified path
   * @param {any} path - string or array representation of path to set.
   * @param {any} val - value to be set at specified path.
   * @param {Object} functions object of functions that can be called within query.
   * @returns {object} object with newly set path in case that multiple logical checks
   * satisfy the first element will be set.
   */
  set(path, val, functions) {
    const funcs = mergeFunctions(functions, this.functions);
    return set(this.object, path, val, this.settings.fatalErrorOnCreate, funcs);
  }
 
  /**
   * Sets all values on specified path
   * @param {any} path - string or array representation of path to set.
   * @param {any} val - value to be set at specified path.
   * @param {Object} functions object of functions that can be called within query.
   * @returns {object} object with newly set path in case that multiple logical checks
   * satisfy the first element will be set.
   */
  setAll(path, val, functions) {
    const funcs = mergeFunctions(functions, this.functions);
    return setAll(this.object, path, val, this.settings.fatalErrorOnCreate, funcs);
  }
 
  /**
   * Chops an array or object into smaller pieces
   * @param {object} value - object or array
   * @param {number} chopSize - size of pieces.
   * @returns {Array} array of chopped pieces.
   */
  chop(chopSize) { return chop(this.object, chopSize); }
 
  /**
   * Exports the object
   * @returns {Object} object
   */
  export() {
    return this.object;
  }
}
 
module.exports = Json;