capitalize

源码

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

说明

将指定的字符串的首字母大写, 剩下为小写

用法

capitalize.ts
capitalize.test.ts
import { capitalize } from "./ddzy/utility/string/capitalize";
// ?
const s1 = '';
const p1 = capitalize(s1);
console.log(p1);
console.log('-------------------');
// ?
const s2 = 'ZHAO';
const p2 = capitalize(s2);
console.log(p2);
console.log('-------------------');
// ?
const s3 = 'duan';
const p3 = capitalize(s3);
console.log(p3);
console.log('----------------------');
// ?
const s4 = '_DDZY';
const p4 = capitalize(s4);
console.log(p4);
// ?
const s5 = ' ___&*duanzhaoyang';
const p5 = capitalize(s5);
console.log(p5);
import { capitalize } from "../../utility/string/capitalize";
describe('capitalize tests...', () => {
test('capitalize should return the origin text when receive an empty string', () => {
const received = '';
const expected = '';
const result = capitalize(received);
expect(result).toBe(expected);
});
test('capitalize should return the converted string correctly', () => {
const received = ['ZHAO', 'duan'];
const expected = ['Zhao', 'Duan'];
const result = received.map((v) => {
return capitalize(v);
});
result.forEach((v, i) => {
expect(v).toBe(expected[i]);
});
});
test('capitalize should return the converted string when receive a text contains special characters', () => {
const received = ['_DDZY', '___&*duanzhaoYANg'];
const expected = ['_Ddzy', '___&*Duanzhaoyang'];
const result = received.map((v) => {
return capitalize(v);
});
result.forEach((v, i) => {
expect(v).toBe(expected[i]);
});
});
});