Skip to content

Commit

Permalink
1.5.19
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Apr 27, 2018
1 parent f652508 commit f48709e
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 35 deletions.
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,24 @@ import XEUtils from 'xe-utils'
XEUtils.includes([11], 22) // false
XEUtils.includes([11, 22], 22) // true
```
### assign/objectAssign/extend (target, ...) 浅拷贝一个或者多个对象到目标对象中
### assign/objectAssign/extend ([deep], target, ...) 浅拷贝一个或者多个对象到目标对象中,如果第一值是true,则使用深拷贝
```shell
import XEUtils, { objectAssign } from 'xe-utils'

const obj1 = {a: null}
XEUtils.assign(obj1, {a: 11}) // {a: 11}
const obj2 = {c: null}
objectAssign(obj2, {a: 11}, {b: 22}) // {a: 11, b: 22, c: null}

// 浅拷贝
const obj2 = {a: null}
const obj3 = {bb: {b: 11}}
const obj4 = objectAssign(obj2, {a: 11}) // {a: 11, c: null, bb: {b: 11}}
obj3.bb = 22 // obj4 = {a: 11, c: null, bb: {b: 22}}

// 深拷贝
const obj2 = {a: null}
const obj3 = {bb: {b: 11}}
const obj4 = XEUtils.extend(true, obj3, {a: 11}) // {a: 11, c: null, bb: {b: 11}}
obj3.bb = 22 // obj4 = {a: 11, c: null, bb: {b: 11}}
```
### stringToJson (str) 字符串转JSON
```shell
Expand Down Expand Up @@ -362,14 +372,17 @@ arrayLast([11, 22]) // 22
```
### each/objectEach/arrayEach ( obj, iteratee, context ) 迭代器
```shell
import XEUtils from 'xe-utils'
import XEUtils, { objectEach, arrayEach } from 'xe-utils'

