Skip to content

Commit

Permalink
Merge pull request #7 from pikesley/reusable-lists
Browse files Browse the repository at this point in the history
Reusable lists. Closes #6
  • Loading branch information
Sam Pikesley committed Jan 18, 2016
2 parents 5c190ca + 8a1b169 commit 00c571e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 18 deletions.
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

0 comments on commit 00c571e

Please sign in to comment.