add some js function

This commit is contained in:
Star 2024-12-13 19:32:01 +08:00
parent c855c57dba
commit 6c1c1c56a4
3 changed files with 388 additions and 6 deletions

12
go.mod
View File

@ -3,11 +3,11 @@ module apigo.cc/gojs/util
go 1.18 go 1.18
require ( require (
apigo.cc/gojs v0.0.7 apigo.cc/gojs v0.0.8
github.com/ZZMarquis/gm v1.3.2 github.com/ZZMarquis/gm v1.3.2
github.com/emmansun/gmsm v0.29.3 github.com/emmansun/gmsm v0.29.6
github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9 github.com/obscuren/ecies v0.0.0-20150213224233-7c0f4a9b18d9
github.com/ssgo/u v1.7.12 github.com/ssgo/u v1.7.13
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
@ -20,7 +20,7 @@ require (
github.com/ssgo/log v1.7.7 // indirect github.com/ssgo/log v1.7.7 // indirect
github.com/ssgo/standard v1.7.7 // indirect github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/tool v0.4.27 // indirect github.com/ssgo/tool v0.4.27 // indirect
golang.org/x/crypto v0.29.0 // indirect golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.20.0 // indirect golang.org/x/text v0.21.0 // indirect
) )

278
util.go
View File

@ -1027,6 +1027,284 @@ func init() {
TsCode: utilTS, TsCode: utilTS,
Example: utilMD, Example: utilMD,
SetSSKey: SetSSKey, SetSSKey: SetSSKey,
JsCode: `
$MOD$.keysBy = function (obj, ...fieldAndValues) {
let keys = []
for (let k in obj) {
let match = true
if (fieldAndValues.length === 1) {
// 查找一位数组
if (obj[k] != fieldAndValues[0]) {
match = false
}
} else {
// 查找二维数组
for (let i = 0; i < fieldAndValues.length; i += 2) {
if (obj[k][fieldAndValues[i]] != fieldAndValues[i + 1]) {
match = false
break
}
}
}
if (match) {
keys.push(k)
}
}
return keys
}
$MOD$.countBy = function (obj, ...fieldAndValues) {
let keys = $MOD$.keysBy(obj, ...fieldAndValues)
l(obj, keys)
return keys.length
}
$MOD$.listBy = function (obj, ...fieldAndValues) {
let list = obj instanceof Array || obj instanceof NodeList ? [] : {}
let keys = $MOD$.keysBy(obj, ...fieldAndValues)
for (let k of keys) {
if (obj instanceof Array || obj instanceof NodeList) {
list.push(obj[k])
} else {
list[k] = obj[k]
}
}
return list
}
$MOD$.hasBy = function (obj, ...fieldAndValues) {
let keys = $MOD$.keysBy(obj, ...fieldAndValues)
return keys.length > 0
}
$MOD$.getBy = function (obj, ...fieldAndValues) {
let keys = $MOD$.keysBy(obj, ...fieldAndValues)
if (keys.length > 0) return obj[keys[0]]
return null
}
$MOD$.setBy = function (obj, value, ...fieldAndValues) {
let keys = $MOD$.keysBy(obj, ...fieldAndValues)
if (keys.length > 0) obj[keys[0]] = value
}
$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 -1
}
$MOD$.removeBy = function (obj, ...fieldAndValues) {
let keys = $MOD$.keysBy(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) {
obj.splice(k, 1)
} else {
delete obj[k]
}
n++
}
return n
}
$MOD$.removeArrayItem = function (list, item) {
let pos = list.indexOf(item)
if (pos !== -1) list.splice(pos, 1)
}
$MOD$.last = function (arr) {
if (arr && arr.length) {
return arr[arr.length - 1]
}
return null
}
$MOD$.len = function (obj) {
if (obj instanceof Array || obj instanceof NodeList) {
return obj.length
} else {
let n = 0
for (let k in obj) n++
return n
}
}
$MOD$.mergeBy = function (olds, news, ...fields) {
if (!olds) return news
for (let newItem of news) {
let fieldAndValues = []
for (let field of fields) {
fieldAndValues.push(field, newItem[field])
}
let oldIndex = $MOD$.indexBy(olds, ...fieldAndValues)
if (oldIndex === -1) {
olds.push(newItem)
} else {
olds[oldIndex] = newItem
}
}
return olds
}
$MOD$.sortBy = function (obj, field, isReverse = false, sortType) {
let list = obj instanceof Array || obj instanceof NodeList ? [] : {}
let sortedKeys = {}
let sortArr = []
for (let k in obj) {
let v = ''
if (field instanceof Array) {
for (let f of field) v += obj[k][f]
} else {
v = obj[k][field]
}
if (!sortedKeys[v]) {
sortedKeys[v] = true
sortArr.push(v)
}
}
sortArr.sort((a, b) => {
if (sortType === 'int') {
a = $MOD$.int(a)
b = $MOD$.int(b)
} else if (sortType === 'float') {
a = $MOD$.float(a)
b = $MOD$.float(b)
}
if (a == b) return 0
if (typeof a === 'number' && typeof b === 'number') {
return isReverse ? b - a : a - b
} else {
return (isReverse ? a < b : a > b) ? 1 : -1
}
})
for (let sortKey of sortArr) {
for (let k in obj) {
let v = ''
if (field instanceof Array) {
for (let f of field) v += obj[k][f]
} else {
v = obj[k][field]
}
if (obj instanceof Array || obj instanceof NodeList) {
if (v == sortKey) list.push(obj[k])
} else {
if (v == sortKey) list[k] = obj[k]
}
}
}
return list
}
$MOD$.in = function (v1, v2) {
if (!(v1 instanceof Array)) v1 = $MOD$.split(v1, /,\s*/)
return v1.indexOf(String(v2)) !== -1
}
$MOD$.uniquePush = function (arr, ...values) {
for (let v of values) {
if (arr.indexOf(v) === -1) arr.push(v)
}
}
$MOD$.clearEmpty = function (arr) {
let a = []
for (let v of arr) if (v) a.push(v)
return a
}
$MOD$.split = function (v1, v2, n) {
let a = $MOD$.str(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)
return a2
}
$MOD$.splitWithoutEmpty = function (v1, v2, n) {
let a = $MOD$.clearEmpty($MOD$.str(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)
return a2
}
$MOD$.joinWithoutEmpty = function (arr, separator) {
return $MOD$.clearEmpty(arr).join(separator)
}
$MOD$.makeDefault = function (item, defaults) {
for (let k in defaults) {
if (!item[k]) item[k] = defaults[k]
}
}
$MOD$._copy = function (obj, isDeepCopy) {
let newObj
if (obj instanceof Array || obj instanceof NodeList) {
newObj = []
for (let o of obj) {
if (isDeepCopy && typeof o === 'object' && o) o = $MOD$.copy(o)
newObj.push(o)
}
} else {
newObj = {}
for (let k in obj) {
let v = obj[k]
if (isDeepCopy && typeof v === 'object' && v) v = $MOD$.copy(v)
newObj[k] = v
}
}
return newObj
}
$MOD$.copy = function (obj) {
return $MOD$._copy(obj, false)
}
$MOD$.deepCopy = function (obj) {
return $MOD$._copy(obj, true)
}
$MOD$.isNumber = function (v) {
const numberMatcher = /^\d*\.?\d+$/
if (typeof v === 'number' || v instanceof Number) return true
if (typeof v === 'string') return numberMatcher.test(v)
if (typeof v === 'object' && v.toString) numberMatcher.test(v.toString())
return false
}
$MOD$.isInt = function (v) {
const intMatcher = /^\d+$/
if (typeof v === 'number' || v instanceof Number) return $MOD$.int(v) === v
if (typeof v === 'string') return intMatcher.test(v)
if (typeof v === 'object' && v.toString) intMatcher.test(v.toString())
return false
}
$MOD$.isFloat = function (v) {
const floatMatcher = /^\d*\.\d+$/
if (typeof v === 'float' || v instanceof Number) return $MOD$.int(v) !== v
if (typeof v === 'string') return floatMatcher.test(v)
if (typeof v === 'object' && v.toString) floatMatcher.test(v.toString())
return false
}
$MOD$.getByKey = function (obj, ...keys) {
let o = {}
for (let k in obj) {
if (keys.indexOf(k) >= 0) {
o[k] = obj[k]
}
}
return o
}
`,
}) })
} }

