Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL array param with default empty value is left undefined #2222

Closed
AdamSelene opened this issue Sep 2, 2015 · 4 comments
Closed

URL array param with default empty value is left undefined #2222

AdamSelene opened this issue Sep 2, 2015 · 4 comments
Assignees
Milestone

Comments

@AdamSelene
Copy link

I'm having an issue with an array state parameter with a default empty value:

  • when the param is not declared in the URL pattern (param4 in the jsbin below), the default value is indeed []
  • when the param is declared in the URL pattern (param3 and param5) AND when it's absent from the URL (allowed since it's supposed to be optional), then it's initialized to 'undefined' (even worse than being undefined: the parameter is present in the params object but has an undefined value, which prevents me from easily using angular.extend or angular.merge, for instance)

See https://jsbin.com/bebeheciju/edit?html,console,output for example

The problem comes from the arrayUnwrap function in the Type definition, which specifically returns 'undefined' for empty arrays.

@christopherthielen
Copy link
Contributor

This does appear to be a bug

@driver-by
Copy link

I've found how to fix that:

// src/urlMatcherFactory.js
function arrayUnwrap(val) {
      switch(val.length) {
        // Was: 
        //case 0: return undefined;
        // Now:
        case 0: return mode === true ? [] : undefined;
        case 1: return mode === "auto" ? val[0] : val;
        default: return val;
      }
    }

But there is one test:
expect(m.format({ param1: [] })).toBe("/foo");
with the comment before the section
// empty array [] is treated like "undefined"

So if we need this behaviour, looks like we can't fix that bug.

@christopherthielen
Copy link
Contributor

What happens if you specify a default value of []?

@driver-by
Copy link

@christopherthielen, I didn't get the point about default value

ExpFront pushed a commit to ExpFront/ui-router that referenced this issue Jun 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants