Skip to content

Commit

Permalink
feat(filters): add police number filter
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsch committed Sep 28, 2021
1 parent 91a0d8e commit 912e74b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/components/src/filters/balPoliceNumber.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { balPoliceNumber } from './balPoliceNumber'

describe('balPoliceNumber', () => {
test('should format a claim number correctly', () => {
expect(balPoliceNumber('501222333')).toBe('50/1.222.333')
})
test('should format null as empty string', () => {
expect(balPoliceNumber(null)).toBe('')
})
test('should output a number with a wrong format raw', () => {
expect(balPoliceNumber('123')).toBe('123')
})
test('should format a claim number with a sign postfix correctly', () => {
expect(balPoliceNumber('0501222333')).toBe('50/1.222.333')
})
})
21 changes: 21 additions & 0 deletions packages/components/src/filters/balPoliceNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Transforms the given string into the correct police-number format.
*
* ```typescript
* balPoliceNumber('501222333') // 50/1.222.333
* ```
*/
export function balPoliceNumber(value: string | undefined | null | number): string {
if (!value) {
return ''
}
let newValue = `${value}`
if (newValue[0] !== '0') {
newValue = `0${value}`
}
const parts = [newValue.substring(1, 3), newValue.substring(3, 4), newValue.substring(4, 7), newValue.substring(7, 10)].filter(val => val.length > 0)
if (!parts || parts.length < 4) {
return `${value}`
}
return `${parts[0]}/${parts[1]}.${parts[2]}.${parts[3]}`
}

0 comments on commit 912e74b

Please sign in to comment.