Giter Club home page Giter Club logo

ddv-util's Introduction

ddv-util

这是一个工具类库,可以作为 vue 过滤器使用


安装


npm

$ npm install ddv-util - S

使用


import util from 'ddv-util';

你也可以把 ddv-util 映射到全局变量当中

//把util映射到全局变量d中
util.globalInit('d', null, true);

API

ddv-util 默认拥有的api:

  • isFunction
  • 用途:判断是否是 function 类型
var p = function(){};

util.isFunction(p);
==>true
  • isArray
  • 用途:判断是否是一个数组
var p = [];

util.isArray(p);
==>true
  • isGlobal
  • 用途:判断是否是全局变量
window.p = {};
util.isGlobal(p);
==>true
  • isNumber
  • 用途:判断是否是 number 类型
var p = 1;
util.isNumber(p);
==>true
  • type
  • 用途:判断是类型
var p = {};
util.type(p,'object');
==>true
  • argsToArray
  • 用途:把伪数组强转数组
var p = {0:'a',1:'b',length:2};;
util.argsToArray(p);
==>['a','b']

ddv-util 内置 time 时间处理模块,详细使用方式请参PHP对应的方法:time, date, gmdate,strtotime

//引入'ddv-util'
import util from 'ddv-util';
//引入时间处理api模块
import utilTime from 'ddv-util/time';
//安装到'ddv-util'
util.extendInit(utilTime);

//获取当前开始
util.now();

//获取php的时间戳
util.time();

//时间戳格式,格式化后是格林时间
util.gmdate(format,timestamp);
util.date(format,timestamp);
util.strtotime(text, now);

注册过滤器


ddv-util 支持注册过滤器,可使用扩展或安装的方式

扩展

extend

//helper.js
export default{
    add(val){
        return val+1;
    }
}

//use.js
import util from 'ddv-util'
import helper from './helper.js'
//在util下扩展helper
util.extend({
    helper
})

util.helper.add(1);
=>2

安装

extendInit

//helper.js
module.exports = function(util){
    util.extendDeep({
        add(val){
            return val+1;
        }
    })
}

//use.js
import util from 'ddv-util'
import helper from './helper.js'
//在util下安装helper
util.extendInit(helper);

util.add(1);
=>2

关于使用vue在 mustache 使用 ddv-util 过滤器

在mustache下使用过滤器语法基本和 Vue-filter 相同


<template>
    <div>
    <!--1参数代表time以第二个参数传进date里面,'Y-d-m'则为第一个。默认不填是第一个,如此类推-->
    {{time | util('date,1','Y-d-m') }}
    </div>
</template>
import Vue from 'vue';
import util from 'ddv-util';
import utilTime from 'ddv-util/time';
util.extendInit(utilTime);

Vue.filter('util',util);

export default {
    data(){
        return{
            time : '1484379264'
        }
    }
}

ddv-util's People

Contributors

yuchonghua avatar hadichen avatar js-yezi avatar

Stargazers

 avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

ddvjsfork

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.