Skip to content

Conversation

@effigies
Copy link
Contributor

Was alerted while building a project using cliffy:

    https://deno.land/x/[email protected]/command/help/_help_generator.ts:354:59:
      354 │   return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
          ╵                                                            ~~

  The left operand of the "??" operator here will never be null or undefined, so it will always be
  returned. This usually indicates a bug in your code:

    https://deno.land/x/[email protected]/command/help/_help_generator.ts:354:9:
      354 │   return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
          ╵          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

for the input "", the return value is already "". null and undefined both raise TypeErrors.

Was alerted while building a project using cliffy:

```
    https://deno.land/x/[email protected]/command/help/_help_generator.ts:354:59:
      354 │   return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
          ╵                                                            ~~

  The left operand of the "??" operator here will never be null or undefined, so it will always be
  returned. This usually indicates a bug in your code:

    https://deno.land/x/[email protected]/command/help/_help_generator.ts:354:9:
      354 │   return string?.charAt(0).toUpperCase() + string.slice(1) ?? "";
          ╵          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

for the input `""`, the return value is already `""`. `null` and `undefined` both raise `TypeError`s.
@effigies
Copy link
Contributor Author

It looks like, in d3c2fa1, you went from

function capitalize( string: string ): string {
    if ( !string ) {
        return '';
    }
    return string.charAt( 0 ).toUpperCase() + string.slice( 1 );
}

to

function capitalize( string: string ): string {
    return string?.charAt( 0 ).toUpperCase() + string.slice( 1 ) ?? '';
}

I suppose you were previously getting capitalize(null) == "" and capitalize(undefined) == "" before, but apparently the TypeError that now results hasn't been a problem in the last 4 years.

Copy link
Owner

@c4spar c4spar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx @effigies! LGTM!

@c4spar c4spar merged commit da5d7b2 into c4spar:main Jun 12, 2024
@effigies effigies deleted the patch-1 branch June 12, 2024 20:16
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

Successfully merging this pull request may close these issues.

2 participants