Skip to content

Commit

Permalink
feat!: improve type safety
Browse files Browse the repository at this point in the history
The functions now use generics for improved type safety.

BREAKING CHANGE:

The return types are now more specific. This may break existing code that relies on less strict typing, but should not affect most users. To preserve the prior behavior, you can cast the return value to `string` via a type assertion.
  • Loading branch information
Planeshifter committed Jul 13, 2024
1 parent f766a56 commit 67858b3
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

// TypeScript Version: 4.1

type Capitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;

/**
* Capitalizes the first character in a string.
*
Expand All @@ -40,7 +42,7 @@
* var out = capitalize( 'Hidden Treasures' );
* // returns 'Hidden Treasures'
*/
declare function capitalize( str: string ): string;
declare function capitalize<S extends string>( str: S ): Capitalize<S>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import capitalize = require( './index' );

// The function returns a string...
{
capitalize( 'abc' ); // $ExpectType string
capitalize( 'abc' ); // $ExpectType "Abc"
capitalize( 'beep boop' ); // $ExpectType "Beep boop"
capitalize( 'a' ); // $ExpectType "A"
capitalize( 'A' ); // $ExpectType "A"
capitalize( 'abc' as string ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* var str = lowercase( 'bEEp' );
* // returns 'beep'
*/
declare function lowercase( str: string ): string;
declare function lowercase<S extends string>( str: S ): Lowercase<S>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import lowercase = require( './index' );

// The function returns a string...
{
lowercase( 'abc' ); // $ExpectType string
lowercase( 'ABC' ); // $ExpectType "abc"
lowercase( 'Beep BOOP' ); // $ExpectType "beep boop"
lowercase( 'abc' as string ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

// TypeScript Version: 4.1

type Uncapitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;

/**
* Uncapitalizes the first character of a string.
*
Expand All @@ -40,7 +42,7 @@
* var out = uncapitalize( 'Hidden Treasures' );
* // returns 'hidden Treasures'
*/
declare function uncapitalize( str: string ): string;
declare function uncapitalize<S extends string>( str: S ): Uncapitalize<S>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import uncapitalize = require( './index' );

// The function returns a string...
{
uncapitalize( 'Last man standing' ); // $ExpectType string
uncapitalize( 'Last man standing' ); // $ExpectType "last man standing"
uncapitalize( 'Hello World!' ); // $ExpectType "hello World!"
uncapitalize( 'BeepBoop' ); // $ExpectType "beepBoop"
uncapitalize( 'A' ); // $ExpectType "a"
uncapitalize( 'foo' as string ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* var str = uppercase( 'bEEp' );
* // returns 'BEEP'
*/
declare function uppercase( str: string ): string;
declare function uppercase<S extends string>( str: S ): Uppercase<S>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import uppercase = require( './index' );

// The function returns a string...
{
uppercase( 'Last man standing' ); // $ExpectType string
uppercase( 'Last man standing' ); // $ExpectType "LAST MAN STANDING"
uppercase( 'Hello World!' ); // $ExpectType "HELLO WORLD!"
uppercase( 'beep' ); // $ExpectType "BEEP"
uppercase( 'BOOP' ); // $ExpectType "BOOP"
uppercase( 'foo' as string ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

// TypeScript Version: 4.1

type Capitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;

/**
* Capitalizes the first character in a string.
*
Expand All @@ -40,7 +42,7 @@
* var out = capitalize( 'Hidden Treasures' );
* // returns 'Hidden Treasures'
*/
declare function capitalize( str: string ): string;
declare function capitalize<S extends string>( str: S ): Capitalize<S>;


// EXPORTS //
Expand Down
8 changes: 6 additions & 2 deletions lib/node_modules/@stdlib/string/capitalize/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ import capitalize = require( './index' );

// TESTS //

// The function returns a string...
// The function returns a capitalized string...
{
capitalize( 'abc' ); // $ExpectType string
capitalize( 'abc' ); // $ExpectType "Abc"
capitalize( 'beep boop' ); // $ExpectType "Beep boop"
capitalize( 'a' ); // $ExpectType "A"
capitalize( 'A' ); // $ExpectType "A"
capitalize( 'abc' as string ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* var str = lowercase( 'bEEp' );
* // returns 'beep'
*/
declare function lowercase( str: string ): string;
declare function lowercase<S extends string>( str: S ): Lowercase<S>;


// EXPORTS //
Expand Down
4 changes: 3 additions & 1 deletion lib/node_modules/@stdlib/string/lowercase/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import lowercase = require( './index' );

// The function returns a string...
{
lowercase( 'abc' ); // $ExpectType string
lowercase( 'ABC' ); // $ExpectType "abc"
lowercase( 'Beep BOOP' ); // $ExpectType "beep boop"
lowercase( 'abc' as string ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

// TypeScript Version: 4.1

type Uncapitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;

/**
* Uncapitalizes the first character of a string.
*
Expand All @@ -40,7 +42,7 @@
* var out = uncapitalize( 'Hidden Treasures' );
* // returns 'hidden Treasures'
*/
declare function uncapitalize( str: string ): string;
declare function uncapitalize<S extends string>( str: S ): Uncapitalize<S>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import uncapitalize = require( './index' );

// The function returns a string...
{
uncapitalize( 'Last man standing' ); // $ExpectType string
uncapitalize( 'Last man standing' ); // $ExpectType "last man standing"
uncapitalize( 'Hello World!' ); // $ExpectType "hello World!"
uncapitalize( 'BeepBoop' ); // $ExpectType "beepBoop"
uncapitalize( 'A' ); // $ExpectType "a"
uncapitalize( 'foo' as string ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* var str = uppercase( 'bEEp' );
* // returns 'BEEP'
*/
declare function uppercase( str: string ): string;
declare function uppercase<S extends string>( str: S ): Uppercase<S>;


// EXPORTS //
Expand Down
6 changes: 5 additions & 1 deletion lib/node_modules/@stdlib/string/uppercase/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import uppercase = require( './index' );

// The function returns a string...
{
uppercase( 'Last man standing' ); // $ExpectType string
uppercase( 'Last man standing' ); // $ExpectType "LAST MAN STANDING"
uppercase( 'Hello World!' ); // $ExpectType "HELLO WORLD!"
uppercase( 'beep' ); // $ExpectType "BEEP"
uppercase( 'BOOP' ); // $ExpectType "BOOP"
uppercase( 'foo' as string ); // $ExpectType string
}

// The compiler throws an error if the function is provided a value other than a string...
Expand Down

1 comment on commit 67858b3

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Coverage Report

Package Statements Branches Functions Lines
string/base/capitalize $\color{green}98/98$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}98/98$
$\color{green}+100.00\%$
string/base/lowercase $\color{green}80/80$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}80/80$
$\color{green}+100.00\%$
string/base/uncapitalize $\color{green}98/98$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}98/98$
$\color{green}+100.00\%$
string/base/uppercase $\color{green}80/80$
$\color{green}+100.00\%$
$\color{green}3/3$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}80/80$
$\color{green}+100.00\%$
string/capitalize $\color{green}106/106$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}106/106$
$\color{green}+100.00\%$
string/lowercase $\color{green}91/91$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}91/91$
$\color{green}+100.00\%$
string/uncapitalize $\color{green}106/106$
$\color{green}+100.00\%$
$\color{green}5/5$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}106/106$
$\color{green}+100.00\%$
string/uppercase $\color{green}90/90$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}90/90$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.