Node.js: dedelimit
Scott Ivey -> http://www.scivey.net
Takes an object with flattened key-value pairs, like this:
"user.name.first": "Tim" "user.name.last": "Generic" "user.age": 27 "user.messages.latest": "Pick up the pace, Tim."
And builds an object with the nested structure indicated by the delimited key order, like this:
user: name: first: "Tim" last: "Generic" age: 27 messages: latest: "Pick up the pace, Tim."
Installation
npm install dedelimit
Usage
var dedelimit = dedelimit; var flattenedKeys = "user.name.first": "Tim" "user.name.last": "Generic" "user.age": 27 "user.messages.latest": "Pick up the pace, Tim."; ;
dedelimit
has an async interface, but the implementation is basically synchronous -- the call is returned on process.nextTick
. It's async because a future version may split the task into several async chunks if there are a large number of keys. So far that doesn't seem necessary.
dedelimit
can also take an options object as its second argument, which currently provides a delimiter
attribute for customizing the delimiter used to split the original keys into sequences. The delimiter defaults to a single period ("."). If an options object is passed, dedelimit
expects a callback as its third argument.
var dedelimit = dedelimit; var flattenedKeys = "user,name,first": "Tim" "user,name,last": "Generic" "user,age": 27 "user,messages,latest": "Pick up the pace, Tim."; ;
The module also exports insertDelimitedKey
, the internal method used for handling each key sequence. This function takes a pre-split array of keys, which is assumed to represent the ordering from a flattened string in an object like flattenedKeys
, and the value to assign to the terminal key in the sequence. Its first argument is an object reference, which will be mutated. dedelimit
uses this to build a reconstituted object one (nested) key-value pair at a time.
If a sequence of keys needs to be calculated in a way too complex to be handled by passing a different delimiter, custom key-splitting code can be paired with insertDelimitedKey
to provide the same functionality as dedelimit
.
If you just need an object rebuilt out of delimited keys, use dedelimit
.
var insertDelimitedKey = insertDelimitedKey; var _objectRef = {}; ; console; // output:// {// user: {// name: {// first: "Gary"// }// }// }//
API
-
dedelimit(flattenedObject[, options], callback)
flattenedObject
is a one-level object with nested structure indicated by delimiters in its keys, e.g."user.name": "name"
for{user: { name: "name"} }
.options
takes one property,delimiter
, which is used to split flattened keys into the intermediate form used to rebuild their structure. A single period,"."
, is the default delimiter.callback
has a typical(err, results)
signature.results
is the hierarchical object created fromflattenedObject
's split keys.- If
options
is omitted,callback
is assumed to be the second parameter. (i.e. if the second parameter is a function, it will be treated as the callback.)
-
insertDelimitedKey(objectRef, keyList, value)
objectRef
is a reference to a plain object which will have the keys inkeyList
added to it recursively. This object reference is mutated.keyList
is an array of pre-split keys, i.e.['user', 'name']
instead of the"user.name"
key used indedelimit
. Each subsequent key is assumed to be one level deeper, i.e.name
is a property ofuser
.value
is the value assigned to the property described by the last element ofkeyList
. In the case of the same['user', 'name']
array,value
would be assigned to thename
field ofuser
, withuser
itself a property of the top-level object.
Use Cases
This module was created to rebuild objects from delimited property lists in the name
attributes of HTML form inputs, e.g. from the following Handlebars view:
<!-- and so on -->
dedelimit
can be used to recreate objects from flattened keys on the client-side or server-side. In the most straightforward case, this form will just be serialized and passed via POST call to /api/customers
, with no further action on the client side. Recreating the original object is pointless there. If client-side models also need updating, then handling it in-browser is more important.
Used server-side with Express, parsing the form looks like this:
app; // parses POST data into req.body app;
This can easily be implemented as middleware for a group of form-handling routes.
GitHub
https://github.com/scivey/dedelimit
Contact
License
MIT