Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:47:39+00:00 2026-05-30T02:47:39+00:00

Is there a JavaScript module that makes it easy to map arguments to parameters?

  • 0

Is there a JavaScript module that makes it easy to map arguments to parameters? Here is how I envision it working:

var arguments = assignArguments(arguments, ‘p1’, ‘p2’, [{‘p3’: 0},
‘p4’], {‘p5’: ‘unknown’});

Within a function, you would call this to generate an object that associated a parameter with an argument. Parameters defined within an array or inline object would be considered optional, where inline objects would permit assigning default values. All other parameters are considered “required”.

Here are some sample inputs/outpus:

foo(1): { p1: 1, p3: 0, p5: 'unknown' } // no p2 (aka undefined) 
foo(1, 2): { p1: 1, p2: 2, p3: 0, p5: 'unknown' } 
foo(1, 2, 3): { p1: 1, p2: 2, p3: 0, p4: 3, p5: 'unknown' } 
foo(1, 2, 3, 4): { p1: 1, p2: 2, p3: 3, p4: 4, p5: 'unknown' } 
foo(1, 2, 3, 4, 5): { p1: 1, p2: 2, p3: 3, p4: 4, p5: 5 }

I am hoping a library like this already exists. This logic gets repeated a lot and I want to eliminate it if possible.

Is anyone aware of a library like this? If not, can someone send me down the right path for implementing one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:47:41+00:00Added an answer on May 30, 2026 at 2:47 am

    I went ahead and thought of a way of doing this myself. It involves parsing an object graph made up of strings, arrays and objects. It’s quite long, but it works well.

    function assignArguments(values, expression) {              
        // determine how many arguments are needed for each parameter
        // grab all of the defaults, too
        var needed = 1;
        var argsNeeded = {};
        var defaults = {};
        var queue = [expression];               
        for (var queueIndex = 0; queueIndex < queue.length; ++queueIndex) {
            var node = queue[queueIndex];
            if (typeof node === 'string') {
                // the node is a required parameter
                argsNeeded[node] = needed;
                ++needed;
            } else if (node instanceof Array) {
                // the node is a list of parameters
                for (var index = 0; index !== node.length; ++index) {
                    queue.push(node[index]);
                }
            } else {
                // the node is a list of optional parameters with defaults
                // make sure there isn't more than one parameter
                var count = 0;
                for (var key in node) {
                    if (node.hasOwnProperty(key)) {
                        if (count !== 0) {
                            throw new Error('A default parameter list had more than one value.');
                        }
                        defaults[key] = node[key];
                        queue.push(key);
                        ++count;
                    }
                }
            }
        }
    
        // determine the order that the parameters appear
        var parameters = [];
        var stack = [expression];
        while (stack.length > 0) {
            var node = stack.pop();
            if (typeof node === 'string') {
                // the node is a required parameter
                parameters.push(node);
            } else if (node instanceof Array) {
                // the node is a list of parameters
                var index = node.length;
                while (index !== 0) {
                    --index;
                    stack.push(node[index]);
                }
            } else {
                // the node is an optional parameter with defaults
                // we'll check for multiple parameters
                var count = 0;
                for (var key in node) {
                    if (node.hasOwnProperty(key)) {
                        if (count > 0) {
                            throw new Error('A default parameter list had more than one value.');
                        }
                        stack.push(key);
                        ++count;
                    }
                }
            }
        }
    
        var result = {};
        var valueIndex = 0;
        for (var index = 0; index !== parameters.length; ++index) {
            var parameter = parameters[index];
            var needed = argsNeeded[parameter];
            if (needed > values.length) {
                if (parameter in defaults) {
                    result[parameter] = defaults[parameter];
                }
            } else {
                result[parameter] = values[valueIndex];
                ++valueIndex;
            }
        }
    
        return result;
    }
    

    And here is an example using it.

    function foo() {
        var params = assignArguments(arguments, ['p1', 'p2', [{p3:0}, 'p4'], {p5: 'unknown'}]);
        return params;
    }
    
    console.log(foo(1));
    console.log(foo(1, 2));
    console.log(foo(1, 2, 3));
    console.log(foo(1, 2, 3, 4));
    console.log(foo(1, 2, 3, 4, 5));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any javascript library that provides me with a custom event as soon
Is there any javascript algo that i can used to check for a website
Is there any javascript function that can encrypt data: For example i want to
I'm wondering is there a JavaScript library available that would allow me to generate
I have a CMS with a form-module, which generates an awful markup, that makes
Is there a JQuery/Javascript module for colouring code? I've got a website where I
I am working on a javascript framework. I have several independent scripts that look
Is there a way to call flex module from Javascript? The context: I'm running
I'm working on a JavaScript application that has to perform two separate checks via
In javascript there's this sweet, sweet function window.setTimeout( func, 1000 ) ; which will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.