0

The following function is meant to check to see if a custom Date widget (javascript) is empty or not. Problem is, there are many variations of this widget where M/D/Y fields display, or it could be M/D or, M/Y.

Of course, I could hard code all the combinations as if checks, but is there a better way of saying "there are 3 possible nodes, that might have values...if x out of 3 nodes exist AND they all have values, set empty to false."

checkIfEmpty: function () {
    var empty = true;

    var mNode = this.getNode('month');
    var month = mNode ? mNode.value : null;

    var dNode = this.getNode('day');
    var day = dNode ? dNode.value : null;

    var yNode = this.getNode('year');
    var year = yNode ? yNode.value : null;

    if (month && day && year) {
        empty = false;
    }

    return empty;
}
7
  • 2
    Not sure I understand your question, wouldn't switching the && to an | | solve your issue? Commented Sep 22, 2015 at 18:06
  • @Siva well sure - there would be many different variations M+Y, M+D, Y+D, Y+M, D+M, D+Y+M. It would be a rather loaded if statement :) Commented Sep 22, 2015 at 18:09
  • You need at less 2 of the 3 values ? Commented Sep 22, 2015 at 18:11
  • @hexaheart The concept is that, the date field can consist of as little as 1 field, and as many as 3, in varying combinations. I need to check that for all of the fields that do exist, their .value is not empty. Commented Sep 22, 2015 at 18:14
  • Can't you check if the vars are not null ? Commented Sep 22, 2015 at 18:16

4 Answers 4

1
checkIfEmpty: function () {
    var empty = true;
    var dateParts = [];
    var mNode = this.getNode('month');
    if(mNode && mNode.value){
        dateParts.push('month');
    }

    var dNode = this.getNode('day');
    if(dNode && dNode.value){
        dateParts.push('day');
    }


    var yNode = this.getNode('year');
    if(yNode && yNode.value){
        dateParts.push('year');
    }

    if (dateParts.length) {
        empty = false;
    }

    return empty;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This was along the lines of what I was thinking. So far the most optimal solution I've seen.
0

You can add to see if the node does not exist

if ( (!mNode || month) && (!dNode || day) && (!yNode || year) ) {

2 Comments

but if a node is null the associate month / day / year variable will be null. At the end they will return true every time ?
If the node does not exist it will return true for that portion of the if check. If the node exists, it checks the month/day/year value.
0
checkIfEmpty: function () {
    var empty = false;

    var mNode = this.getNode('month');
    if(mNode && !mNode.value) {
        empty = true;
    }

    var dNode = this.getNode('day');
    if(dNode && !dNode.value) {
        empty = true;
    }

    var yNode = this.getNode('year');
    if(yNode && !yNode.value) {
        empty = true;
    }

    return empty;
}

Trying to solve my own question - so far, this is the most efficient way of achieving what I am trying to do. Anyone, suggestions on how to make it even more efficient?

1 Comment

if mNode or dNode or yNode is empty then empty will be false.
0

If the value property exists for all valid nodes then:

if(mNode && dNode && yNode){
   empty = false;
}

Otherwise:

if(mNode && mNode.value && dNode && dNode.value && yNode && yNode.value){
   empty = false;
}

I am not sure if I followed, but if you need that at least one to be true so empty is false then:

if(mNode || dNode || yNode) {
   empty = false;
}

Again, if the value property is not standard for all nodes:

if((mNode && mNode.value) || (dNode && dNode.value) || (yNode && yNode.value)){
   empty = false;
}

I think it's clearer if you think about it this way:

If (node && node.value) returns a truthy value then the date property exists otherwise the date property doesn't exist.

Comments

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.