js迭代器

var utils = {
    getType: function(content) {
        return Object.prototype.toString.call(content).match(/\[object (.*)\]/)[1];
    },
    isType: function(content, type) {
        return this.getType(content).toLowerCase() === type.toLowerCase();
    }

};

function Iterator(content, cfg) {
    this.content = content;
    this.length = 0;
    this.count = 0;
    this.position = 0;
    this.direction = (cfg && cfg.direction) || 0;
    this.init(cfg);
}
Iterator.prototype = {
    next: function() {
        if (!this.hasNext()) {
            return null;
        }
        this.count++;
        if (this.direction === 0) {
            return this.content[this.position++];
        } else {
            return this.content[this.position--];
        }

    },  
    rewind: function(index) {
        if (index  < this.length && index >= 0) {
            this.position = index;
            this.count = this.direction === 0 ? index : this.length - index - 1;
        }
    },
    hasNext: function() {
        return this.count < this.length;
    },
    init: function(cfg) {
        if (cfg && cfg.contentHandler && utils.isType(cfg.contentHandler, 'function')) {
            var result = cfg.contentHandler.call(this);
            if (utils.isType(result, 'Array')) {
                this.content = result;
            } else {
                throw new Error('arguments contentHandler of Iterator Error');
                return;
            }
        } else {
            var type = utils.getType(this.content);
            switch(type) {
                case 'String': 
                                this.content = this.content.split('');
                                break;
                case 'Array': 
                                this.content = this.content.concat();
                                break;
                case 'Object': 
                            var arr = [];
                            var keys = Object.keys(this.content);
                            for (var i = 0; i< keys.length; i++) {
                                var obj = {};
                                obj[keys[i]] = this.content[keys[i]];
                                arr.push(obj);
                            }
                            this.content = arr;
                            break;
            }
        }

        this.length = this.content.length;
        this.position = this.direction === 0 ? 0 : this.length - 1;
    }
}

1.支持正向反向迭代。
2.支持重定位。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值