Add StringUtils

This commit is contained in:
Jerko Steiner 2019-04-06 13:58:20 +08:00
parent 42439ee422
commit 89af2f0845
8 changed files with 87 additions and 20 deletions

View File

@ -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('')
})
})
})

View File

@ -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()
}

View File

@ -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'

View File

@ -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

View File

@ -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 {

View File

@ -1,4 +1,3 @@
export * from './IValidationMessage'
export * from './ValidationError'
export * from './Validator'
export * from './trim'

View File

@ -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('')
})
})

View File

@ -1,6 +0,0 @@
export function trim(str?: string) {
if (!str) {
return ''
}
return str.trim()
}