Created
February 5, 2013 19:30
-
-
Save aduth/4716946 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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