hughsk / flat

:steam_locomotive: Flatten/unflatten nested Javascript objects
BSD 3-Clause "New" or "Revised" License
1.78k stars 196 forks source link

Custom delimiter function #65

Open mxl opened 7 years ago

mxl commented 7 years ago

Proposal

Support passing (string, string) => string function as a delimiter option.

For example, for object:

{
  first: {
    second: "hello"
  }
}

and delimiter:

(left, right) => left + right.charAt(0).toUpperCase() + right.slice(1);

it will produce camel case names:

{
  firstSecond: "hello"
}
mxl commented 7 years ago

@hughsk WDYT if I'll prepare such PR?

kristianmandrup commented 6 years ago

I need the camel case option as well. We need this functionality for better customisation of key names than a simple separator!

kristianmandrup commented 6 years ago

There is a snake case variant here

kristianmandrup commented 6 years ago

I complete rewrote flat as flat2

Includes loads of customisation options for custom key generation and transforms to fit any such scenario. Now snakecase and camelCase options come built in as well ;)

lucas-rudd commented 6 years ago

@mxl how would you handle the case where the object looks like so?

{
  first: {
    second: "hello",
    fOurth: "foo"
  }
}

Now you have the key-value pair

firstFOurth: "foo"

How will this be unflattened? Or, how will you determine whether or not this is first.fOurth or first.f.ourth?