Fetching pronoun data from a csv
This commit is contained in:
		
							parent
							
								
									12ccb9c3df
								
							
						
					
					
						commit
						bd7bbc7f87
					
				
					 6 changed files with 2380 additions and 1 deletions
				
			
		
							
								
								
									
										7
									
								
								app.js
									
										
									
									
									
								
							
							
						
						
									
										7
									
								
								app.js
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,7 +1,12 @@
 | 
			
		|||
import express from 'express'
 | 
			
		||||
import { engine } from 'express-handlebars'
 | 
			
		||||
import fetchData from './data.js'
 | 
			
		||||
 | 
			
		||||
const app = express()
 | 
			
		||||
const data = fetchData()
 | 
			
		||||
 | 
			
		||||
// TODO: Use this data haha!
 | 
			
		||||
console.log('Data, ready and waiting!', data)
 | 
			
		||||
 | 
			
		||||
app.engine('handlebars', engine())
 | 
			
		||||
app.set('view engine', 'handlebars')
 | 
			
		||||
| 
						 | 
				
			
			@ -9,7 +14,7 @@ app.set('views', './src/views')
 | 
			
		|||
 | 
			
		||||
app.use(express.static('dist'))
 | 
			
		||||
 | 
			
		||||
app.get('/', (req, res) => {
 | 
			
		||||
app.get('/', () => {
 | 
			
		||||
  res.render('home')
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										36
									
								
								data.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								data.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,36 @@
 | 
			
		|||
import fs from 'fs'
 | 
			
		||||
 | 
			
		||||
const databaseCSV = fs.readFileSync('database.csv', {encoding:'utf8', flag:'r'})
 | 
			
		||||
 | 
			
		||||
export function splitNewLines(string) {
 | 
			
		||||
  return string.split(/\n/)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function cleanupArrayOfStrings(arrayOfStrings) {
 | 
			
		||||
  return arrayOfStrings
 | 
			
		||||
    .map(line => line.trim())
 | 
			
		||||
    .filter(line => line != '')
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function stringCsvToDataObject(string) {
 | 
			
		||||
  let splitString = string.split(',')
 | 
			
		||||
  if (splitString[2].trim() == '') splitString[2] = splitString[1]
 | 
			
		||||
 | 
			
		||||
  const [
 | 
			
		||||
    nominative,
 | 
			
		||||
    accusative,
 | 
			
		||||
    pronominalPossessive,
 | 
			
		||||
    predicativePossessive,
 | 
			
		||||
    reflexive,
 | 
			
		||||
  ] = cleanupArrayOfStrings(splitString)
 | 
			
		||||
 | 
			
		||||
  return {
 | 
			
		||||
    nominative, accusative, pronominalPossessive, predicativePossessive, reflexive
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default function() {
 | 
			
		||||
  const databaseCSV = fs.readFileSync('database.csv', {encoding:'utf8', flag:'r'})
 | 
			
		||||
  return cleanupArrayOfStrings(splitNewLines(databaseCSV)).map(line => stringCsvToDataObject(line))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										3
									
								
								database.csv
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								database.csv
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
she,her,,hers,herself
 | 
			
		||||
he,him,,his,himself
 | 
			
		||||
they,them,,their,themself
 | 
			
		||||
		
		
			
  | 
							
								
								
									
										2278
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										2278
									
								
								package-lock.json
									
										
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							| 
						 | 
				
			
			@ -22,9 +22,11 @@
 | 
			
		|||
  "homepage": "https://gitlab.com/gaiety/pronoun-monster#readme",
 | 
			
		||||
  "type": "module",
 | 
			
		||||
  "devDependencies": {
 | 
			
		||||
    "ava": "^5.2.0",
 | 
			
		||||
    "concurrently": "^7.6.0",
 | 
			
		||||
    "express": "^4.18.2",
 | 
			
		||||
    "express-handlebars": "^7.0.2",
 | 
			
		||||
    "fs": "^0.0.1-security",
 | 
			
		||||
    "tailwindcss": "^3.2.7"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										55
									
								
								tests/data-test.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								tests/data-test.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,55 @@
 | 
			
		|||
import fs from 'fs'
 | 
			
		||||
import test from 'ava'
 | 
			
		||||
import fetchData, { splitNewLines, cleanupArrayOfStrings, stringCsvToDataObject } from '../data.js'
 | 
			
		||||
 | 
			
		||||
test('splitNewLines', t => {
 | 
			
		||||
  const result = splitNewLines(`foo\nbar`)
 | 
			
		||||
 | 
			
		||||
  t.deepEqual(result, ['foo', 'bar'])
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test('cleanupArrayOfStrings', t => {
 | 
			
		||||
  const result = cleanupArrayOfStrings([
 | 
			
		||||
    '',
 | 
			
		||||
    ' foo',
 | 
			
		||||
    'bar ',
 | 
			
		||||
    ''
 | 
			
		||||
  ])
 | 
			
		||||
 | 
			
		||||
  t.deepEqual(result, ['foo', 'bar'])
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test('clean an array made from a string of new lines', t => {
 | 
			
		||||
  const result = cleanupArrayOfStrings(
 | 
			
		||||
    splitNewLines(`
 | 
			
		||||
      foo
 | 
			
		||||
      bar
 | 
			
		||||
    `)
 | 
			
		||||
  )
 | 
			
		||||
 | 
			
		||||
  t.deepEqual(result, ['foo', 'bar'])
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test('stringCsvToDataObject', t => {
 | 
			
		||||
  const result = stringCsvToDataObject('they,them,them, their,themself')
 | 
			
		||||
 | 
			
		||||
  t.deepEqual(result, {
 | 
			
		||||
    nominative: 'they', accusative: 'them', pronominalPossessive: 'them', predicativePossessive: 'their', reflexive: 'themself'
 | 
			
		||||
  })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test('stringCsvToDataObject with missing pronominalPossessive', t => {
 | 
			
		||||
  const result = stringCsvToDataObject('they,them,,their,themself')
 | 
			
		||||
 | 
			
		||||
  t.deepEqual(result, {
 | 
			
		||||
    nominative: 'they', accusative: 'them', pronominalPossessive: 'them', predicativePossessive: 'their', reflexive: 'themself'
 | 
			
		||||
  })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test('database.csv loads into array of valid data', t => {
 | 
			
		||||
  const data = fetchData()
 | 
			
		||||
 | 
			
		||||
  t.truthy(Array.isArray(data))
 | 
			
		||||
  t.is(typeof data[0], 'object')
 | 
			
		||||
  t.is(typeof data[0].nominative, 'string')
 | 
			
		||||
})
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue