Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm about to design an service which handles URL-Parameters in AngularJS. Currently I have two different kinds of parameters.

Single valued

&param=myValue

Multi valued

&param=lorem,ipsum,dolor

I'm not sure about to implement a setter for both, single and multi valued parameters…

/**
 * Set parameter
 *
 * @param {string}         name
 * @param {string|array}   values
 * @param {boolean}        multi
 */
setParam: function(name, value, multi) { }

…or if each type of parameter should get its own setter?

/**
 * Set single valued parameter
 *
 * @param {string}   name
 * @param {string}   value
 */
setSingleValuedParam: function(name, value) { }

/**
 * Set multi valued parameter
 *
 * @param {string}   name
 * @param {array}    values
 */
setMultiValuedParam: function(name, array) { }

Please note: This pseudo-code and does not work!

share|improve this question

migrated from programmers.stackexchange.com Jul 3 at 4:08

This question came from our site for professional programmers interested in conceptual questions about software development.

1 Answer 1

up vote 4 down vote accepted

Even the single one that you show is technically an array with a single element. So I would just implement the multi-valued one.

/**
 * Set parameters
 *
 * @param {string}   name
 * @param {array}    values
 */
setMultiValuedParam: function(name, array) { }

When the query comes, split it by , and there will be 0 or more parameters in the array.

FYI: Definitely include a possibility of 0 parameters, because even if your program does not call with 0, since this is a query parameter, someone else could just hit the URL directly.

share|improve this answer
    
Thank you for your suggestion. I think this would be the better solution. –  gearsdigital Jun 21 at 18:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.