@stacksjs/strings 0.64.6 → 0.65.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -11,7 +11,7 @@ interface SlugOptions {
11
11
 
12
12
  // port from nanoid
13
13
  // https://github.com/ai/nanoid
14
- const urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
14
+ export const urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
15
15
 
16
16
  /**
17
17
  * Replace backslash to slash
@@ -22,7 +22,7 @@ const urlAlphabet = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwy
22
22
  * slash('C:\\Users\\Chris') => 'C:/Users/Chris'
23
23
  * ```
24
24
  */
25
- export function slash(str: string) {
25
+ export function slash(str: string): string {
26
26
  return str.replace(/\\/g, '/')
27
27
  }
28
28
 
@@ -35,8 +35,9 @@ export function slash(str: string) {
35
35
  * ensurePrefix('https://', 'google.com') => 'https://google.com'
36
36
  * ensurePrefix('https://', 'http://google.com') => 'https://google.com'
37
37
  */
38
- export function ensurePrefix(prefix: string, str: string) {
39
- if (!str.startsWith(prefix)) return prefix + str
38
+ export function ensurePrefix(prefix: string, str: string): string {
39
+ if (!str.startsWith(prefix))
40
+ return prefix + str
40
41
  return str
41
42
  }
42
43
 
@@ -50,7 +51,7 @@ export function ensurePrefix(prefix: string, str: string) {
50
51
  * ensureSuffix('.js', 'index.js') => 'index.js'
51
52
  * ```
52
53
  */
53
- export function ensureSuffix(suffix: string, str: string) {
54
+ export function ensureSuffix(suffix: string, str: string): string {
54
55
  return str.endsWith(suffix) ? str : str + suffix
55
56
  }
56
57
 
@@ -67,15 +68,16 @@ export function ensureSuffix(suffix: string, str: string) {
67
68
  * ```
68
69
  */
69
70
  export function template(str: string, ...args: any[]): string {
70
- return str.replace(/{(\d+)}/g, (match, key) => {
71
+ return str.replace(/\{(\d+)\}/g, (match, key) => {
71
72
  const index = Number(key)
72
73
 
73
74
  return Number.isNaN(index) ? match : args[index]
74
75
  })
75
76
  }
76
77
 
77
- export function truncate(str: string, length: number, end = '...') {
78
- if (str.length <= length) return str
78
+ export function truncate(str: string, length: number, end = '...'): string {
79
+ if (str.length <= length)
80
+ return str
79
81
 
80
82
  return str.slice(0, length - end.length) + end
81
83
  }
@@ -84,7 +86,7 @@ export function truncate(str: string, length: number, end = '...') {
84
86
  * Generate a random string
85
87
  * @category String
86
88
  */
87
- export function random(size = 16, dict = urlAlphabet) {
89
+ export function random(size = 16, dict: string = urlAlphabet): string {
88
90
  let id = ''
89
91
  let i = size
90
92
  const len = dict.length
@@ -101,7 +103,8 @@ export function random(size = 16, dict = urlAlphabet) {
101
103
  * ```
102
104
  */
103
105
  export function slug(str: string, options?: SlugOptions): string {
104
- if (options) return slugify(str, options)
106
+ if (options)
107
+ return slugify(str, options)
105
108
 
106
109
  return slugify(str, {
107
110
  lower: true,