add tests for getInitials
parent
2a11d5e71f
commit
ba53330afd
@ -0,0 +1,82 @@
|
|||||||
|
import { expect } from 'chai';
|
||||||
|
import { getInitials } from '../../../../util/getInitials';
|
||||||
|
|
||||||
|
describe('getInitials', () => {
|
||||||
|
describe('empty of null string', () => {
|
||||||
|
it('initials: return undefined if string is undefined', () => {
|
||||||
|
expect(getInitials(undefined)).to.be.equal('0', 'should have return 0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: return undefined if string is empty', () => {
|
||||||
|
expect(getInitials('')).to.be.equal('0', 'should have return 0');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: return undefined if string is null', () => {
|
||||||
|
expect(getInitials(null as any)).to.be.equal('0', 'should have return 0');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('name is a pubkey', () => {
|
||||||
|
it('initials: return the first char after 05 if it starts with 05 and has length >2 ', () => {
|
||||||
|
expect(getInitials('052')).to.be.equal('2', 'should have return 2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: return the first char after 05 capitalized if it starts with 05 and has length >2 ', () => {
|
||||||
|
expect(getInitials('05bcd')).to.be.equal('B', 'should have return B');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: return the first char after 05 if it starts with 05 and has length >2 ', () => {
|
||||||
|
expect(getInitials('059052052052052052052052')).to.be.equal('9', 'should have return 9');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('name has a space in its content', () => {
|
||||||
|
it('initials: return the first char of each first 2 words if a space is present ', () => {
|
||||||
|
expect(getInitials('John Doe')).to.be.equal('JD', 'should have return JD');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: return the first char capitalized of each first 2 words if a space is present ', () => {
|
||||||
|
expect(getInitials('John doe')).to.be.equal('JD', 'should have return JD capitalized');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: return the first char capitalized of each first 2 words if a space is present, even with more than 2 words ', () => {
|
||||||
|
expect(getInitials('John Doe Alice')).to.be.equal('JD', 'should have return JD capitalized');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: return the first char capitalized of each first 2 words if a space is present, even with more than 2 words ', () => {
|
||||||
|
expect(getInitials('John doe Alice')).to.be.equal('JD', 'should have return JD capitalized');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('name is not ascii', () => {
|
||||||
|
// ß maps to SS in uppercase
|
||||||
|
it('initials: shorten to 2 char at most if the uppercase form length is > 2 ', () => {
|
||||||
|
expect(getInitials('John ß')).to.be.equal('JS', 'should have return JS capitalized');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: shorten to 2 char at most if the uppercase form length is > 2 ', () => {
|
||||||
|
expect(getInitials('ß ß')).to.be.equal('SS', 'should have return SS capitalized');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('name has NO spaces in its content', () => {
|
||||||
|
it('initials: return the first 2 chars of the first word if the name has no space ', () => {
|
||||||
|
expect(getInitials('JOHNY')).to.be.equal('JO', 'should have return JO');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: return the first 2 chars capitalized of the first word if the name has no space ', () => {
|
||||||
|
expect(getInitials('Johnny')).to.be.equal('JO', 'should have return JO');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('name is not ascii', () => {
|
||||||
|
// ß maps to SS in uppercase
|
||||||
|
it('initials: shorten to 2 char at most if the uppercase form length is > 2 ', () => {
|
||||||
|
expect(getInitials('ß')).to.be.equal('SS', 'should have return SS capitalized');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initials: shorten to 2 char at most if the uppercase form length is > 2 ', () => {
|
||||||
|
expect(getInitials('ßß')).to.be.equal('SS', 'should have return SS capitalized');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -1,18 +1,35 @@
|
|||||||
export function getInitials(name?: string): string | undefined {
|
export function getInitials(name?: string): string {
|
||||||
if (!name || !name.length) {
|
if (!name || !name.length) {
|
||||||
return;
|
return '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.length > 2 && name.startsWith('05')) {
|
if (name.length > 2 && name.startsWith('05')) {
|
||||||
return name[2];
|
// Just the third char of the pubkey when the name is a pubkey
|
||||||
|
return upperAndShorten(name[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const initials = name
|
if (name.indexOf(' ') === -1) {
|
||||||
.split(' ')
|
// there is no space, just return the first 2 chars of the name
|
||||||
.slice(0, 2)
|
|
||||||
.map(n => {
|
|
||||||
return n[0];
|
|
||||||
});
|
|
||||||
|
|
||||||
return initials.join('');
|
if (name.length > 1) {
|
||||||
|
return upperAndShorten(name.slice(0, 2));
|
||||||
|
}
|
||||||
|
return upperAndShorten(name[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// name has a space, just extract the first char of each words
|
||||||
|
return upperAndShorten(
|
||||||
|
name
|
||||||
|
.split(' ')
|
||||||
|
.slice(0, 2)
|
||||||
|
.map(n => {
|
||||||
|
return n[0];
|
||||||
|
})
|
||||||
|
.join('')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function upperAndShorten(str: string) {
|
||||||
|
// believe it or not, some chars put in uppercase can be more than one char. (ß for instance)
|
||||||
|
return str.toLocaleUpperCase().slice(0, 2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue