From 674cedf1764f97de4b11ea126ce6dc3056998c28 Mon Sep 17 00:00:00 2001 From: Gus Hovland Date: Wed, 23 Jan 2013 00:02:22 +0100 Subject: [PATCH] made it run in node and added node test --- dist/lilrouter.js | 2 +- dist/lilrouter.min.js | 2 +- lib/lilrouter/win.js | 2 +- package.json | 2 +- test/buster.js | 8 ++++++++ test/lilrouter-test.js | 7 ++++--- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/dist/lilrouter.js b/dist/lilrouter.js index 5a2665b..4dd87a8 100644 --- a/dist/lilrouter.js +++ b/dist/lilrouter.js @@ -413,7 +413,7 @@ provide('lilrouter/win', function (require, module, exports) { newcap:true, noarg:true, sub:true, undef:true, boss:true, strict:false, eqnull:true, browser:true, node:true */ -var win = window; +var win = typeof window === 'object' && window; module.exports = function (winObj) { diff --git a/dist/lilrouter.min.js b/dist/lilrouter.min.js index ce85645..a620895 100644 --- a/dist/lilrouter.min.js +++ b/dist/lilrouter.min.js @@ -1,3 +1,3 @@ /*! lilrouter - v0.0.1 - 2013-01-22 * Copyright (c) 2013 August Hovland ; Licensed MIT */ -(function(e){"use strict";function r(e,t){if(t.indexOf(".")===-1)return t;t=t.split("/"),e=e?e.split("/"):[],e.pop(),t[0]==="."&&t.shift();while(t[0]==="..")t.shift(),e.pop();return e.concat(t).join("/")}var t={},n={};e.provide=function(e,r,i){return i?t[e]=r:n[e]=r},e.require=function(i,s){var o,u,a=s||i;return n[a]?n[a]:(o=n[a]={},u={exports:o},t[a](function(t){return e.require(t,r(a,t))},u,o),n[a]=u.exports)}})(this),provide("lil_",function(e,t,n){t.exports={typeOf:function(e){var t=typeof e;return t==="object"&&(t=Array.isArray(e)?"array":t,t=e===null?"null":t),t},each:function(e,t,n){var r=this.typeOf(e),i;r==="array"&&e.length?e.forEach(t,n):r==="object"&&(i=e?Object.keys(e):[],i.forEach(function(r,i){t.call(n,r,e[r],i)}))},every:function(e,t,n){var r=this.typeOf(e),i;return r==="array"&&e.length?e.every(t,n):r==="object"?(i=e?Object.keys(e):[],i.every(function(r,i){return t.call(n,r,e[r],i)})):!1},some:function(e,t,n){var r=this.typeOf(e),i;return r==="array"&&e.length?e.some(t,n):r==="object"?(i=e?Object.keys(e):[],i.some(function(r,i){return t.call(n,r,e[r],i)})):!1},map:function(e,t,n){var r=this.typeOf(e),i=[];return r==="array"&&e.length?e.map(t,n):(r==="object"&&(i={},this.each(e,function(e,n,r){i[e]=t.call(this,e,n,r)},n)),i)},withOut:function(e,t){var n=[];return this.each(e,function(e){e!==t&&n.push(e)}),n},walk:function(e,t,n,r){var i=this,s=function(e,t){i.each(t,function(t,n){o(e[t],n,t,e)})},o=function(e,t,o,u){var a=i.typeOf(t);a==="object"?(!e&&u&&r&&(e=u[o]={}),s(e,t)):n.call(u,e,t,o)};o(e,t)},extend:function(){var e=Array.prototype.slice.call(arguments),t=e.shift();return this.each(e,function(e){this.walk(t,e,function(e,t,n){this[n]=t},!0)},this),t},defaults:function(e,t){return this.walk(e,t,function(e,t,n){e||(this[n]=t)},!0),e},match:function(e,t){var n=!0;return this.walk(e,t,function(e,t){n=e===t}),n},pick:function(e,t){var n={};return t=this.typeOf(t)==="array"?t:Object.keys(t),this.each(t,function(t){n[t]=e&&e[t]}),n}}},!0),provide("lilobj/arr",function(e,t,n){function i(){var e=[];return e.push.apply(e,arguments),e.__proto__=i.prototype,e}var r=e("lil_");i.prototype=[],i.prototype.isA=function(e){function t(){}return t.prototype=e,this instanceof t},i.prototype.extend=function(e){i.prototype=this;var t=new i;return r.each(e,function(n){t[n]=e[n]}),t},i.prototype.create=function(){i.prototype=this;var e=new i;return e.construct!==undefined&&e.construct.apply(e,arguments),e},t.exports=new i},!0),provide("lilobj/obj",function(e,t,n){var r=e("lil_");t.exports={isA:function(e){function t(){}return t.prototype=e,this instanceof t},extend:function(e){var t=Object.create(this);return r.each(e,function(e,n){t[e]=n}),t},create:function(){var e=Object.create(this);return e.construct!==undefined&&e.construct.apply(e,arguments),e}}},!0),provide("lilobj",function(e,t,n){var r=e("./lilobj/obj"),i=e("./lilobj/arr");t.exports={obj:r,arr:i}},!0),provide("lilrouter/win",function(e,t,n){var r=window;t.exports=function(e){return e&&(r=e),{go:function(e,t){r.history.pushState(e,"",t)},location:function(e){return e&&(r.location=e),r.location.pathname},onpopstate:function(e,t){r.onpopstate=e.bind(t)}}}},!0),provide("lilrouter/matcher",function(e,t,n){var r=e("lil_");t.exports=function(e,t){var n=r.withOut(t.split("/"),""),i,s;return t==="/"?{handler:e["/"],params:{}}:(r.some(e,function(e,t){var o=r.withOut(e.split("/"),""),u=o.length===n.length;return i={},s=t,u&&r.every(o,function(e,t){return e.indexOf(":")===0?(i[e.substr(1)]=n[t],!0):e===n[t]?!0:!1})}),{handler:s,params:i})}},!0),provide("lilrouter/router",function(e,t,n){var r=e("lil_"),i=e("lilobj").obj,s=e("./matcher"),o=e("./win");t.exports=i.extend({win:undefined,start:undefined,routes:{get:{},post:{}},construct:function(e){this.win=o(),this.start=this.win.location(),r.each(this.routes,function(t){r.each(e[t],function(e,n){this.routes[t][e]=n},this)},this),this.ctx=e.init(this),this.win.onpopstate(function(e){e.state?this.route(e.state.method,this.win.location(),e.state.body):this.route("get",this.start)},this),this.route("get",this.start)},route:function(e,t,n){var i=s(this.routes[e],t),o=r.extend({},this.ctx,{params:i.params,body:n||{}});i.handler(o,this)},get:function(e){this.win.go({method:"get"},e),this.route("get",e)},post:function(e,t){this.win.go({method:"post",body:t},e),this.route("post",e,t)}})},!0),provide("lilrouter",function(e,t,n){var r=e("./lilrouter/router");t.exports=r},!0); \ No newline at end of file +(function(e){"use strict";function r(e,t){if(t.indexOf(".")===-1)return t;t=t.split("/"),e=e?e.split("/"):[],e.pop(),t[0]==="."&&t.shift();while(t[0]==="..")t.shift(),e.pop();return e.concat(t).join("/")}var t={},n={};e.provide=function(e,r,i){return i?t[e]=r:n[e]=r},e.require=function(i,s){var o,u,a=s||i;return n[a]?n[a]:(o=n[a]={},u={exports:o},t[a](function(t){return e.require(t,r(a,t))},u,o),n[a]=u.exports)}})(this),provide("lil_",function(e,t,n){t.exports={typeOf:function(e){var t=typeof e;return t==="object"&&(t=Array.isArray(e)?"array":t,t=e===null?"null":t),t},each:function(e,t,n){var r=this.typeOf(e),i;r==="array"&&e.length?e.forEach(t,n):r==="object"&&(i=e?Object.keys(e):[],i.forEach(function(r,i){t.call(n,r,e[r],i)}))},every:function(e,t,n){var r=this.typeOf(e),i;return r==="array"&&e.length?e.every(t,n):r==="object"?(i=e?Object.keys(e):[],i.every(function(r,i){return t.call(n,r,e[r],i)})):!1},some:function(e,t,n){var r=this.typeOf(e),i;return r==="array"&&e.length?e.some(t,n):r==="object"?(i=e?Object.keys(e):[],i.some(function(r,i){return t.call(n,r,e[r],i)})):!1},map:function(e,t,n){var r=this.typeOf(e),i=[];return r==="array"&&e.length?e.map(t,n):(r==="object"&&(i={},this.each(e,function(e,n,r){i[e]=t.call(this,e,n,r)},n)),i)},withOut:function(e,t){var n=[];return this.each(e,function(e){e!==t&&n.push(e)}),n},walk:function(e,t,n,r){var i=this,s=function(e,t){i.each(t,function(t,n){o(e[t],n,t,e)})},o=function(e,t,o,u){var a=i.typeOf(t);a==="object"?(!e&&u&&r&&(e=u[o]={}),s(e,t)):n.call(u,e,t,o)};o(e,t)},extend:function(){var e=Array.prototype.slice.call(arguments),t=e.shift();return this.each(e,function(e){this.walk(t,e,function(e,t,n){this[n]=t},!0)},this),t},defaults:function(e,t){return this.walk(e,t,function(e,t,n){e||(this[n]=t)},!0),e},match:function(e,t){var n=!0;return this.walk(e,t,function(e,t){n=e===t}),n},pick:function(e,t){var n={};return t=this.typeOf(t)==="array"?t:Object.keys(t),this.each(t,function(t){n[t]=e&&e[t]}),n}}},!0),provide("lilobj/arr",function(e,t,n){function i(){var e=[];return e.push.apply(e,arguments),e.__proto__=i.prototype,e}var r=e("lil_");i.prototype=[],i.prototype.isA=function(e){function t(){}return t.prototype=e,this instanceof t},i.prototype.extend=function(e){i.prototype=this;var t=new i;return r.each(e,function(n){t[n]=e[n]}),t},i.prototype.create=function(){i.prototype=this;var e=new i;return e.construct!==undefined&&e.construct.apply(e,arguments),e},t.exports=new i},!0),provide("lilobj/obj",function(e,t,n){var r=e("lil_");t.exports={isA:function(e){function t(){}return t.prototype=e,this instanceof t},extend:function(e){var t=Object.create(this);return r.each(e,function(e,n){t[e]=n}),t},create:function(){var e=Object.create(this);return e.construct!==undefined&&e.construct.apply(e,arguments),e}}},!0),provide("lilobj",function(e,t,n){var r=e("./lilobj/obj"),i=e("./lilobj/arr");t.exports={obj:r,arr:i}},!0),provide("lilrouter/win",function(e,t,n){var r=typeof window=="object"&&window;t.exports=function(e){return e&&(r=e),{go:function(e,t){r.history.pushState(e,"",t)},location:function(e){return e&&(r.location=e),r.location.pathname},onpopstate:function(e,t){r.onpopstate=e.bind(t)}}}},!0),provide("lilrouter/matcher",function(e,t,n){var r=e("lil_");t.exports=function(e,t){var n=r.withOut(t.split("/"),""),i,s;return t==="/"?{handler:e["/"],params:{}}:(r.some(e,function(e,t){var o=r.withOut(e.split("/"),""),u=o.length===n.length;return i={},s=t,u&&r.every(o,function(e,t){return e.indexOf(":")===0?(i[e.substr(1)]=n[t],!0):e===n[t]?!0:!1})}),{handler:s,params:i})}},!0),provide("lilrouter/router",function(e,t,n){var r=e("lil_"),i=e("lilobj").obj,s=e("./matcher"),o=e("./win");t.exports=i.extend({win:undefined,start:undefined,routes:{get:{},post:{}},construct:function(e){this.win=o(),this.start=this.win.location(),r.each(this.routes,function(t){r.each(e[t],function(e,n){this.routes[t][e]=n},this)},this),this.ctx=e.init(this),this.win.onpopstate(function(e){e.state?this.route(e.state.method,this.win.location(),e.state.body):this.route("get",this.start)},this),this.route("get",this.start)},route:function(e,t,n){var i=s(this.routes[e],t),o=r.extend({},this.ctx,{params:i.params,body:n||{}});i.handler(o,this)},get:function(e){this.win.go({method:"get"},e),this.route("get",e)},post:function(e,t){this.win.go({method:"post",body:t},e),this.route("post",e,t)}})},!0),provide("lilrouter",function(e,t,n){var r=e("./lilrouter/router");t.exports=r},!0); \ No newline at end of file diff --git a/lib/lilrouter/win.js b/lib/lilrouter/win.js index 2445836..a53d24c 100644 --- a/lib/lilrouter/win.js +++ b/lib/lilrouter/win.js @@ -2,7 +2,7 @@ newcap:true, noarg:true, sub:true, undef:true, boss:true, strict:false, eqnull:true, browser:true, node:true */ -var win = window; +var win = typeof window === 'object' && window; module.exports = function (winObj) { diff --git a/package.json b/package.json index 0deff37..9d5a0b2 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "August Hovland ", "name": "lilrouter", "description": "A li'l app router for the browser", - "version": "0.0.1", + "version": "0.0.2", "repository": { "type": "git", "url": "git://github.com/gushov/lilrouter.git" diff --git a/test/buster.js b/test/buster.js index 4971912..6a87559 100644 --- a/test/buster.js +++ b/test/buster.js @@ -1,5 +1,13 @@ var config = module.exports; +config["lilrouter node tests"] = { + rootPath: "../", + environment: "node", + tests: [ + "test/*-test.js" + ] +}; + config["lilrouter browser tests"] = { rootPath: "../", environment: "browser", diff --git a/test/lilrouter-test.js b/test/lilrouter-test.js index 2ebd6ad..b259968 100644 --- a/test/lilrouter-test.js +++ b/test/lilrouter-test.js @@ -1,10 +1,11 @@ /*jshint curly:true, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true, sub:true, undef:true, boss:true, strict:false, eqnull:true, browser:true, node:true */ -/*global buster, assert, refute */ +/*global assert, refute */ -var win = require('lilrouter/win'); -var lilRouter = require('lilrouter'); +var buster = typeof buster !== 'undefined' ? buster : require("buster"); +var win = typeof module !== 'undefined' ? require('../lib/lilrouter/win') : require('lilrouter/win'); +var lilRouter = typeof module !== 'undefined' ? require('../lib/lilrouter') : require('lilrouter'); buster.testCase("lilrouter", {