104
util.ts
View File

@ -86,6 +86,32 @@ export default {
bool, bool,
searchFile, searchFile,
loadConfig, loadConfig,
keysBy,
countBy,
listBy,
hasBy,
getBy,
setBy,
indexBy,
removeBy,
removeArrayItem,
last,
len,
mergeBy,
sortBy,
in: _in,
uniquePush,
clearEmpty,
split,
splitWithoutEmpty,
joinWithoutEmpty,
makeDefault,
copy,
deepCopy,
isNumber,
isInt,
isFloat,
getByKey,
} }
function json(data: any): string { return '' } function json(data: any): string { return '' }
@ -173,3 +199,81 @@ function bytes(value: any): any { return null }
function bool(value: any): boolean { return false } function bool(value: any): boolean { return false }
function searchFile(filename: string, searchPath?: string): string { return '' } function searchFile(filename: string, searchPath?: string): string { return '' }
function loadConfig(name: string): any { return null } function loadConfig(name: string): any { return null }
// 返回符合条件的key列表支持数组和Object对象
function keysBy(obj: Array<Object>, ...fieldAndValues: any): any[] { return [] }
// 返回符合条件的对象个数支持数组和Object对象
function countBy(obj: Array<Object>, ...fieldAndValues: any): number { return 0 }
// 返回符合条件的对象列表支持数组和Object对象
function listBy(obj: Array<Object>, ...fieldAndValues: any): Array<Object> { return [] }
// 对象是否存在支持数组和Object对象
function hasBy(obj: Array<Object>, ...fieldAndValues: any): boolean { return false }
// 返回符合条件的对象支持数组和Object对象
function getBy(obj: Array<Object>, ...fieldAndValues: any): Object { return {} }
// 按条件替换对象支持数组和Object对象
function setBy(obj: Array<Object>, value: any, ...fieldAndValues: any): void { }
// 返回符合条件的对象索引支持数组和Object对象
function indexBy(obj: Array<Object>, ...fieldAndValues: any): number { return -1 }
// 删除符合条件的对象支持数组和Object对象
function removeBy(obj: Array<Object>, ...fieldAndValues: any): number { return 0 }
// 数组删除对象
function removeArrayItem(list: Array<Object>, item: Object): void { }
// 返回数组的最后一个元素
function last(arr: Array<any>): any { return null }
// 返回数组或对象的元素个数
function len(obj: Object): number { return 0 }
// 按照指定的字段合并支持数组和Object对象
function mergeBy(olds: Array<Object>, news: Array<Object>, ...fields: any): Array<Object> { return [] }
// 按照指定字段对数组排序sortType支持number、int、float、string等
function sortBy(obj: Array<Object>, field: string, isReverse = false, sortType?: string): Array<Object> { return [] }
// 检查是否包含如果arr是一个字符串则用逗号分割再判断
function _in(arr: any[], checkValue: any): boolean { return false }
// 添加元素到数组(如果已经存在则忽略)
function uniquePush(arr: Array<any>, ...values: any): void { }
// 从数组中删除空元素
function clearEmpty(arr: Array<any>) { return null }
// 切割指定个数,剩余的部份保留在最后一个元素中
function split(str: string, sep: string, n: number = 0): string[] { return [] }
// 切割字符串,忽略空白的元素,如果指定了个数剩余的部份保留在最后一个元素中
function splitWithoutEmpty(str: string, sep: string, n: number = 0): string[] { return [] }
// 拼接数组,忽略空白的元素
function joinWithoutEmpty(arr: Array<any>, sep: string): string { return '' }
// 根据默认值填充对象中空的元素
function makeDefault(item: Object, defaults: Object): void { }
// 复制对象浅拷贝仅复制第一层支持数组和Object对象
function copy(obj: Object): Object { return {} }
// 复制对象深拷贝递归复制支持数组和Object对象
function deepCopy(obj: Object): Object { return {} }
// 判断是否是数字
function isNumber(v: any): boolean { return false }
// 判断是否是整数
function isInt(v: any): boolean { return false }
// 判断是否是浮点数
function isFloat(v: any): boolean { return false }
// 从对象中获取指定的key生成新的对象返回
function getByKey(obj: Object, ...keys: string[]): Object { return {} }