js版dataquery,实现数据搜索<search.js>

github address

GitHub - 403f/searchjs: So short! So powerful!

searchjs

So short! So powerful! So flexible! You can use the library to filter data easily. And it's very flexible,You can design the chain style which you want.And it's robust,you can change the order of chain-unction as well as no error.

example

<script src="./search.js"></script>
var data = [
{'userId':132,title:'dads'},
{'userId':1312,title:'ddsadads'},
{'userId':12,title:'daddasdass'},
{'userId':1,title:'hghhfg'},
];

var result = findData(data)
      .filter(true,
        [
	  'userId','title',
      ])
      .where({
	'userId':[
	'>=',11
	]
       })
      .orderBy('userId','desc');
			
for(let i = 0; i < result.length; i++) {
    //Log.fire('none','accept',JSON.stringify(result[i]));
    console.log(result[i]);
}

you can change the order,for example:

<script src="./search.js"></script>
var data = [
{'userId':132,title:'dads'},
{'userId':1312,title:'ddsadads'},
{'userId':12,title:'daddasdass'},
{'userId':1,title:'hghhfg'},
];

var result = findData(data)
      .filter(true,[
	'userId','title',
      ])
      .orderBy('userId','desc')
      .where({
	'userId':[
	    '>=',11
	]
       });
			
for(let i = 0; i < result.length; i++) {
    //Log.fire('none','accept',JSON.stringify(result[i]));
    console.log(result[i]);
}

you also can del the filter function in anyway by anymethod,for example:

<script src="./search.js"></script>
var data = [
{'userId':132,title:'dads'},
{'userId':1312,title:'ddsadads'},
{'userId':12,title:'daddasdass'},
{'userId':1,title:'hghhfg'},
];

var result = findData(data)
      .filter(true,[
	'userId','title',
      ])
      .orderBy('userId','desc');
			
for(let i = 0; i < result.length; i++) {
    //Log.fire('none','accept',JSON.stringify(result[i]));
    console.log(result[i]);
}
<script src="./search.js"></script>
var data = [
{'userId':132,title:'dads'},
{'userId':1312,title:'ddsadads'},
{'userId':12,title:'daddasdass'},
{'userId':1,title:'hghhfg'},
];

var result = findData(data)
      .filter(true,[
	'userId','title',
       ])
      .where({
	'userId':[
	    '>=',11
				]
       });
			
for(let i = 0; i < result.length; i++) {
    //Log.fire('none','accept',JSON.stringify(result[i]));
    console.log(result[i]);
}

extend

if you want to extend the function of the library,you must know the principle of the design of this library. Just take the script as a factory,but this factory handle the data which is given by user.Every filter-function like a worker of this factory,they display different roles,but they handle the same data in order position.So the parameter ‘share_data’ is shared by the worker. And the worker use 'this.result.pop()' to get the share_data by pipline.And the worker use 'this.result.push()' to pass the share_data to the next worker.So if you want to extend a function to this library of script is just like employing a mew worker,and you can put it in any position of the pipline.

source code

(function(root) {
    function register(env, name, obj) {
        if (typeof env[name] == 'object') {
            return false;
        }
        env[name] = obj;
        return true;
    }

    let Log = function() {
        let message_type = ['error', 'important', 'warn', 'accept'];

        let emptyMsg = {
            'funcName': 'none',
            'type': 'accept',
            'content': 'the default log info',
            'time': new Date()
        };

        let msgs = [];

        return {
            'put': function(funcName, type, content) {
                if (typeof funcName != 'string' || message_type.indexOf(type) < 0 || typeof content != 'string') {
                    this.fire(funcName, type, content);
                    return;
                }

                let msg = {
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值