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

Hello, when I was looking at this code, I found that there are repeated judgments and lax writing #11

Open
lively-krishnan opened this issue Feb 5, 2022 · 2 comments

Comments

@lively-krishnan
Copy link

Hello, when I was looking at this code, I found that there are repeated judgments and lax writing

I made the following adjustments

I hope you can correct me if I understand wrong, thanks

function replace(input, view) {
  // optimization to avoid regex calls (indexOf is strictly faster)
  if (input.indexOf(TEMPLATE_OPEN) === -1) return input;
  // var result;
 //  var replaced =
 return input.replace(REGEX, function(original, path) {
    var value = extractValue(path, view);
    
     // undefined === value , No exception will be thrown. It will throw an exception only if the variable is not declared
     // Typeof value == undefined is better
    //  if (undefined === value || null === value) {
    //  return original;
    // }
   
    // if (typeof value === 'object') {
    // result = value;
    // return;
    //}

    // return value;
  });
  // return (undefined !== result) ? result : replaced;
}

this is my adjustment

function replace(input, view) {
 if (input.indexOf(TEMPLATE_OPEN) === -1) return input;
 return input.replace(REGEX, function(original, path) {
    var value = extractValue(path, view)
     if (typeof value == 'undefined' || null === value)  return original;
     return value;
  })
}
@lively-krishnan
Copy link
Author

lively-krishnan commented Feb 5, 2022

Oh, no, I thought about this. I had a problem with that statement, so LET me rewrite it

function replace(input, view) {
 if (input.indexOf(TEMPLATE_OPEN) === -1) return input;
 return input.replace(REGEX, function(original, path) {
    var value = extractValue(path, view)
     if (typeof value === 'undefined' || value === null) {
      console.warn(`No ${path} was found for value`)
      return original
    }
    if (typeof value === 'object') {
      console.warn(`No ${path} was found for value`)
      return original
    }
    return value
  })
}

@lively-krishnan
Copy link
Author

I also noticed that the code was not rigorous,Eslint considers it noncanonical
Unexpected assignment within a 'while' statement.
no-cond-assign

while (view &&  (part = parts.shift()))

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

No branches or pull requests

1 participant