coerce values to string in str prelude functions
This commit is contained in:
parent
afaedeea23
commit
fd3c5da59b
|
|
@ -1,37 +1,37 @@
|
||||||
// strings
|
// strings
|
||||||
export const str = {
|
export const str = {
|
||||||
join: (arr: string[], sep: string = ',') => arr.join(sep),
|
join: (arr: string[], sep: string = ',') => arr.join(sep),
|
||||||
split: (str: string, sep: string = ',') => str.split(sep),
|
split: (str: string, sep: string = ',') => String(str ?? '').split(sep),
|
||||||
'to-upper': (str: string) => str.toUpperCase(),
|
'to-upper': (str: string) => String(str ?? '').toUpperCase(),
|
||||||
'to-lower': (str: string) => str.toLowerCase(),
|
'to-lower': (str: string) => String(str ?? '').toLowerCase(),
|
||||||
trim: (str: string) => str.trim(),
|
trim: (str: string) => String(str ?? '').trim(),
|
||||||
|
|
||||||
// predicates
|
// predicates
|
||||||
'starts-with?': (str: string, prefix: string) => str.startsWith(prefix),
|
'starts-with?': (str: string, prefix: string) => String(str ?? '').startsWith(prefix),
|
||||||
'ends-with?': (str: string, suffix: string) => str.endsWith(suffix),
|
'ends-with?': (str: string, suffix: string) => String(str ?? '').endsWith(suffix),
|
||||||
'contains?': (str: string, substr: string) => str.includes(substr),
|
'contains?': (str: string, substr: string) => String(str ?? '').includes(substr),
|
||||||
'empty?': (str: string) => str.length === 0,
|
'empty?': (str: string) => String(str ?? '').length === 0,
|
||||||
|
|
||||||
// inspection
|
// inspection
|
||||||
'index-of': (str: string, search: string) => str.indexOf(search),
|
'index-of': (str: string, search: string) => String(str ?? '').indexOf(search),
|
||||||
'last-index-of': (str: string, search: string) => str.lastIndexOf(search),
|
'last-index-of': (str: string, search: string) => String(str ?? '').lastIndexOf(search),
|
||||||
|
|
||||||
// transformations
|
// transformations
|
||||||
replace: (str: string, search: string, replacement: string) => str.replace(search, replacement),
|
replace: (str: string, search: string, replacement: string) => String(str ?? '').replace(search, replacement),
|
||||||
'replace-all': (str: string, search: string, replacement: string) => str.replaceAll(search, replacement),
|
'replace-all': (str: string, search: string, replacement: string) => String(str ?? '').replaceAll(search, replacement),
|
||||||
slice: (str: string, start: number, end?: number | null) => str.slice(start, end ?? undefined),
|
slice: (str: string, start: number, end?: number | null) => String(str ?? '').slice(start, end ?? undefined),
|
||||||
substring: (str: string, start: number, end?: number | null) => str.substring(start, end ?? undefined),
|
substring: (str: string, start: number, end?: number | null) => String(str ?? '').substring(start, end ?? undefined),
|
||||||
repeat: (str: string, count: number) => {
|
repeat: (str: string, count: number) => {
|
||||||
if (count < 0) throw new Error(`repeat: count must be non-negative, got ${count}`)
|
if (count < 0) throw new Error(`repeat: count must be non-negative, got ${count}`)
|
||||||
if (!Number.isInteger(count)) throw new Error(`repeat: count must be an integer, got ${count}`)
|
if (!Number.isInteger(count)) throw new Error(`repeat: count must be an integer, got ${count}`)
|
||||||
return str.repeat(count)
|
return String(str ?? '').repeat(count)
|
||||||
},
|
},
|
||||||
'pad-start': (str: string, length: number, pad: string = ' ') => str.padStart(length, pad),
|
'pad-start': (str: string, length: number, pad: string = ' ') => String(str ?? '').padStart(length, pad),
|
||||||
'pad-end': (str: string, length: number, pad: string = ' ') => str.padEnd(length, pad),
|
'pad-end': (str: string, length: number, pad: string = ' ') => String(str ?? '').padEnd(length, pad),
|
||||||
lines: (str: string) => str.split('\n'),
|
lines: (str: string) => String(str ?? '').split('\n'),
|
||||||
chars: (str: string) => str.split(''),
|
chars: (str: string) => String(str ?? '').split(''),
|
||||||
|
|
||||||
// regex
|
// regex
|
||||||
match: (str: string, regex: RegExp) => str.match(regex),
|
match: (str: string, regex: RegExp) => String(str ?? '').match(regex),
|
||||||
'test?': (str: string, regex: RegExp) => regex.test(str),
|
'test?': (str: string, regex: RegExp) => regex.test(String(str ?? '')),
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user