@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/README.md +44 -44
- package/dist/case.d.ts +12 -0
- package/dist/helpers.d.ts +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +176 -2
- package/dist/index.js.map +170 -11
- package/dist/is.d.ts +43 -0
- package/dist/macro.d.ts +37 -0
- package/dist/pluralize.d.ts +23 -0
- package/dist/string.d.ts +6 -0
- package/dist/types.d.ts +1 -0
- package/dist/utils.d.ts +70 -0
- package/package.json +11 -16
- package/src/case.ts +1 -1
- package/src/helpers.ts +1 -1
- package/src/is.ts +42 -42
- package/src/macro.ts +39 -35
- package/src/pluralize.ts +6 -5
- package/src/string.ts +5 -3
- package/src/utils.ts +13 -10
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))
|
|
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(
|
|
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)
|
|
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)
|
|
106
|
+
if (options)
|
|
107
|
+
return slugify(str, options)
|
|
105
108
|
|
|
106
109
|
return slugify(str, {
|
|
107
110
|
lower: true,
|