-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85587fa
commit 3085b4a
Showing
5 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@xstools/utility': minor | ||
--- | ||
|
||
add string/substring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './case'; | ||
export * from './getWords'; | ||
export * from './substring'; | ||
export * from './template'; | ||
export * from './trim'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { describe, expect, test } from 'bun:test'; | ||
import { substring } from './substring'; | ||
|
||
describe('substring', () => { | ||
test('normal usage', () => { | ||
expect(substring('123', 2)).toBe('12'); | ||
expect(substring('123', 10)).toBe('123'); | ||
|
||
expect(substring('今天很开心🌸🌸🌸', 6)).toBe('今天很开心🌸'); | ||
expect('今天很开心🌸🌸🌸'.substring(0, 6)).toBe('今天很开心\ud83c'); | ||
}); | ||
|
||
test('unicode support', () => { | ||
expect(substring('𝒽𝑒𝓁𝓁𝑜 𝓌𝑜𝓇𝓁𝒹', 1)).toBe('𝒽'); | ||
expect('𝒽𝑒𝓁𝓁𝑜 𝓌𝑜𝓇𝓁𝒹'.substring(0, 2)).toBe('𝒽'); | ||
|
||
expect(substring('🐶狗', 1)).toBe('🐶'); | ||
expect('🐶狗'.substring(0, 2)).toBe('🐶'); | ||
|
||
expect(substring('狗狗', 1)).toBe('狗'); | ||
expect('狗狗'.substring(0, 1)).toBe('狗'); | ||
|
||
// https://juejin.cn/post/7070079762429034526 | ||
expect(substring('🐕🦺狗', 1)).toBe('🐕'); | ||
expect('🐕🦺狗'.substring(0, 2)).toBe('🐕'); | ||
}); | ||
|
||
test('empty', () => { | ||
expect(substring('xxx', 0)).toBe(''); | ||
expect(substring('', 0)).toBe(''); | ||
expect(substring('', 10)).toBe(''); | ||
|
||
// @ts-expect-error | ||
expect(substring({}, 10)).toBe(''); | ||
// @ts-expect-error | ||
expect(substring(1, 10)).toBe(''); | ||
// @ts-expect-error | ||
expect(substring([1], 10)).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Returns the part of this string by length. | ||
* | ||
* @example | ||
* substring('123', 2) // => '12' | ||
* substring('123', 10) // => '123' | ||
* substring('今天很开心🌸🌸🌸', 6) // => '今天很开心🌸' | ||
* | ||
* --- WHY NOT NATIVE --- | ||
* '今天很开心🌸🌸🌸'.substring(0, 6) // => '今天很开心\ud83c' | ||
* '今天很开心🌸🌸🌸'.substring(0, 7) // => '今天很开心🌸' | ||
*/ | ||
export const substring = (s: string, length: number): string => { | ||
if (typeof s !== 'string') { | ||
return ''; | ||
} | ||
|
||
const temp = s.substring(0, length * 2); | ||
|
||
return [...temp].slice(0, length).join(''); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters