From c67ae77454ba764b4dbede91444167918e461b98 Mon Sep 17 00:00:00 2001 From: pikesley Date: Mon, 18 Jan 2016 20:51:00 +0000 Subject: [PATCH 1/3] A promising start --- public/js/wtfismygenericthing.js | 11 ++++++++++ spec/javascripts/list_spec.js | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 spec/javascripts/list_spec.js diff --git a/public/js/wtfismygenericthing.js b/public/js/wtfismygenericthing.js index 8cbd6dc..135def1 100644 --- a/public/js/wtfismygenericthing.js +++ b/public/js/wtfismygenericthing.js @@ -1,3 +1,14 @@ +function extract(list) { + var index = Math.floor(Math.random() * list.length) + index = 1 + a = {} + a['victim'] = list[index] + list.splice(index, 1) + a['remainder'] = list + + return(a) +} + function isLetter(char) { return('abcdefghijklmnopqrstuvwxyz@'.indexOf(char.toLowerCase()) > -1) } diff --git a/spec/javascripts/list_spec.js b/spec/javascripts/list_spec.js new file mode 100644 index 0000000..f2e892f --- /dev/null +++ b/spec/javascripts/list_spec.js @@ -0,0 +1,35 @@ +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' + ]) + } + }) + }) +}) From 279e49aa2591b2196d708a7fe8cf374550a018cc Mon Sep 17 00:00:00 2001 From: pikesley Date: Mon, 18 Jan 2016 21:13:07 +0000 Subject: [PATCH 2/3] Reuse the same placeholder --- public/js/wtfismygenericthing.js | 21 ++++++++-------- spec/javascripts/list_spec.js | 25 ++++++++++++++++++++ spec/javascripts/wtfismygenericthing_spec.js | 9 ------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/public/js/wtfismygenericthing.js b/public/js/wtfismygenericthing.js index 135def1..91b8c08 100644 --- a/public/js/wtfismygenericthing.js +++ b/public/js/wtfismygenericthing.js @@ -1,6 +1,5 @@ function extract(list) { var index = Math.floor(Math.random() * list.length) - index = 1 a = {} a['victim'] = list[index] list.splice(index, 1) @@ -67,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 index f2e892f..0821fa2 100644 --- a/spec/javascripts/list_spec.js +++ b/spec/javascripts/list_spec.js @@ -32,4 +32,29 @@ describe('Resusable lists', function() { } }) }) + + 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 + } + }) + }) }) 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() { From 8a1b16936099164777edbddce4d859f2d7e7b3ae Mon Sep 17 00:00:00 2001 From: pikesley Date: Mon, 18 Jan 2016 21:34:41 +0000 Subject: [PATCH 3/3] Woop --- spec/javascripts/list_spec.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/javascripts/list_spec.js b/spec/javascripts/list_spec.js index 0821fa2..4f353c0 100644 --- a/spec/javascripts/list_spec.js +++ b/spec/javascripts/list_spec.js @@ -56,5 +56,33 @@ describe('Resusable lists', function() { 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' + ]) + }) + }) }) })