@windrun-huaiin/lib 12.0.0 → 12.0.1
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/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +25 -4
- package/dist/utils.mjs +25 -4
- package/package.json +1 -1
- package/src/utils.ts +30 -4
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AACpC,OAAO,EAAQ,KAAK,UAAU,EAAE,MAAM,MAAM,CAAA;AAI5C,wBAAgB,EAAE,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,UAEzC;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,IAAI,GAAG,IAAI,UAEhD;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,UA4BnE;AAGD,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC,QAgBxE;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,KAAK,MAAM,OAAO,CAAC;AACpC,OAAO,EAAQ,KAAK,UAAU,EAAE,MAAM,MAAM,CAAA;AAI5C,wBAAgB,EAAE,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,UAEzC;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,IAAI,GAAG,IAAI,UAEhD;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,UA4BnE;AAGD,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,KAAK,CAAC,cAAc,CAAC,WAAW,CAAC,QAgBxE;AAkBD,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,mBAAmB,GAAE,OAAc,EACnC,aAAa,GAAE,MAAa,GAC3B,MAAM,CAyBR"}
|
package/dist/utils.js
CHANGED
|
@@ -58,19 +58,40 @@ function handlePastePlainText(e) {
|
|
|
58
58
|
}
|
|
59
59
|
// Generates localized URL based on locale and locale prefix configuration
|
|
60
60
|
// Supports both 'as-needed' and 'always' localePrefix configurations
|
|
61
|
+
// Ensures URLs have proper trailing slash handling:
|
|
62
|
+
// - Root path (domain only) has trailing slash: '/'
|
|
63
|
+
// - All other paths have no trailing slash: '/blog', '/zh/blog', '/en'
|
|
61
64
|
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
62
|
-
// @param path - Base path (e.g., '/blog', '/docs')
|
|
65
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
63
66
|
// @param localPrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
64
67
|
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
65
68
|
// @example
|
|
69
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
70
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
66
71
|
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
67
72
|
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
68
|
-
// getAsNeededLocalizedUrl('en', '/blog',
|
|
73
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
74
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
69
75
|
function getAsNeededLocalizedUrl(locale, path, localPrefixAsNeeded = true, defaultLocale = 'en') {
|
|
76
|
+
// Normalize path: empty string or undefined treated as '/'
|
|
77
|
+
let normalizedPath = (path || '/').trim();
|
|
78
|
+
// Ensure path starts with '/'
|
|
79
|
+
if (!normalizedPath.startsWith('/')) {
|
|
80
|
+
normalizedPath = '/' + normalizedPath;
|
|
81
|
+
}
|
|
82
|
+
// Remove trailing slashes except for root path '/'
|
|
83
|
+
if (normalizedPath !== '/' && normalizedPath.endsWith('/')) {
|
|
84
|
+
normalizedPath = normalizedPath.replace(/\/+$/, '');
|
|
85
|
+
}
|
|
86
|
+
// Return based on locale prefix configuration
|
|
70
87
|
if (localPrefixAsNeeded && locale === defaultLocale) {
|
|
71
|
-
return
|
|
88
|
+
return normalizedPath;
|
|
89
|
+
}
|
|
90
|
+
// Add locale prefix, but avoid double trailing slash when path is '/'
|
|
91
|
+
if (normalizedPath === '/') {
|
|
92
|
+
return `/${locale}`;
|
|
72
93
|
}
|
|
73
|
-
return `/${locale}${
|
|
94
|
+
return `/${locale}${normalizedPath}`;
|
|
74
95
|
}
|
|
75
96
|
|
|
76
97
|
exports.cn = cn;
|
package/dist/utils.mjs
CHANGED
|
@@ -56,19 +56,40 @@ function handlePastePlainText(e) {
|
|
|
56
56
|
}
|
|
57
57
|
// Generates localized URL based on locale and locale prefix configuration
|
|
58
58
|
// Supports both 'as-needed' and 'always' localePrefix configurations
|
|
59
|
+
// Ensures URLs have proper trailing slash handling:
|
|
60
|
+
// - Root path (domain only) has trailing slash: '/'
|
|
61
|
+
// - All other paths have no trailing slash: '/blog', '/zh/blog', '/en'
|
|
59
62
|
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
60
|
-
// @param path - Base path (e.g., '/blog', '/docs')
|
|
63
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
61
64
|
// @param localPrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
62
65
|
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
63
66
|
// @example
|
|
67
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
68
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
64
69
|
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
65
70
|
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
66
|
-
// getAsNeededLocalizedUrl('en', '/blog',
|
|
71
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
72
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
67
73
|
function getAsNeededLocalizedUrl(locale, path, localPrefixAsNeeded = true, defaultLocale = 'en') {
|
|
74
|
+
// Normalize path: empty string or undefined treated as '/'
|
|
75
|
+
let normalizedPath = (path || '/').trim();
|
|
76
|
+
// Ensure path starts with '/'
|
|
77
|
+
if (!normalizedPath.startsWith('/')) {
|
|
78
|
+
normalizedPath = '/' + normalizedPath;
|
|
79
|
+
}
|
|
80
|
+
// Remove trailing slashes except for root path '/'
|
|
81
|
+
if (normalizedPath !== '/' && normalizedPath.endsWith('/')) {
|
|
82
|
+
normalizedPath = normalizedPath.replace(/\/+$/, '');
|
|
83
|
+
}
|
|
84
|
+
// Return based on locale prefix configuration
|
|
68
85
|
if (localPrefixAsNeeded && locale === defaultLocale) {
|
|
69
|
-
return
|
|
86
|
+
return normalizedPath;
|
|
87
|
+
}
|
|
88
|
+
// Add locale prefix, but avoid double trailing slash when path is '/'
|
|
89
|
+
if (normalizedPath === '/') {
|
|
90
|
+
return `/${locale}`;
|
|
70
91
|
}
|
|
71
|
-
return `/${locale}${
|
|
92
|
+
return `/${locale}${normalizedPath}`;
|
|
72
93
|
}
|
|
73
94
|
|
|
74
95
|
export { cn, formatTimestamp, getAsNeededLocalizedUrl, handlePastePlainText, viewLocalTime };
|
package/package.json
CHANGED
package/src/utils.ts
CHANGED
|
@@ -62,22 +62,48 @@ export function handlePastePlainText(e: React.ClipboardEvent<HTMLElement>) {
|
|
|
62
62
|
|
|
63
63
|
// Generates localized URL based on locale and locale prefix configuration
|
|
64
64
|
// Supports both 'as-needed' and 'always' localePrefix configurations
|
|
65
|
+
// Ensures URLs have proper trailing slash handling:
|
|
66
|
+
// - Root path (domain only) has trailing slash: '/'
|
|
67
|
+
// - All other paths have no trailing slash: '/blog', '/zh/blog', '/en'
|
|
65
68
|
// @param locale - Current locale (e.g., 'en', 'zh', 'ja')
|
|
66
|
-
// @param path - Base path (e.g., '/blog', '/docs')
|
|
69
|
+
// @param path - Base path (e.g., '/', '/blog', '/docs', or '' treated as '/')
|
|
67
70
|
// @param localPrefixAsNeeded - Whether localePrefix is set to 'as-needed' (default: true)
|
|
68
71
|
// @param defaultLocale - The default locale for the application (default: 'en')
|
|
69
72
|
// @example
|
|
73
|
+
// getAsNeededLocalizedUrl('en', '/', true, 'en') // Returns '/'
|
|
74
|
+
// getAsNeededLocalizedUrl('zh', '/', true, 'en') // Returns '/zh'
|
|
70
75
|
// getAsNeededLocalizedUrl('en', '/blog', true, 'en') // Returns '/blog'
|
|
71
76
|
// getAsNeededLocalizedUrl('zh', '/blog', true, 'en') // Returns '/zh/blog'
|
|
72
|
-
// getAsNeededLocalizedUrl('en', '/blog',
|
|
77
|
+
// getAsNeededLocalizedUrl('en', '/blog/', true, 'en') // Returns '/blog'
|
|
78
|
+
// getAsNeededLocalizedUrl('en', '/', false, 'en') // Returns '/en'
|
|
73
79
|
export function getAsNeededLocalizedUrl(
|
|
74
80
|
locale: string,
|
|
75
81
|
path: string,
|
|
76
82
|
localPrefixAsNeeded: boolean = true,
|
|
77
83
|
defaultLocale: string = 'en'
|
|
78
84
|
): string {
|
|
85
|
+
// Normalize path: empty string or undefined treated as '/'
|
|
86
|
+
let normalizedPath = (path || '/').trim();
|
|
87
|
+
|
|
88
|
+
// Ensure path starts with '/'
|
|
89
|
+
if (!normalizedPath.startsWith('/')) {
|
|
90
|
+
normalizedPath = '/' + normalizedPath;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Remove trailing slashes except for root path '/'
|
|
94
|
+
if (normalizedPath !== '/' && normalizedPath.endsWith('/')) {
|
|
95
|
+
normalizedPath = normalizedPath.replace(/\/+$/, '');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Return based on locale prefix configuration
|
|
79
99
|
if (localPrefixAsNeeded && locale === defaultLocale) {
|
|
80
|
-
return
|
|
100
|
+
return normalizedPath;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Add locale prefix, but avoid double trailing slash when path is '/'
|
|
104
|
+
if (normalizedPath === '/') {
|
|
105
|
+
return `/${locale}`;
|
|
81
106
|
}
|
|
82
|
-
|
|
107
|
+
|
|
108
|
+
return `/${locale}${normalizedPath}`;
|
|
83
109
|
}
|