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

Serialise form data bug #2

Open
pdscopes opened this issue Jun 28, 2019 · 1 comment
Open

Serialise form data bug #2

pdscopes opened this issue Jun 28, 2019 · 1 comment
Labels

Comments

@pdscopes
Copy link
Owner

When you have a input name like foo[8.8.8.8] serialising the form data into JSON creates:

{
  "foo": {"8": {"8": {"8": {"8": "VALUE"}}}}
}```

Rather than the expected:
```json
{
  "foo": {"8.8.8.8": "VALUE"}
}```
@pdscopes pdscopes added the bug label Jun 28, 2019
@pdscopes
Copy link
Owner Author

Could be solved by using escape character \.:

    convertTo: function (name) {
        return name.replace(/\./g, '\\.').replace(/\[]/g, '.*').replace(/\[([^\]]+)]/g, '.$1');
    },
    set: function (object, key, value) {
        if (null === key) {
            return object = value;
        }

        var rev = key.split('').reverse().join('');
        var keys = rev.split(/\.(?=[^\\])/g).reverse().map(function (x) {
            return x.split('').reverse().join('');
        });
        // var keys = key.split('.');
        var temp = object;

        for (var k=0; k<keys.length-1; k++) {
            key = keys[k].replace(/\\\./g, '.');
            if (key === '*') {
                key = Object.keys(temp).length;
            }

            if (typeof temp[key] === 'undefined') {
                temp[key] = {};
            }

            temp = temp[key];
        }

        key = keys[k].replace(/\\\./g, '.');
        if (key === '*') {
            key = Object.keys(temp).length;
        }
        temp[key] = value;

        return object;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant