Permalink
Browse files

added support, tests and documentation for String ranges

  • Loading branch information...
1 parent 52c9a52 commit 26c2e548d3b1e6cca3b34e0ee2d6c1908c9e26e5 @constantology committed Dec 19, 2012
Showing with 74 additions and 2 deletions.
  1. +17 −0 README.md
  2. +30 −0 m8.js
  3. +1 −1 m8.min.js
  4. +13 −0 src/lib.js
  5. +13 −1 test/m8.test.js
View
@@ -579,6 +579,23 @@ Creates an empty Object using `Object.create( null )`, the Object has no constru
Optionally pass an Object whose properties you want copied to the empty Object instance.
+### m8.range( begin:Number|String, end:Number|String ):Array
+Returns an Array starting at `begin` where each value is incremented by `1` until `end` is reached.
+
+#### Example:
+
+```javascript
+
+ m8.range( 1, 10 ); // returns => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ m8.range( 20, 1000 ); // returns => [20, 21, 22, ..., 1000]
+
+ m8.range( 'A', 'z' ); // returns => ['A', 'B', 'C', ..., 'x', 'y', 'z']
+ m8.range( 'α', 'ω' ); // returns => ['α', 'β', 'γ', ..., 'χ', 'ψ', 'ω']
+
+```
+
+**NOTE:** Only the first character will be incremented in a `String` range.
+
## m8.remove( item:Array, value_or_index1:Number|Mixed|Number[]|Mixed[][, value_or_index2:Number|Mixed, ..., value_or_indexN:Number|Mixed] ):item
## m8.remove( item:Object, property1:String|String[][, property2:String, ..., propertyN:String] ):item
Removes items from the passed Array or Object and returns the passed Array or Object.
View
30 m8.js
@@ -1,7 +1,10 @@
;!function( root, Name, PACKAGE ) {
"use strict";
+
+
/*~ src/vars.js ~*/
+
// if ENV === commonjs we want root to be global
typeof global == 'undefined' ? root : ( root = global );
@@ -59,7 +62,10 @@
'Number' : [], 'Object' : [], 'RegExp' : [], 'String' : []
};
+
+
/*~ src/lib.js ~*/
+
function __lib__( val ) { return val; }
function bless( ns, ctx ) {
@@ -300,10 +306,23 @@
}
function range( i, j ) {
+ return isNaN( i ) ? range_str( i, j ) : range_num( i, j );
+ }
+ function range_num( i, j ) {
var a = [i];
while ( ++i <= j ) a.push( i );
return a;
}
+ function range_str( i, j ) {
+ i = String( i ).charCodeAt( 0 );
+ j = String( j ).charCodeAt( 0 );
+
+ var a = [], m = -1, n = Math.abs( i - j ); --i;
+
+ while ( ++m <= n ) a.push( String.fromCharCode( ++i ) );
+
+ return a;
+ }
function remove( item, keys ) {
keys = Array.isArray( keys ) ? keys : slice.call( arguments, 1 );
@@ -374,7 +393,10 @@
return o;
}
+
+
/*~ src/lib.x.js ~*/
+
// Commonjs Modules 1.1.1: http://wiki.commonjs.org/wiki/Modules/1.1.1
// notes section: http://wiki.commonjs.org/wiki/Modules/ProposalForNativeExtension
// specifies the possibility of sandboxing JavaScript Natives in Modules in future versions
@@ -401,7 +423,10 @@
Type[__xid__] = extenders.length; // assigned every time __lib__.x() is called, and
} // potentilly throwing overwrite errors.
+
+
/*~ src/nativex.js ~*/
+
x.cache( 'Array', function( Type ) {
var PROTO = Type.prototype;
@@ -542,7 +567,10 @@
}, 'w' );
} );
+
+
/*~ src/expose.js ~*/
+
iter( PACKAGE ) || ( PACKAGE = ENV == 'commonjs' ? module : root );
defs( ( __lib__ = expose( __lib__, Name, PACKAGE ) ), {
@@ -569,4 +597,6 @@
x( Object, Array, Boolean, Function );
+
+
}( this, 'm8' );
Oops, something went wrong.

0 comments on commit 26c2e54

Please sign in to comment.