add excludeByKey
fix bug for Array
This commit is contained in:
parent
6c1c1c56a4
commit
e06153b6f4
38
util.go
38
util.go
@ -1060,10 +1060,10 @@ $MOD$.countBy = function (obj, ...fieldAndValues) {
|
||||
}
|
||||
|
||||
$MOD$.listBy = function (obj, ...fieldAndValues) {
|
||||
let list = obj instanceof Array || obj instanceof NodeList ? [] : {}
|
||||
let list = obj.length !== undefined ? [] : {}
|
||||
let keys = $MOD$.keysBy(obj, ...fieldAndValues)
|
||||
for (let k of keys) {
|
||||
if (obj instanceof Array || obj instanceof NodeList) {
|
||||
if (obj.length !== undefined) {
|
||||
list.push(obj[k])
|
||||
} else {
|
||||
list[k] = obj[k]
|
||||
@ -1091,7 +1091,7 @@ $MOD$.setBy = function (obj, value, ...fieldAndValues) {
|
||||
$MOD$.indexBy = function (obj, ...fieldAndValues) {
|
||||
let keys = $MOD$.keysBy(obj, ...fieldAndValues)
|
||||
if (keys.length > 0) {
|
||||
return obj instanceof Array || obj instanceof NodeList ? $MOD$.int(keys[0]) : keys[0]
|
||||
return obj.length !== undefined ? $MOD$.int(keys[0]) : keys[0]
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@ -1101,7 +1101,7 @@ $MOD$.removeBy = function (obj, ...fieldAndValues) {
|
||||
let n = 0
|
||||
for (let i = keys.length - 1; i >= 0; i--) {
|
||||
let k = keys[i]
|
||||
if (obj instanceof Array || obj instanceof NodeList) {
|
||||
if (obj.length !== undefined) {
|
||||
obj.splice(k, 1)
|
||||
} else {
|
||||
delete obj[k]
|
||||
@ -1124,7 +1124,7 @@ $MOD$.last = function (arr) {
|
||||
}
|
||||
|
||||
$MOD$.len = function (obj) {
|
||||
if (obj instanceof Array || obj instanceof NodeList) {
|
||||
if (obj.length !== undefined) {
|
||||
return obj.length
|
||||
} else {
|
||||
let n = 0
|
||||
@ -1151,12 +1151,12 @@ $MOD$.mergeBy = function (olds, news, ...fields) {
|
||||
}
|
||||
|
||||
$MOD$.sortBy = function (obj, field, isReverse = false, sortType) {
|
||||
let list = obj instanceof Array || obj instanceof NodeList ? [] : {}
|
||||
let list = obj.length !== undefined ? [] : {}
|
||||
let sortedKeys = {}
|
||||
let sortArr = []
|
||||
for (let k in obj) {
|
||||
let v = ''
|
||||
if (field instanceof Array) {
|
||||
if (field.length !== undefined) {
|
||||
for (let f of field) v += obj[k][f]
|
||||
} else {
|
||||
v = obj[k][field]
|
||||
@ -1184,13 +1184,13 @@ $MOD$.sortBy = function (obj, field, isReverse = false, sortType) {
|
||||
for (let sortKey of sortArr) {
|
||||
for (let k in obj) {
|
||||
let v = ''
|
||||
if (field instanceof Array) {
|
||||
if (field.length !== undefined) {
|
||||
for (let f of field) v += obj[k][f]
|
||||
} else {
|
||||
v = obj[k][field]
|
||||
}
|
||||
|
||||
if (obj instanceof Array || obj instanceof NodeList) {
|
||||
if (obj.length !== undefined) {
|
||||
if (v == sortKey) list.push(obj[k])
|
||||
} else {
|
||||
if (v == sortKey) list[k] = obj[k]
|
||||
@ -1201,7 +1201,7 @@ $MOD$.sortBy = function (obj, field, isReverse = false, sortType) {
|
||||
}
|
||||
|
||||
$MOD$.in = function (v1, v2) {
|
||||
if (!(v1 instanceof Array)) v1 = $MOD$.split(v1, /,\s*/)
|
||||
if (!(v1.length !== undefined)) v1 = $MOD$.split(v1, /,\s*/)
|
||||
return v1.indexOf(String(v2)) !== -1
|
||||
}
|
||||
|
||||
@ -1218,7 +1218,7 @@ $MOD$.clearEmpty = function (arr) {
|
||||
}
|
||||
|
||||
$MOD$.split = function (v1, v2, n) {
|
||||
let a = $MOD$.str(v1).split(v2)
|
||||
let a = $MOD$.string(v1).split(v2)
|
||||
if (!n || n < a.length - 1) return a
|
||||
let a2 = a.slice(0, n)
|
||||
a2[n - 1] = a.slice(n - 1).join(v2)
|
||||
@ -1226,7 +1226,7 @@ $MOD$.split = function (v1, v2, n) {
|
||||
}
|
||||
|
||||
$MOD$.splitWithoutEmpty = function (v1, v2, n) {
|
||||
let a = $MOD$.clearEmpty($MOD$.str(v1).split(v2))
|
||||
let a = $MOD$.clearEmpty($MOD$.string(v1).split(v2))
|
||||
if (!n || n < a.length - 1) return a
|
||||
let a2 = a.slice(0, n)
|
||||
a2[n - 1] = a.slice(n - 1).join(v2)
|
||||
@ -1245,7 +1245,7 @@ $MOD$.makeDefault = function (item, defaults) {
|
||||
|
||||
$MOD$._copy = function (obj, isDeepCopy) {
|
||||
let newObj
|
||||
if (obj instanceof Array || obj instanceof NodeList) {
|
||||
if (obj.length !== undefined) {
|
||||
newObj = []
|
||||
for (let o of obj) {
|
||||
if (isDeepCopy && typeof o === 'object' && o) o = $MOD$.copy(o)
|
||||
@ -1296,6 +1296,7 @@ $MOD$.isFloat = function (v) {
|
||||
}
|
||||
|
||||
$MOD$.getByKey = function (obj, ...keys) {
|
||||
if( keys.length === 1 && keys[0].length ) keys = keys[0]
|
||||
let o = {}
|
||||
for (let k in obj) {
|
||||
if (keys.indexOf(k) >= 0) {
|
||||
@ -1304,6 +1305,17 @@ $MOD$.getByKey = function (obj, ...keys) {
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
$MOD$.excludeByKey = function (obj, ...keys) {
|
||||
if( keys.length === 1 && keys[0].length ) keys = keys[0]
|
||||
let o = {}
|
||||
for (let k in obj) {
|
||||
if (keys.indexOf(k) < 0) {
|
||||
o[k] = obj[k]
|
||||
}
|
||||
}
|
||||
return o
|
||||
}
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
4
util.ts
4
util.ts
@ -112,6 +112,7 @@ export default {
|
||||
isInt,
|
||||
isFloat,
|
||||
getByKey,
|
||||
excludeByKey,
|
||||
}
|
||||
|
||||
function json(data: any): string { return '' }
|
||||
@ -277,3 +278,6 @@ function isFloat(v: any): boolean { return false }
|
||||
|
||||
// 从对象中获取指定的key生成新的对象返回
|
||||
function getByKey(obj: Object, ...keys: string[]): Object { return {} }
|
||||
|
||||
// 从对象中排除指定的key生成新的对象返回
|
||||
function excludeByKey(obj: Object, ...keys: string[]): Object { return {} }
|
||||
|
Loading…
Reference in New Issue
Block a user