@ztimson/utils / string
string
Type Aliases
ParsedUrl
ParsedUrl:
object
Parts of a URL
Type declaration
domain
domain:
string
fragment?
optional
fragment:string
host
host:
string
path?
optional
path:string
port?
optional
port:number
protocol?
optional
protocol:string
query?
optional
query:object
Index Signature
[`name`: `string
subdomain?
optional
subdomain:string
Defined in
src/string.ts:182
Functions
formatBytes()
formatBytes(
bytes
,decimals
):string
Convert number of bytes into a human-readable size
Parameters
• bytes: number
Number of bytes
• decimals: number
= 2
Decimal places to preserve
Returns
string
Formated size
Defined in
src/string.ts:30
formatPhoneNumber()
formatPhoneNumber(
number
):string
Extract numbers from a string & create a formated phone number: +1 (123) 456-7890
Parameters
• number: string
String that will be parsed for numbers
Returns
string
Formated phone number
Defined in
src/string.ts:44
insertAt()
insertAt(
target
,str
,index
):String
Insert a string into another string at a given position
Parameters
• target: string
Parent string you want to modify
• str: string
Value that will be injected to parent
• index: number
Position to inject string at
Returns
String
- New string
Example
console.log(insertAt('Hello world!', ' glorious', 5);
// Output: Hello glorious world!
Defined in
src/string.ts:64
matchAll()
matchAll(
value
,regex
):RegExpExecArray
[]
Find all substrings that match a given pattern.
Roughly based on String.prototype.matchAll
.
Parameters
• value: string
String to search.
• regex: string
| RegExp
Regular expression to match.
Returns
RegExpExecArray
[]
Found matches.
Defined in
src/string.ts:162
md5()
md5(
d
):string
Create MD5 hash using native javascript
Parameters
• d: string
String to hash
Returns
string
Hashed string
Defined in
src/string.ts:228
pad()
pad(
text
,length
,char
,start
):any
Add padding to string
Parameters
• text: any
Text that will be padded
• length: number
Target length
• char: string
= ' '
Character to use as padding, defaults to space
• start: boolean
= true
Will pad start of text if true, or the end if false
Returns
any
Padded string
Example
const now = new Date();
const padded = now.getHours() + ':' + pad(now.getMinutes(), 2, '0');
console.log(padded); // Output: "2:05"
Deprecated
Please use String.padStart
& String.padEnd
Defined in
src/string.ts:85
parseUrl()
parseUrl(
url
):ParsedUrl
Break a URL string into its parts for easy parsing
Parameters
• url: string
URL string that will be parsed
Returns
Parts of URL
Defined in
src/string.ts:199
randomHex()
randomHex(
length
):string
Generate a random hexadecimal value
Parameters
• length: number
Number of hexadecimal place values
Returns
string
Hexadecimal number as a string
Defined in
src/string.ts:96
randomString()
randomString(
length
,pool
):string
Generate a string of random characters.
Parameters
• length: number
length of generated string
• pool: string
= CHAR_LIST
character pool to generate string from
Returns
string
generated string
Example
const random = randomString();
const randomByte = randomString(8, "01")
Defined in
src/string.ts:113
randomStringBuilder()
randomStringBuilder(
length
,letters
,numbers
,symbols
):string
Generate a random string with fine control over letters, numbers & symbols
Parameters
• length: number
length of generated string
• letters: boolean
= false
Add letters to pool
• numbers: boolean
= false
Add numbers to pool
• symbols: boolean
= false
Add symbols to pool
Returns
string
generated string
Example
const randomLetter = randomString(1, true);
const randomChar = randomString(1, true, true, true);
Defined in
src/string.ts:135
validateEmail()
validateEmail(
boolean
Check if email is valid
Parameters
• email: string
Target
Returns
boolean
- Follows format
Defined in
src/string.ts:249