Skip to content

Commit

Permalink
feat(utility): add string/substring
Browse files Browse the repository at this point in the history
  • Loading branch information
bingtsingw committed Aug 20, 2024
1 parent 85587fa commit 3085b4a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/serious-weeks-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xstools/utility': minor
---

add string/substring
1 change: 1 addition & 0 deletions packages/utility/src/string/index.ts
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';
40 changes: 40 additions & 0 deletions packages/utility/src/string/substring.spec.ts
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('');
});
});
21 changes: 21 additions & 0 deletions packages/utility/src/string/substring.ts
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('');
};
2 changes: 1 addition & 1 deletion packages/utility/src/string/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, test } from 'bun:test';
import { template } from './template';

describe('template', () => {
test('template', () => {
test('normal usage', () => {
expect(template('hello, {{name}}', { name: 'world' })).toBe('hello, world');
expect(template('hello, {{ name \n}}', { name: 'world' })).toBe('hello, world');
expect(template('hello, {{name}}', {})).toBe('hello, ');
Expand Down

0 comments on commit 3085b4a

Please sign in to comment.