trim

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/string/trim

说明

去除字符串首尾的指定字符, 默认为空格(whitespace)

用法

trim.ts
trim.test.ts
import { trim } from "./ddzy/utility/string/trim";
// ? 空字符串
const s1 = '';
const p1 = trim(s1);
console.log(p1);
console.log('---------------');
// ? 字符串 + 首尾空格 + 默认字符
const s2 = ' ddzy ';
const p2 = trim(s2);
console.log(p2);
console.log('----------------');
// ? 字符串 + 指定字符
const s3 = 'wow, i like program!';
const s4 = 'wow, i love you, wow';
const p3 = trim(s3, 'wow');
const p4 = trim(s4, 'wow');
console.log(p3);
console.log(p4);
import { trim } from "../../utility/string/trim";
describe('trim() tests', () => {
test('trim should return a new processed string by using the default replacer when receive an empty string', () => {
const received = '';
const expected = '';
const result = trim(received);
expect(result).toBe(expected);
});
test('trim should return a new processed string that throw away the whitespace by using the default replacer', () => {
const received = ' ddzy ';
const expected = 'ddzy';
const result = trim(received);
expect(result).toBe(expected);
});
test('trim should return a new processed string by using a special replacer', () => {
const received = [
'wow, i like program!',
'oh... i love you oh',
];
const expected = [
', i like program!',
'... i love you ',
];
const replacer = ['wow', 'oh'];
const result = received.map((v, i) => {
return trim(v, replacer[i]);
});
result.forEach((v, i) => {
expect(v).toBe(expected[i]);
});
});
});