All files / latest json.js

100% Statements 72/72
100% Branches 7/7
100% Functions 7/7
100% Lines 72/72

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 731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 159x 159x 159x 159x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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');
 
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.
   */
  constructor(object, settings) {
    const setting = makeObject(settings);
    this.object = makeJson(object);
    this.fatalErrorOnCreate = setting.fatalErrorOnCreate;
  }
 
  /**
   * Retreives single value from objects specified path
   * @param {any} path - string or array representation of path to set.
   * @returns {any} returns value found at specified path, in case that multiple logical checks
   * satisfy the first element will be returned
   */
  get(path) { return get(this.object, path); }
 
  /**
   * Retreives all values from objects specified path
   * @param {any} path - string or array representation of path to set.
   * @returns {Array} returns array of values that match the specified path with logical checks
   */
  getAll(path) { return getAll(this.object, path); }
 
  /**
   * 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.
   * @returns {object} object with newly set path in case that multiple logical checks
   * satisfy the first element will be set.
   */
  set(path, val) { return set(this.object, path, val, this.fatalErrorOnCreate); }
 
  /**
   * 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.
   * @returns {object} object with newly set path in case that multiple logical checks
   * satisfy the first element will be set.
   */
  setAll(path, val) { return setAll(this.object, path, val, this.fatalErrorOnCreate); }
 
  /**
   * 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;