diff --git a/public/js/wtfismygenericthing.js b/public/js/wtfismygenericthing.js index 8cbd6dc..91b8c08 100644 --- a/public/js/wtfismygenericthing.js +++ b/public/js/wtfismygenericthing.js @@ -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) } @@ -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('') diff --git a/spec/javascripts/list_spec.js b/spec/javascripts/list_spec.js new file mode 100644 index 0000000..4f353c0 --- /dev/null +++ b/spec/javascripts/list_spec.js @@ -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' + ]) + }) + }) + }) +}) diff --git a/spec/javascripts/wtfismygenericthing_spec.js b/spec/javascripts/wtfismygenericthing_spec.js index 12ad8a6..6816783 100644 --- a/spec/javascripts/wtfismygenericthing_spec.js +++ b/spec/javascripts/wtfismygenericthing_spec.js @@ -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() {