/**
 * XadillaX created at 2015-02-12 13:10:03
 *
 * Copyright (c) 2015 Huaban.com, all rights
 * reserved
 */
var xml = require("xml");

var AlfredItem = function() {
    this.items = [];
};

AlfredItem.prototype.addItem = function(uid, title, subtitle, icon, options) {
    var item = {
        item: [
            { _attr: { uid: uid } }
        ]
    };

    var attr = item.item[0]._attr;
    var _item = item.item;
    _item.push({ title: title });
    _item.push({ subtitle: subtitle });
    _item.push({ icon: icon });

    options = options || {};
    for(var key in options) {
        if(!options.hasOwnProperty(key)) continue;
        attr[key] = options[key];
    }

    this.items.push(item);
};

AlfredItem.prototype.delItemViaText = function(key, text) {
    this.items = this.items.reduce(function(res, item) {
        for(var i = 1; i < item.length; i++) {
            var it = item[i];
            if(it[key] === text) {
                return res;
            }
        }

        res.push(item);
        return res;
    }, []);
};

AlfredItem.prototype.delItemViaAttr = function(key, attr) {
    this.items = this.items.reduce(function(res, item) {
        var attrs = item[0]._attr;
        if(!attrs) return res.push(item), res;
        if(attrs[key] === attr) {
            return res;
        }

        res.push(item);
        return res;
    }, []);
};

AlfredItem.prototype.delItemViaTitle = function(title) {
    this.delItemViaText("title", title);
};

AlfredItem.prototype.delItemViaSubtitle = function(subtitle) {
    this.delItemViaSubtitle("subtitle", subtitle);
};

AlfredItem.prototype.delItemViaIcon = function(icon) {
    this.delItemViaIcon("icon", icon);
};

AlfredItem.prototype.delItemViaUid = function(uid) {
    this.delItemViaAttr("uid", uid);
};

AlfredItem.prototype.output = function() {
    return xml({ items: this.items }, { declaration: true });
};

AlfredItem.prototype.inspect = AlfredItem.prototype.output;

module.exports = AlfredItem;


@@ -131,16 +131,54 @@
 xml%22);%0A%0A
+/**%0A * AlfredItem%0A * @constructor%0A */%0A
 var Alfr
@@ -220,24 +220,276 @@
 s = %5B%5D;%0A%7D;%0A%0A
+/**%0A * add an item%0A * @param %7BString%7D uid the UID%0A * @param %7BString%7D title the title of this item%0A * @param %7BString%7D subtitle subtitle of this item%0A * @param %7BString%7D icon icon path of this item%0A * @param %7BObject%7D %5Boptions%5D the optional attributes%0A */%0A
 AlfredItem.p
@@ -989,24 +989,124 @@
 (item);%0A%7D;%0A%0A
+/**%0A * delete an item via text%0A * @param %7BString%7D key the key%0A * @param %7BString%7D text the value%0A */%0A
 AlfredItem.p
@@ -1427,32 +1427,137 @@
     %7D, %5B%5D);%0A%7D;%0A%0A
+/**%0A * delete an item via attribute%0A * @param %7BString%7D key the key%0A * @param %7BString%7D attr the value%0A */%0A
 AlfredItem.proto
@@ -1864,24 +1864,95 @@
 %7D, %5B%5D);%0A%7D;%0A%0A
+/**%0A * delete an item via title%0A * @param %7BString%7D title the title%0A */%0A
 AlfredItem.p
@@ -2033,32 +2033,112 @@
 e%22, title);%0A%7D;%0A%0A
+/**%0A * delete an item via subtitle%0A * @param %7BString%7D subtitle the subtitle%0A */%0A
 AlfredItem.proto
@@ -2235,24 +2235,97 @@
 title);%0A%7D;%0A%0A
+/**%0A * delete an item via icon%0A * @param %7BString%7D icon the icon path%0A */%0A
 AlfredItem.p
@@ -2406,24 +2406,89 @@
  icon);%0A%7D;%0A%0A
+/**%0A * delete an item via uid%0A * @param %7BString%7D uid the uid%0A */%0A
 AlfredItem.p
@@ -2565,24 +2565,53 @@
 , uid);%0A%7D;%0A%0A
+/**%0A * output the string%0A */%0A
 AlfredItem.p

add some comments