Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Self harm words (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels authored Feb 10, 2020
1 parent ba62046 commit 5bc85d5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions f2/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const bodyParser = require('body-parser')
const path = require('path')
const formidable = require('formidable')
const { getAllCerts, encryptAndSend } = require('./src/utils/encryptedEmail')
const { selfHarmWordsScan } = require('./utils/selfHarmWordsScan')

require('dotenv').config()

Expand Down Expand Up @@ -65,6 +66,12 @@ const uploadData = (req, res) => {
// Extract the JSON from the "JSON" form element
const data = JSON.parse(fields['json'])
console.log('Parsed JSON:', data)

const selfHarmWords = selfHarmWordsScan(data)
if (selfHarmWords) {
console.warn(`Self harm words detected: ${selfHarmWords}`)
}
data.selfHarmWords = selfHarmWords
data.submissionTime = new Date().toISOString()
data.contactInfo.email = randomizeString(data.contactInfo.email)

Expand Down
25 changes: 25 additions & 0 deletions f2/utils/__tests__/selfHarmWordsScan.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { selfHarmWordsScan } from '../selfHarmWordsScan'

describe('selfHarmWordsScan', () => {
it('returns empty list when no words match', () => {
const data = { a: { b: 'hi' } }
expect(selfHarmWordsScan(data)).toEqual([])
})

it('returns 2 words when they are nested in data', () => {
const data = {
a: { b: 'hi' },
aa: ['this', 'agile', 'thing'],
cc: { ccc: { dd: 'hi', ee: 'scrum' } },
}
expect(selfHarmWordsScan(data)).toEqual(['agile', 'scrum'])
})

it('catches words with accents', () => {
const data = {
a: 'agilé',
aa: 'this sçrum',
}
expect(selfHarmWordsScan(data)).toEqual(['agile', 'scrum'])
})
})
14 changes: 14 additions & 0 deletions f2/utils/selfHarmWordsScan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 'use strict'

const selfHarmWords = 'agile, lean, mvp, scrum'.split(',').map(w => w.trim())

// from https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
const removeAccents = s => s.normalize('NFD').replace(/[\u0300-\u036f]/g, '')

const selfHarmWordsScan = data => {
const json = removeAccents(JSON.stringify(data).toLowerCase())
const wordsUsed = selfHarmWords.filter(w => json.includes(w))
return wordsUsed
}

module.exports = { selfHarmWordsScan }

0 comments on commit 5bc85d5

Please sign in to comment.