Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

small change in toString() and updated documentation

  • Loading branch information...
commit f7d36313d5346d95220b720e9cacc697b9390f93 1 parent 827c663
Michael authored
Showing with 44 additions and 16 deletions.
  1. +43 −15 README.md
  2. +1 −1  Source/Range.js
View
58 README.md
@@ -1,13 +1,18 @@
Range
=====
-A class to represent a range of values, inspired by [Ruby's Range class](http://ruby-doc.org/core/classes/Range.html)
+A class to represent a range of values, inspired by
+[Ruby's Range class](http://ruby-doc.org/core/classes/Range.html)
How To Use
----------
-A range can be generated with any class that implements the compare and next methods. The compare method acts as the UFO operator, and the next method should return the next logical member of the given class. The Number.Comparable implementation included with the Range class can be seen below.
+A range can be generated with any class that implements the compare
+and next methods. The compare method acts as the UFO operator, and
+the next method should return the next logical member of the given
+class. The Number.Comparable implementation included with the Range
+class can be seen below.
Number.implement({
next: function(){ return this+1; },
@@ -23,11 +28,14 @@ Generate a Range of Numbers
var twoDigitIntegers = new Range(10,99); // (10..99)
-By default, the Range includes the ending value. Use the "inclusive" option to change this behaviour.
+By default, the Range includes the ending value. Use the "inclusive"
+option to change this behaviour.
var twoDigitIntegers = new Range(10,100,{inclusive:false}); // (10...100)
-See [Ruby's Range class documentation](http://ruby-doc.org/core/classes/Range.html) for more examples on using a "comparable" class that supports the "next" (succ) and "compare" (UFO operator) methods.
+See [Ruby's Range class documentation](http://ruby-doc.org/core/classes/Range.html)
+for more examples on using a "comparable" class that supports the
+ "next" (succ) and "compare" (UFO operator) methods.
Other Features
--------------
@@ -40,7 +48,8 @@ Simply returns the element that begins the range.
(new Range(5,9)).first() // 5
### last
-Returns the element defined as the range end, regardless of inclusiveness.
+Returns the element defined as the range end, regardless of
+inclusiveness.
var myRange = new Range(0,20) // [Object:myRange]
range.toArray().getLast() // 20
@@ -51,15 +60,21 @@ Returns the element defined as the range end, regardless of inclusiveness.
range.last() // 20
### toArray
-Calculates all values between the supplied beginning and end values, returning them as an array. The end value will be included unless explicitly defined in the options passed to the Range constructor.
+Calculates all values between the supplied beginning and end values,
+returning them as an array. The end value will be included unless
+explicitly defined in the options passed to the Range constructor.
(new Range('a','f')).toArray().join('') // "abcdef"
(new Range(3,6,{inclusive:false})).toArray().join('') // "345"
-toArray results are cached. Once toArray is called, subsequent calls will be much faster. Other Range methods that iterate over all values or need access to values at arbitrary positions will also run faster after a call to toArray has been made.
+toArray results are cached. Once toArray is called, subsequent calls
+will be much faster. Other Range methods that iterate over all values
+or need access to values at arbitrary positions will also run faster
+after a call to toArray has been made.
### each(func(value,index,range),bind)
-Iterates over every element of the range (respecting end insclusivity), calling the provided function on the element.
+Iterates over every element of the range (respecting end
+insclusivity), calling the provided function on the element.
var squares = {}
(new Range(-1,3)).each(function(i){
@@ -68,7 +83,8 @@ Iterates over every element of the range (respecting end insclusivity), calling
console.log(squares) // { -1:1, 0:0, 1:1, 2:4, 3:9 }
### step(step,func(value,index,range),bind)
-Iterates over every element of the range much like the each method, skipping {step}-1 elements between iterations.
+Iterates over every element of the range much like the each method,
+skipping {step}-1 elements between iterations.
(new Range(0,50)).step(25,function(val,i){
console.log(i+' => '+val)
@@ -94,7 +110,8 @@ Determines if the Range contains the given value.
excl.contains(3) // false
### valueAt(index)
-Returns the value at the given index in the range. Negative numbers are supported.
+Returns the value at the given index in the range. Negative numbers
+are supported.
var range = new Range(20,30) // (20..30)
range.valueAt(0) // 20
@@ -111,7 +128,9 @@ Returns the value at the given index in the range. Negative numbers are supporte
range.valueAt(-2) // 28
### equals(other)
-Checks equality (read: equivalency) of two ranges. If a range passes this test, it should behave exactly the same as this range. Note Note the known issues listed below.
+Checks equality (read: equivalency) of two ranges. If a range passes
+this test, it should behave exactly the same as this range. Note Note
+the known issues listed below.
(new Range(0,1)).equals(new Range(0,1)) // true
(new Range(0,1)).equals(new Range(0,2)) // false
@@ -121,10 +140,13 @@ Checks equality (read: equivalency) of two ranges. If a range passes this test,
## Options
### inclusive
-Determines if the end value is included as part of the range in all instance methods. Defaults to true.
+Determines if the end value is included as part of the range in all
+instance methods. Defaults to true.
### nextMethod
-An optional function to use instead of the Class's next method. Allows for some very interesting ranges. Defaults to (begin.next || $lambda($chk(end) ? end : begin)).
+An optional function to use instead of the Class's next method.
+Allows for some very interesting ranges. Defaults to
+(begin.next || $lambda($chk(end) ? end : begin)).
new Range(2,250,{nextMethod:function(){
return this.pow(1.2).ceil();
@@ -134,7 +156,13 @@ An optional function to use instead of the Class's next method. Allows for some
Known Issues
------------
-The equality check in the equals method is currently slightly flawed, as I have no way of determining if the provided (or inferred) next function is equal to the other range's next function. For now, it is comparing that the toString methods of the functions return the same value. This method happens to provide the correct result in the vast majority of cases, but I am convinced it is not the best way to compare functions.
+The equality check in the equals method is currently slightly flawed,
+as I have no way of determining if the provided (or inferred) next
+function is equal to the other range's next function. For now, it is
+comparing that the toString methods of the functions return the same
+value. This method happens to provide the correct result in the vast
+majority of cases, but I am convinced it is not the best way to
+compare functions.
Additional Info
---------------
@@ -142,4 +170,4 @@ Additional Info
I am always open for feature requests or any feedback.
I can be reached at [Github](http://github.com/michaelficarra).
-Thanks go out to the Ruby community for the original idea and implementation.
+Thanks to the Ruby community for the original idea and implementation.
View
2  Source/Range.js
@@ -79,7 +79,7 @@ var Range = new Class({
&& range.options.nextMethod.toString() == this.options.nextMethod.toString()
);
},
- toString: function(){ return '('+this.begin.inspect()+(this.options.inclusive ? '..' : '...')+this.end.inspect()+')'; }
+ toString: function(){ return '('+this.begin.toString()+(this.options.inclusive ? '..' : '...')+this.end.toString()+')'; }
});
/* Copyright 2010 Michael Ficarra
Please sign in to comment.
Something went wrong with that request. Please try again.