1.0.18 • Published 6 years ago

sunduan-util v1.0.18

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

sunduan-util

Build Status

codecov

主要为前端提供常用的工具函数

特征

  1. 常用的 array 方法
  2. 对象的复制
  3. 时间格式的转化
  4. 函数节流

npm address: https://www.npmjs.com/package/sunduan-util

Using NPM

install:

npm i sunduan-util -save

use:

import {
  hello,
  throttle, // 函数节流
  deepClone,
  // array function
  pop,
  forEach,
  FormatDate
} from 'sunduan-util';

Using CDN:

<script src="https://unpkg.com/sunduan-util/dist/sunduan-util.js"></script>
<script type="text/javascript">
  console.log(sunduanUtil)
</script>

test

	console.log(hello(), 'sunDuanUtil');

throttle

  • throttle(callback,time)

    paramstypedefaultrequire
    callbackFunction-true
    timeNumber500false
    return---
    var func = function() {
      console.log(window.innerWidth, window.innerHeight);
    }
    
    func = throttle(func, 2000);
    window.addEventListener('resize', func);

pop

  • pop(list)

    typedefaultrequire
    listObject-true
    returnArray--
var list = [1, 2, 3, 4, 5, 6];

var newList = pop(list);

console.log(newList); // [1, 2, 3, 4, 5]
console.log(list); // [1, 2, 3, 4, 5, 6]

forEach

  • forEach(obj, callback)

    typedefaultrequire
    objObject-true
    callbackFunction-true
    return---
    var obj = { a: 10, b: 20, c: 30 }
    forEach(obj, function(value, index, obj) {
      console.log(value, index, obj);
    })
    
    var list = [1, 2, 3, 4]
    forEach(list, function(value, index, obj) {
      console.log(value, index, obj);
    })

buildURL

  • buildURL(url, params)

    typedefaultrequire
    urlString-true
    paramsObjectnullfalse
    returnString-true
    var url = 'https://testxxxx.com/comments';
    
    var params = {
      postId: 1,
      page: 1,
      pageSize: 10
    };
    
    var url = buildURL(url, params);
    
    console.log(url); // https://testxxxx.com/comments?postId=1&page=1&pageSize=10

deepClone

  • deepClone(obj, type)

    type: default | JSON

    typedefaultrequire
    objObject-true
    typeStringdefaultfalse
    returnString-true
    var obj = {
      a: 'a',
      b: 'b',
      c: 'c',
      d: {
        one: 'd1',
        two: 'd2'
      },
      e: ['e1', 'e2', 'e3', 'e4']
    }
    var newObj = deepClone(obj)
    newObj.e.push('e5')
    console.log(newObj.e) // ['e1', 'e2', 'e3', 'e4', 'e5']
    console.log(obj.e) // ['e1', 'e2', 'e3', 'e4']
    
    var obj = {
      a: 'a1',
      b: 'b2',
      c: 'c3',
      d: {
        one: 'd1',
        two: 'd2'
      },
      e: ['e1', 'e2', 'e3', 'e4']
    }
    var newObj = deepClone(obj, 'JSON')
    newObj.d.one = 'd3'
    console.log(newObj.d) // { one: 'd3', two: 'd2' }
    console.log(obj.d) // { one: 'd1', two: 'd2' }

    FormatDate

  • formatDate.getDate(date, format)

    format type: YYYY-MM-DD HH:mm:ss , YYYY/MM/DD HH:mm , YYYY/MM/DD HH , YYYY/MM/DD , YYYY/MM , YYYY ...... format 年月日中间使用非字母的字符代替

    typedefaultrequire
    dateDateNumber-true
    formatStringYYYY/MM/DD HH:mm:ssfalse
var formatDate = new FormatDate()
var time = formatDate.getDate(+new Date())
console.log(time.date) // 2018/05/19 15:35:38

var formatDate = new FormatDate()
var timeTwo = formatDate.getDate(+new Date('2018/05/19 15:27:51'), 'YYYY-MM-DD HH:mm:ss')
console.log(timeTwo.date) // 2018-05-19 15:27:51

setTimeout(function () {
  var timeTwo = formatDate.getDate(+new Date('2018/05/19 15:27:53'))
  console.log(timeTwo.date) // 2018|05|19
}, 2000)

var timeTwo = formatDate.getDate(+new Date('2018/05/19 15:27:51'), 'YYYY-MM-DD HH:mm')
console.log(timeTwo.date) // 2018-05-19 15:27

var timeTwo = formatDate.getDate(+new Date('2018/05/19 15:27:51'), 'YYYY-MM-DD HH')
console.log(timeTwo.date) // 2018-05-19 15

var timeTwo = formatDate.getDate(+new Date('2018/05/19 15:27:51'), 'YYYY-MM-DD')
console.log(timeTwo.date) // 2018-05-19

var timeTwo = formatDate.getDate(+new Date('2018/05/19 15:27:51'), 'YYYY|MM|DD')
console.log(timeTwo.date) // 2018|05|19

var timeTwo = formatDate.getDate(+new Date('2018/05/19 15:27:51'), 'YYYY&MM&DD')
console.log(timeTwo.date) // 2018&05&19
1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago