diff --git a/packages/common/src/StringUtils.test.ts b/packages/common/src/StringUtils.test.ts new file mode 100644 index 0000000..6d065c5 --- /dev/null +++ b/packages/common/src/StringUtils.test.ts @@ -0,0 +1,60 @@ +import * as StringUtils from './StringUtils' + +describe('StringUtils', () => { + + describe('ellipsis', () => { + it('return "" when maxLength <= 0 or string is undefined', () => { + expect(StringUtils.ellipsis('test', 0)).toBe('') + expect(StringUtils.ellipsis(undefined, 100)).toBe('') + }) + + it('defaults maxLength to 255 characters', () => { + let c = '' + for (let i = 0; i < 300; i++) { + c += 'a' + } + expect(StringUtils.ellipsis(c)).toBe(c.substring(0, 252)) + }) + + it('does nothing when text is not too long', () => { + expect(StringUtils.ellipsis('test', 4)).toBe('test') + expect(StringUtils.ellipsis('test', 5)).toBe('test') + expect(StringUtils.ellipsis('test', 10)).toBe('test') + }) + + it('only shortens strings without spaces', () => { + expect(StringUtils.ellipsis('test', 3)).toBe('tes') + expect(StringUtils.ellipsis('test', 2)).toBe('te') + expect(StringUtils.ellipsis('test', 1)).toBe('t') + expect(StringUtils.ellipsis('test', 0)).toBe('') + expect(StringUtils.ellipsis('test', -1)).toBe('') + }) + + it('adds end ellipsis to text with spaces', () => { + const str = 'this is a test' + expect(StringUtils.ellipsis(str, 14)).toEqual('this is a test') + expect(StringUtils.ellipsis(str, 13)).toEqual('this is a...') + expect(StringUtils.ellipsis(str, 12)).toEqual('this is a...') + expect(StringUtils.ellipsis(str, 11)).toEqual('this is...') + expect(StringUtils.ellipsis(str, 10)).toEqual('this is...') + expect(StringUtils.ellipsis(str, 9)).toEqual('this...') + expect(StringUtils.ellipsis(str, 8)).toEqual('this...') + expect(StringUtils.ellipsis(str, 7)).toEqual('this...') + expect(StringUtils.ellipsis(str, 6)).toEqual('thi...') + expect(StringUtils.ellipsis(str, 5)).toEqual('th...') + expect(StringUtils.ellipsis(str, 4)).toEqual('t...') + expect(StringUtils.ellipsis(str, 3)).toEqual('thi') + expect(StringUtils.ellipsis(str, 2)).toEqual('th') + expect(StringUtils.ellipsis(str, 1)).toEqual('t') + }) + }) + + describe('trim', () => { + it('trims string', () => { + expect(StringUtils.trim('test')).toEqual('test') + expect(StringUtils.trim(' test ')).toEqual('test') + expect(StringUtils.trim(undefined)).toEqual('') + }) + }) + +}) diff --git a/packages/common/src/StringUtils.ts b/packages/common/src/StringUtils.ts new file mode 100644 index 0000000..8bfdaff --- /dev/null +++ b/packages/common/src/StringUtils.ts @@ -0,0 +1,22 @@ +export function ellipsis(text: string | undefined, maxLength = 255): string { + if (maxLength <= 0 || text === undefined) { + return '' + } + if (text.length <= maxLength) { + return text + } + let str = text.trim().substring(0, maxLength) + const index = str.lastIndexOf(' ') + if (index === -1) { + return str.length > 3 ? str.substring(0, str.length - 3) + '...' : str + } + str = str.substring(0, index) + '...' + return ellipsis(str, maxLength) +} + +export function trim(str?: string) { + if (!str) { + return '' + } + return str.trim() +} diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 23219da..79af98e 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -9,6 +9,7 @@ export * from './IUser' export * from './IUser' export * from './IUserInTeam' export * from './IUserTeam' +export * from './StringUtils' export * from './URLFormatter' export * from './filterProps' export * from './indexBy' diff --git a/packages/server/src/services/UserService.ts b/packages/server/src/services/UserService.ts index e75d829..3b574c6 100644 --- a/packages/server/src/services/UserService.ts +++ b/packages/server/src/services/UserService.ts @@ -1,12 +1,12 @@ import createError from 'http-errors' import {BaseService} from './BaseService' -import {ICredentials, INewUser, IUser} from '@rondo/common' +import {ICredentials, INewUser, IUser, trim} from '@rondo/common' import {IUserService} from './IUserService' import {UserEmail} from '../entities/UserEmail' import {User} from '../entities/User' import {compare, hash} from 'bcrypt' import {validate as validateEmail} from 'email-validator' -import {Validator, trim} from '../validator' +import {Validator} from '../validator' const SALT_ROUNDS = 10 const MIN_PASSWORD_LENGTH = 10 diff --git a/packages/server/src/team/TeamService.ts b/packages/server/src/team/TeamService.ts index 1b8b64e..6f86f01 100644 --- a/packages/server/src/team/TeamService.ts +++ b/packages/server/src/team/TeamService.ts @@ -1,10 +1,10 @@ import {BaseService} from '../services/BaseService' import {ITeamService} from './ITeamService' -import {IUserInTeam} from '@rondo/common' +import {IUserInTeam, trim} from '@rondo/common' import {IUserTeamParams} from './IUserTeamParams' import {Team} from '../entities/Team' import {UserTeam} from '../entities/UserTeam' -import {Validator, trim} from '../validator' +import {Validator} from '../validator' export class TeamService extends BaseService implements ITeamService { diff --git a/packages/server/src/validator/index.ts b/packages/server/src/validator/index.ts index e66861d..31baf0f 100644 --- a/packages/server/src/validator/index.ts +++ b/packages/server/src/validator/index.ts @@ -1,4 +1,3 @@ export * from './IValidationMessage' export * from './ValidationError' export * from './Validator' -export * from './trim' diff --git a/packages/server/src/validator/trim.test.ts b/packages/server/src/validator/trim.test.ts deleted file mode 100644 index dfdcd99..0000000 --- a/packages/server/src/validator/trim.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {trim} from './trim' - -describe('trim', () => { - it('trims string', () => { - expect(trim('test')).toEqual('test') - expect(trim(' test ')).toEqual('test') - expect(trim(undefined)).toEqual('') - }) -}) diff --git a/packages/server/src/validator/trim.ts b/packages/server/src/validator/trim.ts deleted file mode 100644 index fe46baa..0000000 --- a/packages/server/src/validator/trim.ts +++ /dev/null @@ -1,6 +0,0 @@ -export function trim(str?: string) { - if (!str) { - return '' - } - return str.trim() -}