Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
24 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
/* eslint @typescript-eslint/explicit-module-boundary-types: 0 */ | ||
type StringInABox<T> = T extends "" | ||
? [] | ||
: string extends T | ||
? [] | [string] | ||
: T extends string | ||
? [T] | ||
: T; | ||
|
||
// If a non-empty string is given, put it into an array. | ||
// If an empty string is given, return an empty array. | ||
// Bypass everything else. | ||
|
||
// type signature overloading | ||
function arrayiffy(something: string): [string]; | ||
function arrayiffy(something: string): []; | ||
function arrayiffy(something: any): any { | ||
if (typeof something === "string") { | ||
if (something.length) { | ||
return [something]; | ||
} | ||
return []; | ||
} | ||
return something; | ||
function arrayiffy<T>(something: T): StringInABox<T>; | ||
function arrayiffy<T>(something: T): [] | [string] | T { | ||
if (typeof something !== "string") return something; | ||
if (something.length) return [something]; | ||
return []; | ||
} | ||
|
||
export { arrayiffy }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
declare function arrayiffy(something: string): [string]; | ||
declare function arrayiffy(something: string): []; | ||
declare type StringInABox<T> = T extends "" | ||
? [] | ||
: string extends T | ||
? [] | [string] | ||
: T extends string | ||
? [T] | ||
: T; | ||
declare function arrayiffy<T>(something: T): StringInABox<T>; | ||
|
||
export { arrayiffy }; |