Skip to content

Instantly share code, notes, and snippets.

@aduth
Created February 5, 2013 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aduth/4716946 to your computer and use it in GitHub Desktop.
Save aduth/4716946 to your computer and use it in GitHub Desktop.
# Before
get: (fileName, complete) ->
throw new Error("File name `#{fileName}` has not been added yet") unless fileName of @ipsums
if @cached[fileName].length
# Pull from cache if available
complete null, @cached[fileName].shift()
else
# Else, generate fresh
complete null, @getFresh(fileName)
# Start refreshing cache
@cache fileName
# After
get: (fileName, numberOfWords, complete) ->
throw new Error("File name `#{fileName}` has not been added yet") unless fileName of @ipsums
throw new Error("Must be valid number of words") unless typeof numberOfWords is 'number' and numberOfWords >= 0
# Generate phrase
phrase = ''
for counter in [0..numberOfWords] by @options.cachePhraseWords
if @cached[fileName].length
# Pull from cache if available
phrase += @cached[fileName].shift()
else
# Else, generate fresh
phrase += @getFresh fileName
# Reduce phrase to numberOfWords words
rCleanEnd = new RegExp("[\\\\#{LibroIpsum.clauseSeparators.join('\\\\')}\\s]*$")
phrase = phrase.replace(rCleanEnd, '')
.split(' ')
.slice(0, numberOfWords)
.join(' ') + '.'
complete null, phrase
# Start refreshing cache
@cache fileName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment