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

infra(unicorn): prefer-string-slice #2814

Draft
wants to merge 4 commits into
base: next
Choose a base branch
from

Conversation

ST-DDT
Copy link
Member

@ST-DDT ST-DDT commented Apr 13, 2024

@ST-DDT ST-DDT added p: 1-normal Nothing urgent c: infra Changes to our infrastructure or project setup labels Apr 13, 2024
@ST-DDT ST-DDT added this to the vAnytime milestone Apr 13, 2024
@ST-DDT ST-DDT requested review from a team April 13, 2024 18:12
@ST-DDT ST-DDT self-assigned this Apr 13, 2024
Copy link

netlify bot commented Apr 13, 2024

Deploy Preview for fakerjs ready!

Name Link
🔨 Latest commit 36f85bd
🔍 Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/6648f6633e649000086edd91
😎 Deploy Preview https://deploy-preview-2814.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Apr 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.96%. Comparing base (7dc8a18) to head (36f85bd).

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #2814      +/-   ##
==========================================
- Coverage   99.96%   99.96%   -0.01%     
==========================================
  Files        2977     2977              
  Lines      215422   215424       +2     
  Branches      950      952       +2     
==========================================
- Hits       215351   215342       -9     
- Misses         71       82      +11     
Files Coverage Δ
src/modules/finance/index.ts 100.00% <100.00%> (ø)
src/modules/helpers/eval.ts 96.56% <100.00%> (ø)
src/modules/helpers/index.ts 98.38% <100.00%> (+<0.01%) ⬆️
src/modules/internet/index.ts 100.00% <100.00%> (ø)
src/modules/lorem/index.ts 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

@ST-DDT
Copy link
Member Author

ST-DDT commented Apr 13, 2024

Please pay extra attention when reviewing the files changed by f48dfe4 because I had to convert them by hand and I don't know why.

@xDivisionByZerox
Copy link
Member

From the docs:

String#substr() and String#substring() are the two lesser known legacy ways to slice a string.

Have to disagree on the String#substring part. Without having prove of it, I would say that most JS developers would know String#substring while being unaware of String#slice 🤔

@ST-DDT
Copy link
Member Author

ST-DDT commented Apr 13, 2024

I can also disable the rule permanently.

@xDivisionByZerox
Copy link
Member

While I have some doubts about the previously explained statement, this rule would at least unify the way string slicing would be performed in the project. This might be a DX improvement (for faker developers) in itself because one has not wondered why X is used in one place while Y is used in another.

@Shinigami92
Copy link
Member

Shinigami92 commented Apr 15, 2024

My personal rules:

  • never use substr 🙅 its the most deprecated one
  • use substring in specific cases (as far as I know it has some specific behavior, but I would need always to look these up)
  • mostly use slice
    • pros: it is well known, acts like for arrays so some can imagine exactly how it works by thinking that each char is just an array element, and some can use shortways like -1 to address elements from the end
    • cons: in minor cases it is confusing if there is currently a string or an array, however in todays world using TypeScript, this is not a problem anymore as you have highlighting and code tooltips

Examples:

- filePath.substring(pathLocales.length + 1, filePath.length - 3)
+ filePath.slice(pathLocales.length + 1, -3)

this is a really good change, and I like it

- fileContent.substring(0, compareIndex)
+ fileContent.slice(0, Math.max(0, compareIndex))

this is not so good, because it complexifies the code by using Math.max and some need to more think about what's going on
-> Looks like the thing is that substring(_, negativeValue) results always in empty string.

TBH I personally like use substring for such cases then... 👀
Does the lint-rule has some options to configure it?

@matthewmayer
Copy link
Contributor

This only really makes sense if we manually go through cases like this and remove the ugly Math.max in places where we know compareIndex must be positive.

fileContent.slice(0, Math.max(0, compareIndex))

@ST-DDT ST-DDT marked this pull request as draft May 18, 2024 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: infra Changes to our infrastructure or project setup p: 1-normal Nothing urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants