From f9cf686e63c8fdf5bdb91b04d1a960d3e7d172a6 Mon Sep 17 00:00:00 2001 From: Tommy Chen Date: Mon, 3 Sep 2012 22:15:00 +0800 Subject: [PATCH] rebuild --- lib/qfs.js | 412 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/utils.js | 97 ++++++++++++ 2 files changed, 509 insertions(+) create mode 100644 lib/qfs.js create mode 100644 lib/utils.js diff --git a/lib/qfs.js b/lib/qfs.js new file mode 100644 index 0000000..d016074 --- /dev/null +++ b/lib/qfs.js @@ -0,0 +1,412 @@ +var fs = require('fs'), + pathFn = require('path'), + async = require('async'), + utils = require('./utils'), + sep = pathFn.sep; + +// If Node version under v0.8, port path.exists to fs.exists +if (fs.exists === undefined){ + fs.exists = pathFn.exists; + fs.existsSync = pathFn.existsSync; +} + +module.exports = qfs; + +function qfs(){ + return new qfs.fn.init(arguments); +}; + +function Item(path){ + path = pathFn.normalize(path); + this.path = path; + this.name = pathFn.basename(path); + this.dir = pathFn.dirname(path); + this.ext = pathFn.extname(path); +}; + +qfs.fn = qfs.prototype = { + length: 0, + init: function(args){ + var arr = [], + _this = this; + + args = utils.toArray(args); + + // No argument + if (!args.length){ + return this; + // Multiple arguments + } else if (args.length > 1){ + var path = pathFn.join.apply(null, args); + arr.push(path); + // One argument + } else { + var obj = args.shift(); + + // Invalid arguments + if (!obj){ + return this; + } else { + // object is array + if (utils.isArray(obj)){ + arr = obj; + // qfs object + } else if (obj instanceof Item){ + this.length = 1; + this[0] = obj; + return this; + // path string + } else { + arr.push(obj); + } + } + } + + this.length = arr.length; + utils.each(arr, function(item, i){ + _this[i] = new Item(item); + }); + + return this; + } +}; + +qfs.fn.init.prototype = qfs.fn; + +qfs.fn.eq = function(i){ + i = +i; + return i < 0 ? qfs(this[this.length + i]) : qfs(this[i]); +}; + +qfs.fn.first = function(){ + return this.eq(0); +}; + +qfs.fn.last = function(){ + return this.eq(-1); +}; + +qfs.fn.slice = function(start, end){ + return qfs([].slice.apply(this.toArray(), arguments)); +}; + +qfs.fn.toArray = function(){ + var arr = []; + + this.each(function(){ + arr.push(this.path()); + }); + + return arr; +}; + +utils.each(['path', 'name', 'dir', 'ext'], function(item){ + qfs.fn[item] = function(){ + return this[0][item]; + }; +}); + +qfs.fn.each = function(callback){ + for (var i=0; i