Merge branch 'main' into env-and-templates

This commit is contained in:
Angela Quinton 2023-03-12 17:45:37 -04:00
commit 76c684752e
6 changed files with 2392 additions and 1 deletions

19
app.js
View file

@ -1,7 +1,12 @@
import express from 'express' import express from 'express'
import { engine } from 'express-handlebars' import { engine } from 'express-handlebars'
import fetchData from './data.js'
const app = express() const app = express()
const data = fetchData()
// TODO: Use this data haha!
console.log('Data, ready and waiting!', data)
app.engine('handlebars', engine()) app.engine('handlebars', engine())
app.set('view engine', 'handlebars') app.set('view engine', 'handlebars')
@ -11,11 +16,23 @@ app.use(express.static('dist'))
const siteName = "Pronoun Site" const siteName = "Pronoun Site"
app.get('/', (req, res) => { app.get('/', () => {
const pageTitle = siteName; const pageTitle = siteName;
res.render('home', { siteName, pageTitle } ) res.render('home', { siteName, pageTitle } )
}) })
app.get('/:nominative/:accusative/:predicative_possessive/:reflexive', (req, res) => {
const {
nominative,
accusative,
predicative_possessive: predicativePossessive,
reflexive,
} = req.params
res.render('individual', {
nominative, accusative, pronominalPossessive: accusative, predicativePossessive, reflexive
})
})
app.get('/:nominative/:accusative/:pronominal_possessive/:predicative_possessive/:reflexive', (req, res) => { app.get('/:nominative/:accusative/:pronominal_possessive/:predicative_possessive/:reflexive', (req, res) => {
const { const {
nominative, nominative,

36
data.js Normal file
View 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
View file

@ -0,0 +1,3 @@
she,her,,hers,herself
he,him,,his,himself
they,them,,their,themself
1 she her hers herself
2 he him his himself
3 they them their themself

2278
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -22,9 +22,11 @@
"homepage": "https://gitlab.com/gaiety/pronoun-monster#readme", "homepage": "https://gitlab.com/gaiety/pronoun-monster#readme",
"type": "module", "type": "module",
"devDependencies": { "devDependencies": {
"ava": "^5.2.0",
"concurrently": "^7.6.0", "concurrently": "^7.6.0",
"express": "^4.18.2", "express": "^4.18.2",
"express-handlebars": "^7.0.2", "express-handlebars": "^7.0.2",
"fs": "^0.0.1-security",
"tailwindcss": "^3.2.7" "tailwindcss": "^3.2.7"
} }
} }

55
tests/data-test.js Normal file
View 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')
})