UNPKG

spawn-object

Version:

辅助 TableStore 列操作的工具函数,能够将大对象拆分为仅包含单个属性对象的列表

46 lines (39 loc) 845 B
'use strict' const typeasy = require('typeasy') const TableStore = require('tablestore') module.exports = function spawnObject(obj) { const list = [] if (typeasy(obj) !== 'object') { return list } Object.getOwnPropertyNames(obj).forEach((key) => { const value = obj[key] const type = typeasy(value) if (['string', 'boolean', 'uint8array'].includes(type)) { list.push({ [key]: value, }) } else if (type === 'number') { if (value % 1 === 0) { list.push({ [key]: TableStore.Long.fromNumber(value), }) } else { list.push({ [key]: value, }) } } else if (['object', 'array'].includes(type)) { try { list.push({ [key]: JSON.stringify(value), }) } catch (err) { // 空 } } else { // 空 } }) return list }