0

I'm rewriting my old table sort library. In the library, I have this method which gets some attributes from the table:

<table data-sort-cols="" data-sort-orders="">

getSortInfo : function(element) {
    switch (element.tagName) {
        case "TABLE":
            function getSortAttr(element, attr) {
                var info = element.getAttribute(attr);
                if (info != "") {
                    info = info.split(",").map(function(val) { return parseInt(val,10) });
                } else {
                    info = [];
                }
                return info;
            };
            return {
                columns : getSortAttr(element, "data-sort-cols"),
                orders  : getSortAttr(element, "data-sort-orders"),
            }

I want to shorten the getSortAttr function. Function returns the assigned value (e.g. "1,3,2" -> [1,3,2]). If there is not an assigned value to the attribute, it returns an empty array. I want to get rid of the if statement.

Is there a way to modify this line, and make it return an empty array if the string is empty ""?

info = info.split(",").map(function(val) { return parseInt(val,10) });

I've tried

// returns [NaN]
"".split(",").map(function(val) {
    return parseInt(val, 10);
})

// returns [undefined]
"".split(",").map(function(val) {
    var num = parseInt(val, 10);
    if (!isNaN(num) && isFinite(num)) {
        return num;
    }
})

but didn't work.

5
  • Til ES6, that function declaration is invalid in that position. Better move it outside of the switch statement. Commented May 19, 2016 at 23:56
  • Originally, it's outside the switch statement, declared as another method of the main object. Testing things atm. Thanks for the notice though. Commented May 20, 2016 at 0:00
  • Why do you want to get rid of the if statement? Checking the input once before the map seems better than using a filter as in the accepted answer, since that unnecessarily checks all the elements of the array. Commented May 20, 2016 at 1:49
  • @Barmar data-sort-x attributes are set at the initialization stage and getSortInfo method is called when the user clicks on a column header. data-sort-x is set to empty strings before any sorting; after a sort, it changes to "2,1" (column indexes or orders). If statement required for the first sorting. After that I thought it is redundant ... Though I'm not sure if what I'm looking for is a good practice. Commented May 20, 2016 at 11:41
  • The answer you accepted performs the test on every sort. It just tests in a different way. Commented May 20, 2016 at 15:36

2 Answers 2

1

The following function will check if info == "", if info == "", it will then return an empty array, if not, it will split the info string and map it. This is called a shorthand if statement.

info = (info == "") ? [] : info.split(",").map(function(val) { return parseInt(val,10) });

You can also add more conditions to the shorthand condition, e.g:

(info == "" || info != undefined || info != null)

If the above info == "" doesn't work, this might mean that info is undefined. To combat this we can use the following statement:

(typeof(info) != undefined)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use

function getSortAttr(element, attr) {
   return (element.getAttribute(attr) || "").split(",").filter(Boolean).map(Number);
}

to filter out empty strings from the array before mapping them to numbers.

2 Comments

This seems to do the job. I've omitted the || part and used element.getAttribute(attr).split(",").filter(Boolean).map(Number), because the attribute is already set to an empty string before any function call. It's dynamically modifed later.
OK, I wasn't sure whether the attributes always existed. Better be safe than sorry :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.