Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring comma-separated sequences into a parse_sequence method

  • Loading branch information...
commit 271caccb23421b29c334f1035219d1b0af28cf14 1 parent 284f51b
@avk authored
Showing with 44 additions and 31 deletions.
  1. +44 −31 lib/jquery.chrono.js
View
75 lib/jquery.chrono.js
@@ -188,6 +188,46 @@ jQueryChrono = (function() {
}
/**
+ * 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
* setTimeout or setInterval.<br />
@@ -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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.