Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add browser support

  • Loading branch information...
commit 156b74870eb695aabdd68b098b135f230c244222 1 parent 7d91bbf
@vesln authored
View
9 Makefile
@@ -11,4 +11,11 @@ test:
--ui $(UI) \
$(TESTS)
-.PHONY: test
+browser: $(SRC)
+ @node support/compile $^
+
+clean:
+ @rm -f decorator.js
+ @rm -f decorator.min.js
+
+.PHONY: test browser clean
View
18 Readme.md
@@ -123,24 +123,36 @@ range().step(1).step();
## Installation
+Node.js:
+
```
$ npm install r...e
```
-## Requirements
+Browser:
+
+Download `range.min.js`, it's all you need.
-- Node.js >= 0.6.0
+```html
+<script src="range.min.js"></script>
+```
## Tests
+Node.js:
+
```
$ npm install
$ make test
```
+Browser:
+
+- Clone the repository
+- Open `test/browser/index.html` in your favourite browser
+
## TODO
-- Browser support
- Handle floats
## License
View
8 package.json
@@ -11,11 +11,13 @@
"type": "git",
"url": "https://github.com/vesln/r...e"
},
- "keywords": ["range"],
- "dependencies": {},
+ "keywords": [
+ "range"
+ ],
"devDependencies": {
"mocha": "*",
- "chai": "*"
+ "chai": "*",
+ "folio": "~0.3.4"
},
"author": "Veselin Todorov <hi@vesln.com>",
"license": "MIT",
View
332 range.js
@@ -0,0 +1,332 @@
+!function (name, context, definition) {
+ if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
+ module.exports = definition();
+ } else if (typeof define === 'function' && typeof define.amd === 'object') {
+ define(function () {
+ return definition();
+ });
+ } else {
+ context[name] = definition();
+ }
+}('range', this, function () {
+
+ function require(p) {
+ var path = require.resolve(p)
+ , mod = require.modules[path];
+ if (!mod) throw new Error('failed to require "' + p + '"');
+ if (!mod.exports) {
+ mod.exports = {};
+ mod.call(mod.exports, mod, mod.exports, require.relative(path));
+ }
+ return mod.exports;
+ }
+
+ require.modules = {};
+
+ require.resolve = function (path) {
+ var orig = path
+ , reg = path + '.js'
+ , index = path + '/index.js';
+ return require.modules[reg] && reg
+ || require.modules[index] && index
+ || orig;
+ };
+
+ require.register = function (path, fn) {
+ require.modules[path] = fn;
+ };
+
+ require.relative = function (parent) {
+ return function(p){
+ if ('.' != p.charAt(0)) return require(p);
+
+ var path = parent.split('/')
+ , segs = p.split('/');
+ path.pop();
+
+ for (var i = 0; i < segs.length; i++) {
+ var seg = segs[i];
+ if ('..' == seg) path.pop();
+ else if ('.' != seg) path.push(seg);
+ }
+
+ return require(path.join('/'));
+ };
+ };
+
+ require.alias = function (from, to) {
+ var fn = require.modules[from];
+ require.modules[to] = fn;
+ };
+
+
+ require.register("range.js", function(module, exports, require){
+
+ /*!
+ * Sequences.
+ */
+
+ var sequence = 'abcdefghijklmnopqrstuvwxyz'
+ , sequenceUpper = sequence.toUpperCase()
+ , sequence = sequence.split('')
+ , sequenceUpper = sequenceUpper.split('');
+
+ /**
+ * Range constructor.
+ *
+ * @param {Number|String} min
+ * @param {Number|String} max
+ * @param {Number} step
+ * @constructor
+ */
+
+ function Range(min, max, step) {
+ var scope = null
+ , re = /^([A-Za-z]|\d+)\.{2}([A-Za-z]|\d+)$/;
+
+ if (typeof min === 'string' && min.match(re)) {
+ step = max;
+ scope = min.split('..');
+ min = cast(scope[0]);
+ max = cast(scope[1]);
+ }
+
+ this.min(min);
+ this.max(max);
+ this.step(step);
+ };
+
+ /**
+ * Min accessor.
+ *
+ * @param {Number|String} value
+ * @returns {Range|Number|String} `this` or the value
+ * @api public
+ */
+
+ Range.prototype.min = function(min) {
+ if (arguments.length === 0) {
+ return this._min;
+ }
+
+ this._min = min;
+ return this;
+ };
+
+ /**
+ * Max accessor.
+ *
+ * @param {Number|String} value
+ * @returns {Range|Number|String} `this` or the value
+ * @api public
+ */
+
+ Range.prototype.max = function(max) {
+ if (arguments.length === 0) {
+ return this._max;
+ }
+
+ this._max = max;
+ return this;
+ };
+
+ /**
+ * Step accessor. Default to 1.
+ *
+ * @param {Number} value
+ * @returns {Range|Number} `this` or the value
+ * @api public
+ */
+
+ Range.prototype.step = function(step) {
+ if (arguments.length === 0) {
+ return this._step;
+ }
+
+ this._step = step === void 0 ? 1 : step;
+ return this;
+ };
+
+ /**
+ * Range to array.
+ *
+ * @returns {Array}
+ * @api public
+ */
+
+ Range.prototype.toArray = function() {
+ return typeof this.min() === 'number'
+ ? this.numericArray()
+ : this.alphaArray();
+ };
+
+ /**
+ * Build an array from numeric range.
+ *
+ * @returns {Array}
+ * @api private
+ */
+
+ Range.prototype.numericArray = function() {
+ var step = this.step()
+ , min = this.min()
+ , max = this.max()
+ , isFloat = typeof min === 'float'
+ , array = [];
+
+ while (min <= max) {
+ array[array.length] = min;
+ min += step;
+ }
+
+ return array;
+ };
+
+ /**
+ * Build an array from alpha range.
+ *
+ * @returns {Array}
+ * @api private
+ */
+
+ Range.prototype.alphaArray = function() {
+ var step = this.step()
+ , first = this.min()
+ , min = null
+ , max = null
+ , base = null
+ , array = [];
+
+ base = first === first.toUpperCase()
+ ? sequenceUpper
+ : sequence;
+
+ min = base.indexOf(first) - step;
+ max = base.indexOf(this.max());
+
+ while ((min += step) <= max) {
+ array[array.length] = base[min];
+ }
+
+ return array;
+ };
+
+ /**
+ * Check if the supplied value is an element
+ * of the range.
+ *
+ * @param {String|Number} value
+ * @returns {Boolean}
+ * @api public
+ */
+
+ Range.prototype.include = function(value) {
+ return !!~this.toArray().indexOf(value);
+ };
+
+ /**
+ * Each.
+ *
+ * @param {Function} callback
+ * @api public
+ */
+
+ Range.prototype.each = function(fn) {
+ this.toArray().forEach(fn);
+ };
+
+ /**
+ * Map.
+ *
+ * @param {Function} callback
+ * @returns {Array}
+ * @api public
+ */
+
+ Range.prototype.map = function(fn) {
+ return this.toArray().map(fn);
+ };
+
+ /**
+ * Join.
+ *
+ * @param {String} separator, default to ','
+ * @returns {String}
+ * @api public
+ */
+
+ Range.prototype.join = function(sep) {
+ sep = sep === void 0 ? ',' : sep;
+ return this.toArray().join(sep);
+ };
+
+ /**
+ * Check if the current range is equal to
+ * another range.
+ *
+ * @param {Range} range
+ * @returns {Boolean}
+ * @api public
+ */
+
+ Range.prototype.equal = function(range) {
+ return this.min() === range.min()
+ && this.max() === range.max()
+ && this.step() === range.step();
+ };
+
+ /**
+ * Sum the given range.
+ *
+ * @returns {Number}
+ * @api public
+ */
+
+ Range.prototype.sum = function() {
+ var total = 0;
+
+ this.each(function(n) {
+ total += n;
+ });
+
+ return total;
+ };
+
+ /*!
+ * Simple cast helper.
+ *
+ * @param {String|Number} n
+ * @returns {String|Number}
+ * @api private
+ */
+
+ function cast(n) {
+ if (n == +n) return +n;
+ return n;
+ };
+
+ /**
+ * Create a range.
+ *
+ * @param {Number|String} min
+ * @param {Number|String} max
+ * @param {Number} step
+ * @api public
+ */
+
+ module.exports = function(min, max, step) {
+ return new Range(min, max, step);
+ };
+
+ /*!
+ * Expose `Range`.
+ */
+
+ module.exports.Range = Range;
+
+ }); // module: range.js
+
+ require.alias("./range.js", "range");
+
+ return require('range');
+});
View
1  range.min.js
@@ -0,0 +1 @@
+!function(e,t,n){typeof require=="function"&&typeof exports=="object"&&typeof module=="object"?module.exports=n():typeof define=="function"&&typeof define.amd=="object"?define(function(){return n()}):t[e]=n()}("range",this,function(){function e(t){var n=e.resolve(t),r=e.modules[n];if(!r)throw new Error('failed to require "'+t+'"');return r.exports||(r.exports={},r.call(r.exports,r,r.exports,e.relative(n))),r.exports}return e.modules={},e.resolve=function(t){var n=t,r=t+".js",i=t+"/index.js";return e.modules[r]&&r||e.modules[i]&&i||n},e.register=function(t,n){e.modules[t]=n},e.relative=function(t){return function(n){if("."!=n.charAt(0))return e(n);var r=t.split("/"),i=n.split("/");r.pop();for(var s=0;s<i.length;s++){var o=i[s];".."==o?r.pop():"."!=o&&r.push(o)}return e(r.join("/"))}},e.alias=function(t,n){var r=e.modules[t];e.modules[n]=r},e.register("range.js",function(e,t,n){function s(e,t,n){var r=null,i=/^([A-Za-z]|\d+)\.{2}([A-Za-z]|\d+)$/;typeof e=="string"&&e.match(i)&&(n=t,r=e.split(".."),e=o(r[0]),t=o(r[1])),this.min(e),this.max(t),this.step(n)}function o(e){return e==+e?+e:e}var r="abcdefghijklmnopqrstuvwxyz",i=r.toUpperCase(),r=r.split(""),i=i.split("");s.prototype.min=function(e){return arguments.length===0?this._min:(this._min=e,this)},s.prototype.max=function(e){return arguments.length===0?this._max:(this._max=e,this)},s.prototype.step=function(e){return arguments.length===0?this._step:(this._step=e===void 0?1:e,this)},s.prototype.toArray=function(){return typeof this.min()=="number"?this.numericArray():this.alphaArray()},s.prototype.numericArray=function(){var e=this.step(),t=this.min(),n=this.max(),r=typeof t=="float",i=[];while(t<=n)i[i.length]=t,t+=e;return i},s.prototype.alphaArray=function(){var e=this.step(),t=this.min(),n=null,s=null,o=null,u=[];o=t===t.toUpperCase()?i:r,n=o.indexOf(t)-e,s=o.indexOf(this.max());while((n+=e)<=s)u[u.length]=o[n];return u},s.prototype.include=function(e){return!!~this.toArray().indexOf(e)},s.prototype.each=function(e){this.toArray().forEach(e)},s.prototype.map=function(e){return this.toArray().map(e)},s.prototype.join=function(e){return e=e===void 0?",":e,this.toArray().join(e)},s.prototype.equal=function(e){return this.min()===e.min()&&this.max()===e.max()&&this.step()===e.step()},s.prototype.sum=function(){var e=0;return this.each(function(t){e+=t}),e},e.exports=function(e,t,n){return new s(e,t,n)},e.exports.Range=s}),e.alias("./range.js","range"),e("range")})
View
43 support/compile.js
@@ -0,0 +1,43 @@
+var folio = require('folio');
+
+folio('range')
+ .root(__dirname, '..')
+ .use(folio.requires())
+ .dir('./lib')
+ .package('range')
+ .entry('./range.js')
+ .pop()
+ .use(folio.indent())
+ .line(' ')
+ .pop()
+ .use(folio.indent())
+ .line(' ')
+ .pop()
+ .use(folio.wrapper())
+ .prefix([
+ '!function (name, context, definition) {'
+ , ' if (typeof require === \'function\' && typeof exports === \'object\' && typeof module === \'object\') {'
+ , ' module.exports = definition();'
+ , ' } else if (typeof define === \'function\' && typeof define.amd === \'object\') {'
+ , ' define(function () {'
+ , ' return definition();'
+ , ' });'
+ , ' } else {'
+ , ' context[name] = definition();'
+ , ' }'
+ , '}(\'range\', this, function () {\n'
+ ].join('\n'))
+ .suffix([
+ '\n return require(\'range\');'
+ , '});'
+ ].join('\n'))
+ .pop()
+ .use(folio.save())
+ .file('./range.js')
+ .pop()
+ .use(folio.minify())
+ .pop()
+ .use(folio.save())
+ .file('./range.min.js')
+ .pop()
+ .compile();
View
23 test/browser/index.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Mocha</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
+ <script src="../../node_modules/mocha/mocha.js"></script>
+ <script src="../../node_modules/chai/chai.js"></script>
+ <script src="./require.js"></script>
+ <script src="../support/bootstrap.js"></script>
+ <script src="../../range.js"></script>
+ <script>mocha.setup('bdd')</script>
+ <script src="../range.test.js"></script>
+ <script>onload = function() {
+ mocha.run();
+ }
+ </script>
+ </head>
+ <body>
+ <div id="mocha"></div>
+ </body>
+</html>
View
1  test/browser/require.js
@@ -0,0 +1 @@
+function require() {}
View
2  test/range.test.js
@@ -3,7 +3,7 @@
* Internal dependencies.
*/
-var range = require('..');
+var range = range || require('../');
describe('range', function() {
it('can build a numeric range', function() {
View
2  test/support/bootstrap.js
@@ -3,7 +3,7 @@
* External dependencies.
*/
-var chai = require('chai');
+var chai = chai || require('chai');
/**
* Register `should`.
Please sign in to comment.
Something went wrong with that request. Please try again.