Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More

@synatic/json-magic

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@synatic/json-magic - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

@@ -10,6 +10,7 @@ const $check = require('check-types');

* @param {string} path - the path to parse
* @param {string} [separator] - the separator to use. Either . or / or dot
* @param {string|null} [separator] - the separator to use. Either . or / or dot
* @param {boolean} [ignoreSeparator] - whether to ignore the separation or not
* @return {null|*[]|string[]|*}
*/
static parsePath(path, separator) {
static parsePath(path, separator, ignoreSeparator) {
if (!path) return null;

@@ -21,5 +22,3 @@ if ($check.array(path)) return path;

if (separator) {
sep = separator;
} else if (separator === 'dot') {
sep = '.';
sep = separator === 'dot' ? '.' : separator;
} else {

@@ -37,2 +36,5 @@ if (count(path, '.') > count(path, '/')) {

}
if (ignoreSeparator)
return [path];
return path.split(sep);

@@ -90,9 +92,10 @@ } else {

* @param {string|array} path - the path to retreive
* @param {string} [separator] - the path separator, either slash or dot
* @param {string|null} [separator] - the path separator, either slash or dot
* @param {boolean} [ignoreSeparator] - whether to ignore the separation or not
* @return {*}
*/
static get(obj, path, separator) {
static get(obj, path, separator, ignoreSeparator) {
if (!obj) throw new Error('Invalid object for get');
if (!$check.object(obj) && !$check.array(obj)) throw new Error('Invalid object for get');
return jsonPointer.get(obj, Magic.parsePath(path, separator));
return jsonPointer.get(obj, Magic.parsePath(path, separator, ignoreSeparator));
}

@@ -105,8 +108,9 @@

* @param {*} value - the value to set
* @param {boolean} [ignoreSeparator] - whether to ignore the separation or not
* @return {*}
*/
static set(obj, path, value) {
static set(obj, path, value, ignoreSeparator) {
if (!obj) throw new Error('Invalid object for set');
if (!$check.object(obj) && !$check.array(obj)) throw new Error('Invalid object for set');
return jsonPointer.set(obj, Magic.parsePath(path), value);
return jsonPointer.set(obj, Magic.parsePath(path, null, ignoreSeparator), value);
}

@@ -113,0 +117,0 @@

{
"name": "@synatic/json-magic",
"version": "0.1.1",
"version": "0.1.2",
"description": "Utilities for manipulating JSON objects.",

@@ -50,4 +50,4 @@ "main": "index.js",

"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^7.1.0",
"mocha": "^8.2.1",
"eslint-config-prettier": "^8.1.0",
"mocha": "^9.0.1",
"nyc": "^15.1.0",

@@ -54,0 +54,0 @@ "prettier": "^2.2.1"