import { isBasicType } from './Index'
* 返回指定个数的制表符字符串
* @param count
* @returns
*/
function setTabs(count: number): string {
let str = ''
for (let i = 0; i < count; i++) {
str += `\t`
}
return str
}
* 构造数组类型的文本内容
* @param arr 被格式化的对象数组
* @param tabCount 制表符个数
* @returns
*/
function convertArray(arr: Array<object>, tabCount: number = 0): string {
let formatterJSONArr = ''
const arrLen = arr.length
if (arrLen === 0) return formatterJSONArr += '[]'
const someSimple = arr.some(item => isBasicType(item))
if (someSimple) {
if (arrLen >= 5) {
formatterJSONArr += `[\n`
tabCount++
formatterJSONArr += `${setTabs(tabCount)}`
arr.forEach((item, index) => {
const lastIndex = index === arrLen - 1
if (null === item || isBasicType(item)) {
if (typeof item === 'string') {
formatterJSONArr += `"${item}"${lastIndex ? '\n' : ',\n'}`
} else {
formatterJSONArr += `${item}${lastIndex ? '\n' : ',\n'}`
}
} else {
if (item.constructor === Array) {
formatterJSONArr += convertArray(item, tabCount + 1)
} else {
formatterJSONArr += convertObject(item, tabCount + 1,!lastIndex)
}
formatterJSONArr += `\n`
}
if (!lastIndex) {
formatterJSONArr += `${setTabs(tabCount)}`
}
})
tabCount--
formatterJSONArr += `${setTabs(tabCount)}`
formatterJSONArr += `]`
} else {
formatterJSONArr += `[`
arr.forEach((item, index) => {
const lastIndex = index === arrLen - 1
if (null === item || isBasicType(item)) {
if (typeof item === 'string') {
formatterJSONArr += `"${item}"${lastIndex ? '' : ','}`
} else {
formatterJSONArr += `${item}${lastIndex ? '' : ','}`
}
} else {
if (item.constructor === Array) {
formatterJSONArr += convertArray(item, tabCount + 1)
} else {
formatterJSONArr += convertObject(item, tabCount + 1,!lastIndex)
}
formatterJSONArr += `${setTabs(tabCount)}`
}
})
formatterJSONArr += `]`
}
} else {
formatterJSONArr = '[\n'
tabCount++;
arr.forEach((item, index) => {
const lastIndex = index === arrLen - 1
formatterJSONArr += `${setTabs(tabCount)}`
formatterJSONArr += convertObject(item, tabCount,!lastIndex)
})
tabCount--
formatterJSONArr += `${setTabs(tabCount)}]`
}
return formatterJSONArr
}
* 构造对象类型的文本内容
* @param obj 被格式化的对象
* @param tabCount 制表符个数
* @param addComma 末尾是否需要增加逗号
* @returns
*/
function convertObject(obj: object, tabCount: number = 0, addComma: boolean = false): string {
const keys: Array<string> = Object.keys(obj)
const keysLen: number = keys.length
let formatterJSON: string = keys.length > 0 ? '{\n' : '{'
tabCount++;
keys.forEach((key: string, index) => {
const type = isBasicType(obj[key])
const isStr = typeof obj[key] === 'string'
const lastKey = index === keysLen - 1
if (null === obj[key] || type) {
formatterJSON += `${setTabs(tabCount)}"${key}": ${isStr ? '"' + obj[key] + '"' : obj[key]}${lastKey ? '' : ','}\n`
} else {
if (obj[key].constructor === Array) {
formatterJSON += `${setTabs(tabCount)}"${key}": ` + convertArray(obj[key], tabCount) + `${lastKey ? '' : ','}\n`
} else {
formatterJSON += `${setTabs(tabCount)}"${key}":`
formatterJSON += convertObject(obj[key], tabCount,!lastKey)
}
}
})
tabCount--
if (tabCount === 0) {
formatterJSON += `${setTabs(tabCount)}}${addComma ? ',' : ''}`
} else {
formatterJSON += `${setTabs(tabCount)}}${addComma ? ',' : ''}\n`
}
return formatterJSON
}
export {
convertArray, convertObject, setTabs
}