| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Summary: The `hasSlash` method uses `path.substr(path, path.length - 1)` to remove the last character from `path`. Clearly, the first parameter is suspect; it should be `0`. The code works as written, but only very accidentally: the first parameter is coerced by `ToNumber` to `NaN`, which is then coerced by `ToInteger` to `+0`, per [the spec][1]. [1]: https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.substr Test Plan: Reading the spec should be sufficient. To verify in the Real World: ```js const path = "has-slash-but-does-not-need-slash/" const a = path.substr(path, path.length - 1); const b = path.substr(0, path.length - 1); console.log(a === b); // true console.log(a); // has-slash-but-does-not-need-slash ``` wchargin-branch: ensureslash-accidental-coercion
|
Wow, excellent catch and analysis. Thanks! |
Sorry, something went wrong.
|
You're welcome! Thanks for the quick response. |
Sorry, something went wrong.
|
(Thanks to @decentralion for spotting this while adding Flow types to some of our code. :-) ) |
Sorry, something went wrong.
Summary: The `hasSlash` method uses `path.substr(path, path.length - 1)` to remove the last character from `path`. Clearly, the first parameter is suspect; it should be `0`. The code works as written, but only very accidentally: the first parameter is coerced by `ToNumber` to `NaN`, which is then coerced by `ToInteger` to `+0`, per [the spec][1]. [1]: https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.substr Test Plan: Reading the spec should be sufficient. To verify in the Real World: ```js const path = "has-slash-but-does-not-need-slash/" const a = path.substr(path, path.length - 1); const b = path.substr(0, path.length - 1); console.log(a === b); // true console.log(a); // has-slash-but-does-not-need-slash ``` wchargin-branch: ensureslash-accidental-coercion
| Back | FazBrowse Home | New Git URL |
Summary:
The hasSlash method uses path.substr(path, path.length - 1) to
remove the last character from path. Clearly, the first parameter is
suspect; it should be 0. The code works as written, but only very
accidentally: the first parameter is coerced by ToNumber to NaN,
which is then coerced by ToInteger to +0, per the spec.
Test Plan:
Reading the spec should be sufficient. To verify in the Real World:
wchargin-branch: ensureslash-accidental-coercion