const result = []
XEUtils.each({a: 11, b: 22}, (item, key) => {
if (key === 'b') {
result.push(item)
}
}) // [22]
// 通用迭代器
})
objectEach({a: 11, b: 22}, (item, key) => {
// 对象迭代器
})
arrayEach([11, 22, 33], (item, key) => {
// 数组迭代器
})
```
### groupBy ( obj, iteratee, context ) 集合分组,默认使用键值分组,如果有iteratee则使用结果进行分组
```shell
Expand Down Expand Up @@ -512,7 +525,7 @@ import XEUtils from 'xe-utils'
XEUtils.stringToDate('2017-12-20') // Wed Dec 20 2017 00:00:00 GMT+0800 (中国标准时间)
XEUtils.stringToDate('2017-12-20 10:10:30') // Wed Dec 20 2017 10:10:30 GMT+0800 (中国标准时间)
XEUtils.stringToDate('12/20/2017', 'MM/dd/yyyy') // Wed Dec 20 2017 00:00:00 GMT+0800 (中国标准时间)
XEUtils.stringToDate('12/20/2017 10:10:30.100', 'MM/dd/yyyy HH:mm') // Wed Dec 20 2017 10:10:00 GMT+0800 (中国标准时间)
XEUtils.stringToDate('2017/12/20 10:10:30', 'yyyy/MM/dd HH:mm') // Wed Dec 20 2017 10:10:00 GMT+0800 (中国标准时间)
XEUtils.stringToDate('12/20/2017 10:10:30.100', 'MM/dd/yyyy HH:mm:ss.SSS') // Wed Dec 20 2017 10:10:30 GMT+0800 (中国标准时间)
```
### dateToString ( date, format ) 日期化为任意格式字符串(yyyy年份、MM月份、dd天、HH小时、mm分钟、ss秒、S毫秒、E星期几、q季度)
Expand Down Expand Up @@ -607,6 +620,15 @@ arrayMax([{a: 11}, {a: 44}], (item) => {
}) // {a: 44}
```
### commafy ( num, options ) 数值千分位分隔符、小数点
```shell
import XEUtils from 'xe-utils'

XEUtils.commafy(1000000) // 千分位格式化 1,000,000
XEUtils.commafy(1000000, {fixed: 2}) // 格式化金额 1,000,000.00
XEUtils.commafy(1234123412341234, {spaceNumber: 4, separator: ' ', fixed: 0}) // 格式化银行卡 1234 1234 1234 1234
```
### escape ( str ) 转义HTML字符串,替换&, <, >, ", ', `字符
```shell
import XEUtils from 'xe-utils'
Expand Down
48 changes: 37 additions & 11 deletions dist/xe-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,26 +595,37 @@
}
var contains = includes

function extend (target, args, isClone) {
for (var source, index = 1, len = args.length; index < len; index++) {
source = args[index]
arrayEach(objectKeys(args[index]), function (key) {
target[key] = isClone ? clone(source[key], isClone) : source[key]
})
}
return target
}

/**
* 浅拷贝一个或者多个对象到目标对象中
*
* @param {Object} obj 目标对象
* @param {...Object}
* @return {Boolean}
*/
var objectAssign = Object.assign || function (target) {
var objectAssign = function (target) {
if (target) {
for (var source, index = 1, len = arguments.length; index < len; index++) {
source = arguments[index]
arrayEach(objectKeys(arguments[index]), function (key) {
target[key] = source[key]
})
var args = arguments
if (target === true) {
if (args.length > 1) {
target = isArray(target[1]) ? [] : {}
return extend(target, args, true)
}
} else {
return Object.assign ? Object.assign.apply(Object, args) : extend(target, args)
}
}
return target
}
var assign = objectAssign
var extend = objectAssign

/**
* 字符串转JSON
Expand Down Expand Up @@ -853,6 +864,7 @@
isFunction: isFunction,
isBoolean: isBoolean,
isString: isString,
isNumber: isNumber,
isRegExp: isRegExp,
isObject: isObject,
isPlainObject: isPlainObject,
Expand All @@ -875,8 +887,8 @@
includes: includes,
contains: contains,
objectAssign: objectAssign,
assign: assign,
extend: extend,
assign: objectAssign,
extend: objectAssign,
stringToJson: stringToJson,
jsonToString: jsonToString,
objectKeys: objectKeys,
Expand Down Expand Up @@ -1301,12 +1313,26 @@
}
var max = arrayMax

/**
* 千分位分隔符、小数点
*
* @param {String/Number} num 数值
* @param {Object} 参数 {separator: 分隔符, fixed: 小数位数}
* @return {String}
*/
function commafy (num, options) {
var opts = baseExports.objectAssign({ spaceNumber: 3, separator: ',', fixed: 0 }, options)
var result = parseFloat(('' + num).replace(/,/g, '') || 0).toFixed(opts.fixed).split('.')
return result[0].replace(new RegExp('(?=(?!(\\b))(\\d{' + opts.spaceNumber + '})+$)', 'g'), opts.separator) + (result[1] ? '.' + result[1] : '')
}

var numberExports = {
getRandom: getRandom,
arrayMin: arrayMin,
min: min,
arrayMax: arrayMax,
max: max
max: max,
commafy: commafy
}

var escapeMap = {
Expand Down
2 changes: 1 addition & 1 deletion dist/xe-utils.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xe-utils",
"version": "1.5.18",
"version": "1.5.19",
"description": "XEUtils 函数库",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

function XEUtils () {}

XEUtils.version = '1.5.18'
XEUtils.version = '1.5.19'

module.exports = XEUtils
32 changes: 22 additions & 10 deletions src/method/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,26 +344,37 @@ function includes (obj, val) {
}
var contains = includes

function extend (target, args, isClone) {
for (var source, index = 1, len = args.length; index < len; index++) {
source = args[index]
arrayEach(objectKeys(args[index]), function (key) {
target[key] = isClone ? clone(source[key], isClone) : source[key]
})
}
return target
}

/**
* 浅拷贝一个或者多个对象到目标对象中
*
* @param {Object} obj 目标对象
* @param {...Object}
* @return {Boolean}
*/
var objectAssign = Object.assign || function (target) {
var objectAssign = function (target) {
if (target) {
for (var source, index = 1, len = arguments.length; index < len; index++) {
source = arguments[index]
arrayEach(objectKeys(arguments[index]), function (key) {
target[key] = source[key]
})
var args = arguments
if (target === true) {
if (args.length > 1) {
target = isArray(target[1]) ? [] : {}
return extend(target, args, true)
}
} else {
return Object.assign ? Object.assign.apply(Object, args) : extend(target, args)
}
}
return target
}
var assign = objectAssign
var extend = objectAssign

/**
* 字符串转JSON
Expand Down Expand Up @@ -602,6 +613,7 @@ var baseExports = {
isFunction: isFunction,
isBoolean: isBoolean,
isString: isString,
isNumber: isNumber,
isRegExp: isRegExp,
isObject: isObject,
isPlainObject: isPlainObject,
Expand All @@ -624,8 +636,8 @@ var baseExports = {
includes: includes,
contains: contains,
objectAssign: objectAssign,
assign: assign,
extend: extend,
assign: objectAssign,
extend: objectAssign,
stringToJson: stringToJson,
jsonToString: jsonToString,
objectKeys: objectKeys,
Expand Down
16 changes: 15 additions & 1 deletion src/method/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,26 @@ function arrayMax () {
}
var max = arrayMax

/**
* 千分位分隔符、小数点
*
* @param {String/Number} num 数值
* @param {Object} 参数 {separator: 分隔符, fixed: 小数位数}
* @return {String}
*/
function commafy (num, options) {
var opts = baseExports.objectAssign({spaceNumber: 3, separator: ',', fixed: 0}, options)
var result = parseFloat(('' + num).replace(/,/g, '') || 0).toFixed(opts.fixed).split('.')
return result[0].replace(new RegExp('(?=(?!(\\b))(\\d{' + opts.spaceNumber + '})+$)', 'g'), opts.separator) + (result[1] ? '.' + result[1] : '')
}

var numberExports = {
getRandom: getRandom,
arrayMin: arrayMin,
min: min,
arrayMax: arrayMax,
max: max
max: max,
commafy: commafy
}

module.exports = numberExports

0 comments on commit f48709e

Please sign in to comment.