Comparing version 0.0.1 to 0.0.2
156
lib/index.js
@@ -1,50 +0,142 @@ | ||
var map = require('functools').map, | ||
get = require('./get'); | ||
var boxcars = (function(undefined){ | ||
var slice = Array.prototype.slice; | ||
var XHR; | ||
var boxcars = function(undefined){ | ||
var cache = (function(newValue){ | ||
if (typeof XMLHttpRequest != "undefined"){ | ||
XHR = XMLHttpRequest; | ||
} else { | ||
XHR = function XMLHttpRequest() { | ||
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } | ||
catch (e) {} | ||
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } | ||
catch (e) {} | ||
try { return new ActiveXObject("Microsoft.XMLHTTP"); } | ||
catch (e) {} | ||
throw new Error("This browser does not support XMLHttpRequest."); | ||
}; | ||
}; | ||
var templates = undefined; | ||
function get(url, callback){ | ||
var req = new XHR; | ||
return function cache(newValue){ | ||
req.open('GET', url, true); | ||
if( cache.arguments.length > 0 ){ | ||
templates = newValue; | ||
req.onreadystatechange = function (aEvt) { | ||
if ( req.readyState == 4 ) { | ||
if ( req.status == 200 ){ | ||
callback(undefined, req.responseText); | ||
} else { | ||
callback(new Error('GET ' + url + ' has been failed (' + req.status + ')')); | ||
} | ||
} | ||
}; | ||
if( templates == undefined){ | ||
templates = {}; | ||
} | ||
req.send(); | ||
}; | ||
return templates; | ||
}; | ||
function isArray(el){ | ||
return Object.prototype.toString.call(el) == '[object Array]'; | ||
} | ||
})(); | ||
var slice = Array.prototype.slice; | ||
function template(path, callback){ | ||
if( cache().hasOwnProperty(path) ){ | ||
callback(undefined, cache()[path]); | ||
return; | ||
return function(undefined){ | ||
var cache = (function(newValue){ | ||
var elements = undefined; | ||
return function cache(newValue){ | ||
if( cache.arguments.length > 0 ){ | ||
elements = newValue; | ||
} | ||
if( elements == undefined){ | ||
elements = {}; | ||
} | ||
return elements; | ||
}; | ||
})(); | ||
function element(path, callback){ | ||
if( cache().hasOwnProperty(path) ){ | ||
callback(undefined, cache()[path]); | ||
return; | ||
} | ||
get(path, callback); | ||
} | ||
get(path, callback); | ||
} | ||
function main(){ | ||
var paths = slice.call(main.arguments, 0, main.arguments.length - 1), | ||
callback = slice.call(main.arguments, main.arguments.length - 1)[0], | ||
function main(){ | ||
var paths = slice.call(main.arguments, 0, main.length - 1), | ||
callback = slice.call(main.arguments, main.length - 1)[0]; | ||
isSingleContent = main.arguments.length == 2 && typeof paths[0] == 'string', | ||
isListContent, isObjectContent, | ||
filenames, contents, map, key; | ||
main.arguments.length == 2 && ( paths = paths[0] ); | ||
main.arguments.length == 2 && !isSingleContent && ( paths = paths[0] ); | ||
map.async(template, paths, callback); | ||
} | ||
isListContent = isArray(paths); | ||
isObjectContent = !isListContent && typeof paths == 'object' && paths.hasOwnProperty != undefined; | ||
main.cache = cache; | ||
if( isListContent ){ | ||
filenames = paths; | ||
contents = []; | ||
} else { | ||
filenames = []; | ||
contents = {}; | ||
map = {}; | ||
return main; | ||
}; | ||
for(key in paths){ | ||
filenames.push( paths[key] ); | ||
map[ paths[key] ] = key; | ||
contents[ key ] = undefined; | ||
} | ||
module.exports = boxcars; | ||
} | ||
(function iter(i){ | ||
if( i >= filenames.length ){ | ||
callback( undefined, contents ); | ||
return; | ||
} | ||
element(filenames[i], function(error, content){ | ||
if(error){ | ||
callback(error); | ||
return; | ||
} else if( isSingleContent ){ | ||
callback(undefined, content); | ||
return; | ||
} else if( isListContent ){ | ||
contents.push( content ); | ||
} else { | ||
contents[ map[filenames[i]] ] = content; | ||
} | ||
iter(i+1); | ||
}); | ||
}(0)); | ||
} | ||
main.cache = cache; | ||
return main; | ||
}; | ||
})(); | ||
if(typeof module != 'undefined' && module.exports){ | ||
module.exports = functools; | ||
} |
{ | ||
"name": "boxcars", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "boxcars is a tiny collection library that comes with built-in caching for preloading contents.", | ||
@@ -9,11 +9,3 @@ "author": "Azer Koculu <azer@kodfabrik.com>", | ||
"main": "lib/index.js", | ||
"dependencies":{ | ||
"functools": "1.x" | ||
}, | ||
"devDependencies": { | ||
"one": "1.x", | ||
"highkick": "1.x", | ||
"lowkick": "0.x" | ||
}, | ||
"repository": { "type": "git", "url" : "http://github.com/azer/boxcars.git" } | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4878
0
0
103
5
- Removedfunctools@1.x
- Removedfunctools@1.4.0(transitive)