0

i am using the below code for comparing two values:

function CompareValue($ValueTobePresent,$ExpectedValue)
{
    var $ValueTobePresent=$ValueTobePresent.toLocaleLowerCase();
    var $ExpectedValue = $ExpectedValue.toLocaleLowerCase();
    _assertEqual($ValueTobePresent, $ExpectedValue);

}

Now my problem is if a null value is passed the function fails with the below message: Logging exception: Cannot call method "toLocaleLowerCase" of null [81 ms] Cannot call method "toLocaleLowerCase" of null.

Is there a way i can solve it out so that i can handle the null values also? Also i have around 25 comparisons to do so i want the code not to be very time consuming.

Thanks

Mrinal kumar
  • 67
  • 1
  • 1
  • 9
  • `$ValueTobePresent = ($ValueTobePresent == null) ? $ValueTobePresent : ""` I think it'll also handle the `undefined` condition. – fuyushimoya Jul 09 '15 at 09:51
  • Are you comparing any type of values or just strings? Is it possible to pass Arrays, Objects, Date objects, numbers, booleans as $ValueTobePresent and $ExpectedValue parameters? – Zlatin Zlatev Jul 09 '15 at 10:01
  • Mostly i am comparing only strings. It is just that i take values from two applications and i compare it – Mrinal kumar Jul 09 '15 at 10:14

2 Answers2

1

You could replace null values with empty strings if this works for you

function CompareValue($ValueTobePresent,$ExpectedValue)
{
    var $ValueTobePresent = ($ValueTobePresent || '').toLocaleLowerCase();
    var $ExpectedValue = ($ExpectedValue || '').toLocaleLowerCase();
    _assertEqual($ValueTobePresent, $ExpectedValue);
}
Zlatin Zlatev
  • 3,034
  • 1
  • 24
  • 32
  • Try `CompareValue(1, 1)` or `CompareValue(true, false)` – Tushar Jul 09 '15 at 09:56
  • The initial snippet does not support such calls. Also using toString() would produce results like CompareValue('null', null) CompareValue('1', 1) It really depends what OP wants, but my assumption is that we are comparing strings. – Zlatin Zlatev Jul 09 '15 at 09:58
  • `but my assumption is that we are comparing strings.` If this is the case, this question would not be present here – Tushar Jul 09 '15 at 10:00
0
  1. Remove var inside the function
  2. Use ternary operator to check if arguments passed are valid
  3. Use toString() before calling toLocaleLowerCase()

Code:

function CompareValue($ValueTobePresent, $ExpectedValue) {
    $ValueTobePresent = $ValueTobePresent ? $ValueTobePresent.toString().toLocaleLowerCase() : '';
    $ExpectedValue = $ExpectedValue ? $ExpectedValue.toString().toLocaleLowerCase() : '';
    _assertEqual($ValueTobePresent, $ExpectedValue);
}
Tushar
  • 85,780
  • 21
  • 159
  • 179