* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
import path from 'path'
export const setString = (str, len) => {
let StrLen = 0
let s = ''
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 128) {
StrLen += 2
} else {
StrLen++
}
s += str.charAt(i)
if (StrLen >= len) {
return s + '...'
}
}
return s
}
export const OptionFormat = (GetOptions) => {
let options = '{'
for (let n = 0; n < GetOptions.length; n++) {
options = options + "'" + GetOptions[n].option_name + "':'" + GetOptions[n].option_value + "'"
if (n < GetOptions.length - 1) {
options = options + ','
}
}
return JSON.parse(options + '}')
}
export const HovercUnique = (arr) => {
const n = {}
const r = []
for (let i = 0; i < arr.length; i++) {
if (!n[arr[i]]) {
n[arr[i]] = true
r.push(arr[i])
}
}
return r
}
export const getJsonLength = (jsonData) => {
return Object.keys(jsonData).length
}
export const getResponseData = (data, error) => {
const res = {
data
}
if (error) {
const err_code = error.code
res.error = {
code: err_code,
message: error.message
}
}
return res
}
export const getDatabasePath = (fileName) => {
const databasePath = process.env.DATABASE_PATH || path.resolve(__dirname, '../database')
return path.resolve(databasePath, fileName)
}