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

json-magic

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-magic - npm Package Compare versions

Comparing version 0.0.10 to 0.0.11

lib/JSONPointer.js

235

lib/Magic.js
const $check=require('check-types');
const jsonPointer=require('json-pointer');
const jsonPointer=require('./JSONPointer.js');

@@ -7,2 +7,8 @@ class Magic{

/**Parse a string path to an array
*
* @param {string} path - the path to parse
* @param {string} [separator] - the separator to use. Either . or / or dot
* @returns {null|*[]|string[]|*}
*/
static parsePath(path,separator){

@@ -16,2 +22,4 @@ if (!path)return null;

sep=separator;
}else if (separator==="dot"){
sep=".";
}else{

@@ -35,8 +43,16 @@ if (count(path,'.')>count(path,'/')){

static compilePath(path,seprator,ignoreLeading){
/**Compiles an array to a path
*
* @param {array} path - the path to compile
* @param {string} [separator] - the separator to use
* @param {boolean} [ignoreLeading] - ignore the leading separator unless dot notation
* @returns {string|*}
*/
static compilePath(path,separator,ignoreLeading){
if (!path)return path;
if (!$check.array(path))throw new Error('Invalid type for path');
let sep=seprator||"/";
let sep=separator||"/";
if (sep==='.'){
if (sep==='.'||sep==="dot"){
return path.join('.');

@@ -49,2 +65,8 @@ }else{

/**Check if the path is in the object
*
* @param {object||array} obj - The object to check
* @param {string|array} path - The path to check
* @returns {boolean}
*/
static has(obj,path){

@@ -68,28 +90,47 @@ if (!obj)return false;

*
* @param {Object} obj
* @param {String|Array} path
* @param {String} separator
* @param {object|array} obj - the object to check
* @param {string|array} path - the path to retreive
* @param {string} [separator] - the path separator, either slash or dot
* @returns {*}
*/
static get (object,path,separator){
if (!object)throw new Error('Invalid object for get');
if (!$check.object(object)&&!$check.array(object))throw new Error('Invalid object for get');
return jsonPointer.get(object,Magic.parsePath(path,separator));
static get (obj,path,separator){
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));
}
static set(object,path,value){
if (!object)throw new Error('Invalid object for set');
if (!$check.object(object)&&!$check.array(object))throw new Error('Invalid object for set');
return jsonPointer.set(object,Magic.parsePath(path),value);
/**Set a value for the path on the specified object
*
* @param {object|array} obj - the object to set the value on
* @param {string|array} path - the path to set the value on
* @param {*} value - the value to set
* @returns {*}
*/
static set(obj,path,value){
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);
}
static remove(object,path){
if (!object)throw new Error('Invalid object for remove');
if (!$check.object(object)&&!$check.array(object))throw new Error('Invalid object for remove');
return jsonPointer.remove(object,Magic.parsePath(path));
/**Removes the value at the specified path
*
* @param {object|array} obj - the object to set the value on
* @param {string|array} path - the path to set the value on
* @returns {*}
*/
static remove(obj,path){
if (!obj)throw new Error('Invalid object for remove');
if (!$check.object(obj)&&!$check.array(obj))throw new Error('Invalid object for remove');
return jsonPointer.remove(obj,Magic.parsePath(path));
}
static pathDict(object,separator){
/**Returns a dictionary of paths generated from the object
*
* @param {object|array} obj - The object to generate a set of paths from
* @param {string} [separator] - The separator to show on the paths
* @returns {{}}
*/
static pathDict(obj,separator){
if (separator&&separator.toLowerCase()!=='/'){
let dict=jsonPointer.dict(object);
let dict=jsonPointer.dict(obj);
let newDict={};

@@ -104,9 +145,15 @@ for (let k in dict){

}else{
return jsonPointer.dict(object);
return jsonPointer.dict(obj);
}
}
static pathArray(object,format){
let dict=jsonPointer.dict(object);
/**Returns an array of paths defined in the object
*
* @param {object|array} obj -
* @param {string} [format] - the format for the path, e.g. . or /
* @returns {[]}
*/
static pathArray(obj,format){
let dict=jsonPointer.dict(obj);
let newDict=[];

@@ -116,3 +163,3 @@

if (!dict.hasOwnProperty(k))continue;
if (format&&format.toLowerCase()==='dot'){
if (format&&(format.toLowerCase()==='dot'||format===".")){
let newK=k.split('/');

@@ -135,16 +182,23 @@ newK.shift();

static walk(object,iterator,separator) {
if (!object) return object;
/**Walk an object or array and call an iterator function
*
* @param {object|array} obj - The object or array to walk
* @param {function} iterator - the iterator
* @param {string} [separator] - the path separator
* @returns {void|*}
*/
static walk(obj,iterator,separator) {
if (!obj) return obj;
let sep = separator || "/";
//if not an object or array, then base path
if (!$check.object(object) && !$check.array(object)) {
if (!$check.object(obj) && !$check.array(obj)) {
if (sep === '.') {
return iterator(object, '');
return iterator(obj, '');
} else {
return iterator(object, sep);
return iterator(obj, sep);
}
}
return jsonPointer.walk(object, (value, path)=> {
return jsonPointer.walk(obj, (value, path)=> {
let newPath = path;

@@ -159,4 +213,11 @@

static renameKey(object,renamer,separator){
if (!object)return object;
/**Renames a key by the renamer function
*
* @param {object|array} obj - object to rename key on
* @param {function} renamer - the function to call for each key
* @param {string} [separator] - the path separator
* @returns {*}
*/
static renameKey(obj,renamer,separator){
if (!obj)return obj;
let sep=separator||"/";

@@ -166,4 +227,4 @@ if (!renamer)return object;

//if not an object or array, then base path
if (!$check.object(object)&&!$check.array(object)){
return object;
if (!$check.object(obj)&&!$check.array(obj)){
return obj;
}

@@ -197,6 +258,6 @@

inner(object,[],null);
inner(obj,[],null);
for (let renamePath of renamePaths){
Magic.set(object,renamePath.newPath, Magic.get(object,renamePath.curPath));
Magic.set(obj,renamePath.newPath, Magic.get(obj,renamePath.curPath));
}

@@ -210,16 +271,23 @@

for (let removePath of removePaths){
Magic.remove(object,removePath.curPath);
Magic.remove(obj,removePath.curPath);
}
return object;
return obj;
}
static changeValue(object,changer,separator){
if (!object)return object;
/**Changes a value in an object passing each value to the changer function
*
* @param {object|array} obj - object to rename key on
* @param {function} changer - the function to call for each key
* @param {string} [separator] - the path separator
* @returns {*}
*/
static changeValue(obj,changer,separator){
if (!obj)return obj;
let sep=separator||"/";
if (!changer)return object;
if (!changer)return obj;
//if not an object or array, then base path
if (!$check.object(object)&&!$check.array(object)){
return object;
if (!$check.object(obj)&&!$check.array(obj)){
return obj;
}

@@ -229,3 +297,3 @@

Magic.walk(object,(val,path)=>{
Magic.walk(obj,(val,path)=>{
let newVal=changer(val,path);

@@ -241,20 +309,30 @@ if (newVal!==val){

for (let setPath of setPaths){
Magic.set(object,setPath.path, setPath.newVal);
Magic.set(obj,setPath.path, setPath.newVal);
}
return object;
return obj;
}
static convertDateTOISOString(object){
Magic.walk(object,function(value,path){
/**Converts all dates to ISOStrings
*
* @param {object|array} obj - object to rename key on
* @returns {*}
*/
static convertDateTOISOString(obj){
Magic.walk(obj,function(value,path){
if ($check.date(value)){
Magic.set(object,path,value.toISOString());
Magic.set(obj,path,value.toISOString());
}
});
return object;
return obj;
}
static fixForMongo(object){
return Magic.renameKey(object,(key,path)=>{
/**Fixes an object or array to remove any fields starting with $ that can cause issues storing in mongo
*
* @param {array|object} obj - the object to convert
* @returns {*}
*/
static fixForMongo(obj){
return Magic.renameKey(obj,(key,path)=>{
if (!key)return key;

@@ -271,2 +349,47 @@ if (!$check.string(key))return key;

/**Sets a property and value on each object and subObject
*
* @param {array|object} obj - the object to convert
* @param {string} property - the property name to set
* @param {boolean} override - override the value
* @param {*} value - the value to set
*/
static setProperty(obj,property,value,override){
if (!obj)return obj;
//if not an object or array, then base path
if (!$check.object(obj)&&!$check.array(obj)){
return obj;
}
let setPaths=[];
const inner=(curObj,curPath,curKey)=>{
if ($check.array(curObj)){
for (let i=0;i<curObj.length;i++) {
inner(curObj[i], curPath.concat(i), i);
}
}else if ($check.object(curObj)){
if(!curObj[property]||override){
setPaths.push({
path:curPath.concat(property)
});
}
for (let k in curObj){
if (!curObj.hasOwnProperty(k)) continue;
inner(curObj[k],curPath.concat(k),k);
}
}
};
inner(obj,[],null);
for (let setPath of setPaths){
Magic.set(obj,setPath.path, value);
}
return obj;
}
}

@@ -277,3 +400,3 @@

return '' + object;
};
}

@@ -291,2 +414,2 @@

module.exports=Magic;
module.exports=Magic;
{
"name": "json-magic",
"version": "0.0.10",
"version": "0.0.11",
"description": "Utilities for manipulating JSON objects.",

@@ -15,4 +15,3 @@ "homepage": "https://github.com/filepounder/json-magic",

"dependencies": {
"check-types": "8.0.2",
"json-pointer": "0.6.0"
"check-types": "11.1.2"
},

@@ -19,0 +18,0 @@ "engines": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc