Giter Club home page Giter Club logo

Comments (2)

tiagocorreiaalmeida avatar tiagocorreiaalmeida commented on July 21, 2024 1

I had a similar problem, but in my case the key was nested much deeper, so I updated the source code to contain the path of the key being provided to the callback function

Here's the git patch for it

diff --git a/node_modules/humps/humps.js b/node_modules/humps/humps.js
index 444b4d9..670b8f0 100644
--- a/node_modules/humps/humps.js
+++ b/node_modules/humps/humps.js
@@ -10,7 +10,7 @@
 
 ;(function(global) {
 
-  var _processKeys = function(convert, obj, options) {
+  var _processKeys = function(convert, obj, options, path = []) {
     if(!_isObject(obj) || _isDate(obj) || _isRegExp(obj) || _isBoolean(obj) || _isFunction(obj)) {
       return obj;
     }
@@ -22,14 +22,14 @@
     if(_isArray(obj)) {
       output = [];
       for(l=obj.length; i<l; i++) {
-        output.push(_processKeys(convert, obj[i], options));
+        output.push(_processKeys(convert, obj[i], options, path));
       }
     }
     else {
       output = {};
       for(var key in obj) {
         if(Object.prototype.hasOwnProperty.call(obj, key)) {
-          output[convert(key, options)] = _processKeys(convert, obj[key], options);
+          output[convert(key, options, path)] = _processKeys(convert, obj[key], options, path.concat(key));
         }
       }
     }
@@ -106,8 +106,8 @@
       return convert;
     }
 
-    return function(string, options) {
-      return callback(string, convert, options);
+    return function(string, options, path) {
+      return callback(string, convert, options, path);
     }
   };
 

Then it can be used as follow:

humps.camelizeKeys(obj, (key, convert, _, path) => {
  if (path.includes("media")) {
    return key;
  }

  return convert(key);
});

Hope this helps

from humps.

leviwheatcroft avatar leviwheatcroft commented on July 21, 2024

I'm a bit late but maybe something like...

var raw = yourObject
var result = Object.assign(camelizeKeys(raw), { important_stuff: raw.important_stuff })

from humps.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.