Skip to content

Commit

Permalink
Refactoring comma-separated sequences into a parse_sequence method
Browse files Browse the repository at this point in the history
  • Loading branch information
avk committed May 26, 2011
1 parent 284f51b commit 271cacc
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions lib/jquery.chrono.js
Expand Up @@ -187,6 +187,46 @@ jQueryChrono = (function() {
return parsed;
}

/**
* Parses a string sequence of delay with unit arguments.
*
* @param {Object} parsed The arguments parsed so far
* @param {arguments} args The original arguments from the caller
* (e.g. {@link jQueryChrono.create_timer})
* @throws Exception if the sequence contains blanks, invalid delays, or invalid units
* @returns {Object} The parsed parameter updated with the parsed delay
* and units, each set to the minimum unit in the sequence
* e.g. "1 minute, 15 seconds" => parsed.delay: 75, parsed.units: seconds
*/
function parse_sequence(parsed, args) {
var commaArgs, name, minInterval = '', timer, timers = [];

// if the first arg is a string, try splitting it on commas
commaArgs = (typeof args[0] === 'string') ? args[0].split(',') : [];

// create a timer for each sequence element
for (name in commaArgs) {
if (! /\d\s?\w+/.test(commaArgs[name])) {
$.error("$.after and $.every - Invalid delays with units sequence: " +
commaArgs.join(','));
}
timer = create_timer.call(this, commaArgs[name], parsed.callback);

// keep track of the minimum interval so we can convert the whole set to this in the next loop
if (minInterval === '' || valid_units[timer.units] <= valid_units[minInterval]) {
minInterval = timer.units;
}
timers[name] = timer;
}
parsed.units = minInterval;

// convert each timer to the lowest interval, then add those units to parsed.delay
for (name in timers) {
parsed.delay += timers[name].delay * (valid_units[timers[name].units] / valid_units[minInterval]);
}
return parsed;
}

/**
* Accepts more human-readable arguments for creating JavaScript timers and
* converts them to values that can be inspected and passed along to
Expand Down Expand Up @@ -216,43 +256,16 @@ jQueryChrono = (function() {
units : null,
when : null,
callback : null
},
commaArgs,
name,
minInterval = '',
timers = [];
};

if (arguments.length < 2 || arguments.length > 3) {
$.error("$.after and $.every - Accept only 2 or 3 arguments");
}

parsed = parse_callback(parsed, arguments);

//if the first arg is a string, try splitting it on commas
commaArgs = typeof arguments[0] === 'string' ? arguments[0].split(',') : [];

//if there are multiple items separated by commas, create a timer for each of them
if (commaArgs.length > 1) {
for (name in commaArgs) {
if (! /\d\s?\w+/.test(commaArgs[name])) {
$.error("$.after and $.every - Invalid delays with units sequence: " +
commaArgs.join(','));
}
timer = create_timer.call(this, commaArgs[name], parsed.callback);

//keep track of the minimum interval so we can convert the whole set to this in the next loop
minInterval = ( minInterval === '' || valid_units[timer.units] <= valid_units[minInterval] ) ? timer.units : minInterval;
timers[name] = timer;
}

//set the units on the main parsed object
parsed.units = minInterval;

//iterate over the timers and convert all to the lowest interval, then add those units to parsed.delay
for (name in timers) {
parsed.delay += timers[name].delay * (valid_units[timers[name].units] / valid_units[minInterval]);
}
} else {
if (typeof arguments[0] === 'string' && arguments[0].search(',') > -1) {
parsed = parse_sequence(parsed, arguments);
} else {
parsed = parse_delay(parsed, arguments);
parsed = parse_units(parsed, arguments);
}
Expand Down

0 comments on commit 271cacc

Please sign in to comment.