Comparing version
var boxcars = (function(undefined){ | ||
var XHR; | ||
var slice = Array.prototype.slice, | ||
node = typeof process != 'undefined' && process.versions && process.versions.node != undefined, | ||
get = node && nodeGet || browserGet, | ||
if (typeof XMLHttpRequest != "undefined"){ | ||
XHR, libhttp, libhttps, liburl; | ||
if(node){ | ||
libhttp = require('http'); | ||
libhttps = require('https'); | ||
liburl = require('url'); | ||
} else if (typeof XMLHttpRequest != "undefined"){ | ||
XHR = XMLHttpRequest; | ||
} else { | ||
XHR = function XMLHttpRequest() { | ||
@@ -17,5 +30,10 @@ try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } | ||
}; | ||
}; | ||
function get(url, callback){ | ||
function isArray(el){ | ||
return Object.prototype.toString.call(el) == '[object Array]'; | ||
} | ||
function browserGet(url, callback){ | ||
var req = new XHR; | ||
@@ -38,10 +56,31 @@ | ||
function isArray(el){ | ||
return Object.prototype.toString.call(el) == '[object Array]'; | ||
function nodeGet(url, callback){ | ||
var urlopts = liburl.parse(url); | ||
var host = urlopts.host.replace(/\:\d+/, ''), | ||
options = { | ||
host: host, | ||
port: urlopts.port, | ||
path: urlopts.pathname, | ||
headers: { | ||
Host: host | ||
} | ||
}; | ||
( urlopts.protocol == 'https' && libhttps || libhttp ).get(options, function(res) { | ||
var buffer = ''; | ||
res.setEncoding('utf8'); | ||
res.on('data', function (chunk) { | ||
buffer += chunk; | ||
}); | ||
res.on('end', function(){ | ||
callback(undefined, buffer); | ||
}); | ||
}); | ||
} | ||
var slice = Array.prototype.slice; | ||
return function(undefined){ | ||
return function boxcarsColl(){ | ||
var parentPaths = slice.call(arguments); | ||
var cache = (function(newValue){ | ||
@@ -53,3 +92,3 @@ | ||
if( cache.arguments.length > 0 ){ | ||
if( arguments.length > 0 ){ | ||
elements = newValue; | ||
@@ -83,6 +122,6 @@ } | ||
function main(){ | ||
var paths = slice.call(main.arguments, 0, main.arguments.length - 1), | ||
callback = slice.call(main.arguments, main.arguments.length - 1)[0], | ||
var paths = parentPaths.concat( slice.call(arguments, 0, arguments.length - 1) ), | ||
callback = arguments[arguments.length - 1], | ||
isSingleContent = main.arguments.length == 2 && typeof paths[0] == 'string', | ||
isSingleContent = paths.length == 1 && typeof paths[0] == 'string', | ||
@@ -92,3 +131,3 @@ isListContent, isObjectContent, | ||
main.arguments.length == 2 && !isSingleContent && ( paths = paths[0] ); | ||
paths.length == 1 && !isSingleContent && ( paths = paths[0] ); | ||
@@ -95,0 +134,0 @@ isListContent = isArray(paths); |
{ | ||
"name": "boxcars", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "boxcars is a tiny collection library that comes with built-in caching for preloading contents.", | ||
@@ -8,3 +8,10 @@ "author": "Azer Koculu <azer@kodfabrik.com>", | ||
"main": "boxcars.js", | ||
"devDependencies": { | ||
"highkick": "1.x", | ||
"lowkick": "0.x" | ||
}, | ||
"scripts": { | ||
"test": "node test/run_node.js" | ||
}, | ||
"repository": { "type": "git", "url" : "http://github.com/azer/boxcars.git" } | ||
} |
@@ -1,3 +0,5 @@ | ||
boxcars is a tiny collection library that comes with built-in caching for preloading contents. | ||
boxcars is a zero-dependency tiny library for fetching remote resources. It supports both NodeJS and web browsers. | ||
Tested Platforms: Node, IE6, Chrome, Firefox | ||
 | ||
@@ -13,20 +15,23 @@ | ||
===== | ||
one-liner: | ||
```javascript | ||
var boxcars = require('boxcars'), | ||
mustache = require('mustache'); | ||
var boxcars = require('boxcars'); | ||
var templates = boxcars(); | ||
boxcars('http://google.com', 'http://amazon.com', 'http://github.com')(function(error, homepages){ | ||
console.log( homepages[0] ); // puts the homepage of google.com | ||
}); | ||
``` | ||
templates('templates/foo.html', 'templates/bar.html', 'templates/quux.html', function(error, templates){ | ||
if(error) throw error; | ||
Defining collections: | ||
console.log( templates[0] ); // will put the content of templates/foo.html | ||
```javascript | ||
// objects or arrays can be passed, as well | ||
templates({ 'corge': 'templates/corge.html', 'eggs': 'templates/eggs.html' }, function(error, moreTemplates){ | ||
if(error) throw error; | ||
var templates = boxcars('templates/foo.html', 'templates/bar.html', 'templates/qux.html'); | ||
console.log( moreTemplates.corge ); | ||
templates('templates/corge.html', function(error, templates){ | ||
}); | ||
console.log( templates.length ); // 4 | ||
console.log( templates[0] ); // will put the content of templates/foo.html | ||
@@ -37,2 +42,12 @@ }); | ||
Passing Objects: | ||
```javascript | ||
templates({ 'corge': 'templates/corge.html', 'eggs': 'templates/eggs.html' }, function(error, moreTemplates){ | ||
if(error) throw error; | ||
console.log( moreTemplates.corge ); | ||
}); | ||
``` | ||
Preloading: | ||
@@ -58,8 +73,13 @@ | ||
``` | ||
$ make publish-tests | ||
$ make test | ||
``` | ||
To run the tests on NodeJS: | ||
``` | ||
$ make test do=command cmd=node | ||
``` | ||
To see the summary of the results; | ||
```bash | ||
$ make verify-tests | ||
$ make test do=verify | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
68000
716.91%16
166.67%309
43.06%83
31.75%2
Infinity%3
Infinity%