Hey Lucky. Welcome to Node!

I'm not sure what language you were using before trying JavaScript but one 
thing I want to mention is that JavaScript is very much non-opinionated. It 
doesn't matter if you want to use a functional design paradigm or an object 
oriented paradigm. The reason I mention this is because how you solve this 
problem will probably depend on how you want to design your applications. I 
personally prefer OOP so I will start with solving this problem that way.

To start lets create a class for your request and export it so you can use 
it elsewhere.

SomeRequest.js

module.exports = class SomeRequest {
  constructor (requestObj) {
    this.uuid = requestObj.id
    this.username = requestObj.name
  }
}

Now to use this class you would do the following

someOtherFile.js

const SomeRequest = require('./SomeRequest.js')
const request = new SomeRequest(originalRequest)

The request object should be converted as you like and is ready to use. As 
someone from a Java shop this makes the most sense to some of my coworkers 
since it looks more like what they are used to.

Now if you want to do it functionally like you are I wouldn't bother using 
a JSON templater. It's another dependency that you don't control and I 
don't think it does enough to justify it's use. Instead I would try 
something like the following.

convertSomeRequest.js

module.exports = (requestObj) => {
  return {
    uuid: requestObj.id,
    username: requestObj.name
  }
}

And to use it in another file...

const convertSomeRequest = require('./convertSomeRequest.js')
const request = convertSomeRequest(originalRequest)


One last thing. Looking at this post it seems you are trying to work with 
raw JSON all the time. It's easier to work with data if you are dealing 
with an actual object vs a string. If your using a framework like express 
or koa then the JSON should automatically be converted to an object and 
then back to JSON when you respond. If not you will need to convert the 
responses yourself. Javascript has a nice JSON library built in to deal 
with this.

JSON.stringify(object) // converts an object to a JSON string.
JSON.parse(jsonString) // converts a JSON formatted string into an object.

This should make things easier to work with for you.

>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/e1c71158-bc71-42c4-bc8a-b64fd118166f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to