GithubHelp home page GithubHelp logo

cup.js's Introduction

cup.js

属于自己用的一个辅助性类库,类似underscore。

API 说明

验证相关

cup.is.objcup.isObject

检查一个变量是否是对象(该检测只针对{}的情况,array非对象)

cup.is.obj({}) //true
cup.is.obj([]) //false

cup.is.arrcup.isArray

检查一个变量是否是数组

cup.is.obj([]) //true
cup.is.obj({}) //false

cup.is.funccup.isFunction

检查一个变量是否是函数

cup.is.obj(function () {}) //true

cup.is.strcup.isString

检查一个变量是否是字符串

cup.is.str('') //true

cup.is.numcup.isNumber

检查一个变量是否是数字

cup.is.num(1) //true

cup.is.ipcup.isIP

检查一个变量是否是合法IP地址

cup.is.ip('192.168.1.1') //true

数据存储

默认使用localStorage实现,如果浏览器不支持localStorage则用cookie

cup.db.set

设置存储区内容,如果内容是对象或者数组会自动序列化成字符串

cup.set('a', {x: 1, y: 2});
cup.set('b', [1, 2, 3]);
cup.set('c', 1);
cup.set('d', 'hello')
cup.db.get

获取存储区里面的内容,如果内容是对象或者数组会自动序列化成对象或者数组

cup.db.get('a') // {x: 1, y: 2}
cup.db.get('b') // [1, 2, 3]
cup.db.get('c') // 1
cup.db.get('d') // 'hello'
cup.db.del

删除存储区内容

cup.db.del('a')
cup.db.get('a') //undefined

模板引擎

采用正则替换生成函数,函数再生成HTML字符串方式,并未加上安全性的措施,请用于可信任的内容生成。

cup.template.parse
cup.template.parse('<h1><% title %></h1>', {title: 'hxy'})
//<h1>hxy</h1>

语法为在<% %>里面放置对象变量名则为直接输出变量内容, 可以在<% %>里面使用var|if|for|else|switch|case|break等js语句会转换成相应地逻辑。

var tmpl = '<h1><% title %></h1>'
          +'<ul>'
          +'<% for(vari=0;i<list.length;i++) {%>'
          +'<li><% list[i].url %></li>'
          +'<% } %>'
          +'</ul>';
var data = {
            title: 'hxy',
            list: [
              {url: 'www.google.com'},
              {url: 'www.facebook.com'},
              {url: 'www.twitter.com'}
            ]}
var html = cup.template.parse(tmpl, data);
/*
<h1>hxy</h1>
<ul>
<li>www.google.com</li>
<li>www.facebook.com</li>
<li>www.twitter.com</li>
</ul>
*/
使用缓存

如果模板比较大,里面的逻辑结构比较多和复杂的话,推荐使用缓存机制,这样效率会大大提升, 如果只是很简单的模板,则不推荐用缓存。

首次模板渲染的时候会将编译好的模板函数放入cup.db中,第二次渲染时则直接在cup.db中获取模板函数执行

cup.template.parse('<h1><% title %></h1>', {title: 'hxy'}, 'pagetitle')

cup.js's People

Contributors

coffeedeveloper avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.