Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable lists #7

Merged
merged 3 commits into from
Jan 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions public/js/wtfismygenericthing.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function extract(list) {
var index = Math.floor(Math.random() * list.length)
a = {}
a['victim'] = list[index]
list.splice(index, 1)
a['remainder'] = list

return(a)
}

function isLetter(char) {
return('abcdefghijklmnopqrstuvwxyz@'.indexOf(char.toLowerCase()) > -1)
}
Expand Down Expand Up @@ -56,18 +66,20 @@ function chunks(string) {
return separate(string)
}

function substitute(chunk, json) {
if(isPlaceholder(chunk)) {
return getRandom(json[stripFirst(chunk)])
} else {
return chunk
}
}

function populateTemplate(template, json) {
var populated = []
$.each(chunks(template), function(index, chunk) {
populated.push(substitute(chunk, json))

subs = ''
if(isPlaceholder(chunk)) {
extracted = extract(json[stripFirst(chunk)])
subs = extracted['victim']
json[chunk] = extracted['remainder']
} else {
subs = chunk
}
populated.push(subs)

})
complete = populated.join('')

Expand Down
88 changes: 88 additions & 0 deletions spec/javascripts/list_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
describe('Resusable lists', function() {
describe('return a list item and remove it from the list', function() {
it('pulls out an item', function() {
items = [
'Brockway',
'North Haverbrook',
'Ogdenville'
]
var extracted = extract(items)
expect(extracted['remainder'].length).toEqual(2)

switch(extracted['victim']) {
case 'Brockway':
expect(extracted['remainder']).toEqual([
'North Haverbrook',
'Ogdenville'
])
break

case 'North Haverbrook':
expect(extracted['remainder']).toEqual([
'Brockway',
'Ogdenville'
])
break

case 'Ogdenville':
expect(extracted['remainder']).toEqual([
'Brockway',
'North Haverbrook'
])
}
})
})

describe('populate a template with a repeated placeholder', function() {
var json = {
'templates': [
'@these and @these'
],
'these': [
'this',
'that'
]
}

it('reuses the list', function() {
templated = template(json)
switch(templated.substring(0, 4)) {
case 'this':
expect(templated).toEqual('this and that')
break

case 'that':
expect(templated).toEqual('that and this')
break
}
})

describe('with a recursive template', function() {
var json = {
'templates': [
'@things and @stuff'
],
'things': [
'Lions',
'Tigers',
'Bears'
],
'stuff': [
'@things and @things'
]
}

it('reuses the list when things get recursive', function() {
templated = template(json)
words = templated.split(' ').sort()
expect(words).toEqual([
'Bears',
'Lions',
'Tigers',
'and',
'and'
])
})
})
})
})
9 changes: 0 additions & 9 deletions spec/javascripts/wtfismygenericthing_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ describe('Templates', function() {
]
}

describe('replace', function() {
it('replaces a placeholder', function() {
expect(substitute('@things', json)).toEqual('cats')
})
it('ignores a non-placeholder', function() {
expect(substitute('plain', json)).toEqual('plain')
})
})

describe('populateTemplate', function() {
var template = 'Put @things into @stuff'
it('fills a simple template', function() {
Expand Down