Skip to content

Commit

Permalink
Update stripGender function to handle null values and add null checks
Browse files Browse the repository at this point in the history
* Update `stripGender` function in `src/string-utilities.ts` to handle null and undefined values by returning an empty string.
* Add test cases in `src/string-utilities.test.ts` for `stripGender` function to handle null and undefined values.
* Update `sportlinkRowToContact` function in `src/sportlink-to-mailchimp-converter.ts` to ensure `categorie` is not null or undefined before calling `stripGender`.
* Update `sportlinkRowToContact` function to check if `categorie` is null or undefined before checking `if (tags.length === 0)`.
  • Loading branch information
EdwinOtten committed Dec 5, 2024
1 parent 1f2f8b8 commit f366022
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tasks": {
"test": "npm install && npm test"
}
}
4 changes: 2 additions & 2 deletions src/sportlink-to-mailchimp-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const sportlinkRowToContact = (row: SportlinkRow, config: SportlinkToMailchimpCo
}

// If not tagged yet, assume Athletics
if (tags.length === 0) {
if (tags.length === 0 && categorie !== null && categorie !== undefined) {
tags.push(`Atletiek ${stripGender(categorie)}`)
}

Expand Down Expand Up @@ -141,4 +141,4 @@ const parseCsv = async (file: LocalFile) => new Promise<SportlinkRow[]>((resolve

const formatParseErrors = (errors: ParseError[]): string => errors
.map(error => `type: ${error.type}, code: ${error.code}, message: ${error.message}, row: ${error.row}, index: ${error.index}`)
.join('\n')
.join('\n')
79 changes: 42 additions & 37 deletions src/string-utilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
import { stripGender, isNullOrEmpty, trimSpacesAndRemoveDoubleQuotes } from './string-utilities'

describe('stripGender', () => {
it('should remove gender-related words and trim the string', () => {
expect(stripGender('Meisjes kleding')).toBe('kleding')
expect(stripGender('Jongens schoenen')).toBe('schoenen')
expect(stripGender('Vrouwen accessoires')).toBe('accessoires')
expect(stripGender('Mannen horloges')).toBe('horloges')
expect(stripGender('Unisex kleding')).toBe('Unisex kleding') // No gender-related word
})
})

describe('isNullOrEmpty', () => {
it('should return true for undefined, null, or empty strings', () => {
expect(isNullOrEmpty(undefined!)).toBe(true)
expect(isNullOrEmpty(null!)).toBe(true)
expect(isNullOrEmpty('')).toBe(true)
})

it('should return false for non-empty strings', () => {
expect(isNullOrEmpty('text')).toBe(false)
expect(isNullOrEmpty(' ')).toBe(false)
})
})

describe('trimSpacesAndRemoveDoubleQuotes', () => {
it('should trim spaces and remove double quotes', () => {
expect(trimSpacesAndRemoveDoubleQuotes(' "text" ')).toBe('text')
expect(trimSpacesAndRemoveDoubleQuotes(' "text')).toBe('text')
expect(trimSpacesAndRemoveDoubleQuotes('text" ')).toBe('text')
expect(trimSpacesAndRemoveDoubleQuotes('text')).toBe('text')
})

it('should handle empty strings', () => {
expect(trimSpacesAndRemoveDoubleQuotes('')).toBe('')
})
})
import { stripGender, isNullOrEmpty, trimSpacesAndRemoveDoubleQuotes } from './string-utilities'

describe('stripGender', () => {
it('should remove gender-related words and trim the string', () => {
expect(stripGender('Meisjes kleding')).toBe('kleding')
expect(stripGender('Jongens schoenen')).toBe('schoenen')
expect(stripGender('Vrouwen accessoires')).toBe('accessoires')
expect(stripGender('Mannen horloges')).toBe('horloges')
expect(stripGender('Unisex kleding')).toBe('Unisex kleding') // No gender-related word
})

it('should return an empty string for null or undefined values', () => {
expect(stripGender(null!)).toBe('')
expect(stripGender(undefined!)).toBe('')
})
})

describe('isNullOrEmpty', () => {
it('should return true for undefined, null, or empty strings', () => {
expect(isNullOrEmpty(undefined!)).toBe(true)
expect(isNullOrEmpty(null!)).toBe(true)
expect(isNullOrEmpty('')).toBe(true)
})

it('should return false for non-empty strings', () => {
expect(isNullOrEmpty('text')).toBe(false)
expect(isNullOrEmpty(' ')).toBe(false)
})
})

describe('trimSpacesAndRemoveDoubleQuotes', () => {
it('should trim spaces and remove double quotes', () => {
expect(trimSpacesAndRemoveDoubleQuotes(' "text" ')).toBe('text')
expect(trimSpacesAndRemoveDoubleQuotes(' "text')).toBe('text')
expect(trimSpacesAndRemoveDoubleQuotes('text" ')).toBe('text')
expect(trimSpacesAndRemoveDoubleQuotes('text')).toBe('text')
})

it('should handle empty strings', () => {
expect(trimSpacesAndRemoveDoubleQuotes('')).toBe('')
})
})
15 changes: 10 additions & 5 deletions src/string-utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const regexGender = /Meisjes|Jongens|Vrouwen|Mannen/
export const stripGender = (categorie: string) => categorie.replace(regexGender, '').trim()

export const isNullOrEmpty = (text: string) => text === undefined || text === null || text.length <= 0
export const trimSpacesAndRemoveDoubleQuotes = (text: string) => text.trim().split('"').join('')
const regexGender = /Meisjes|Jongens|Vrouwen|Mannen/
export const stripGender = (categorie: string) => {
if (categorie === null || categorie === undefined) {
return ''
}
return categorie.replace(regexGender, '').trim()
}

export const isNullOrEmpty = (text: string) => text === undefined || text === null || text.length <= 0
export const trimSpacesAndRemoveDoubleQuotes = (text: string) => text.trim().split('"').join('')

0 comments on commit f366022

Please sign in to comment.