Skip to content

Commit

Permalink
Added Cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
jim committed May 19, 2008
1 parent a8294a2 commit 5d32fef
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2008 Jim Benton

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Empty file removed README
Empty file.
5 changes: 5 additions & 0 deletions README.markdown
@@ -0,0 +1,5 @@
# Gismos

A collection of JavaScript utilities and sub-libraries I've built over the course of many projects.

README should be included in each folder describing usage and any dependencies.
19 changes: 19 additions & 0 deletions cycle/README.markdown
@@ -0,0 +1,19 @@
## Cycle - a Javascript implementation of Rails' Text Helper

Built to ease zebra-striping a table. I've avoided any JS library-provided niceties to maximize portability. No support for naming.

Basic usage:

>>> Cycle.thru('one', 'two', 'three');
"one"
>>> Cycle.thru('one', 'two', 'three');
"two"
>>> Cycle.thru('one', 'two', 'three');
"three"
>>> Cycle.thru('one', 'two', 'three');
"one"
>>> Cycle.thru('one', 'two', 'three');
"two"
>>> Cycle.reset('one', 'two', 'three');
>>> Cycle.thru('one', 'two', 'three');
"one"
32 changes: 32 additions & 0 deletions cycle/cycle.js
@@ -0,0 +1,32 @@
var Cycle = {
cycles: [],
thru: function() {
var values = Array.prototype.slice.call(arguments);
for(var i=0,len=this.cycles.length;i<len;i++) {
var cycle = this.cycles[i];
if(cycle.ref == this.toRef(values)) {
var next_value = cycle.values[cycle.index];
cycle.index = (cycle.index == cycle.values.length -1) ? 0 : (cycle.index +1)
return next_value;
}
}
this.add(values);
return this.thru.apply(this, values);
},
add: function(values) {
this.cycles.push({
ref: this.toRef(values),
values: values,
index: 0
});
},
reset: function() {
var values = Array.prototype.slice.call(arguments);
for(var i=0,len=this.cycles.length;i<len;i++) {
if(this.cycles[i].ref == this.toRef(values)) this.cycles[i].index = 0;
}
},
toRef: function(array) {
return array.join('-');
}
};

0 comments on commit 5d32fef

Please sign in to comment.