MCPcopy Create free account
hub / github.com/AlexErrant/Pentive / flattenObject

Function flattenObject

app/src/sqlite/settings.ts:142–171  ·  view source on GitHub ↗
(obj: SettingRecord, parentKey = '')

Source from the content-addressed store, hash-verified

140export const delimiter = '/'
141
142export function flattenObject(obj: SettingRecord, parentKey = '') {
143 const result: Record<string, SettingValue> = {}
144 const entries = objEntries(obj)
145 if (entries.length === 0) {
146 C.toastFatal('Settings object must have at least 1 key-value pair.')
147 }
148 for (const [key, value] of entries) {
149 if (key === '__proto__') {
150 C.toastFatal('"__proto__" is not a valid key name.')
151 }
152 if (key.length === 0) {
153 C.toastFatal('key cannot be empty')
154 }
155 if (key.includes(delimiter)) {
156 C.toastFatal(
157 `The '${delimiter}' character is not allowed in a key's name: ${key}`,
158 )
159 }
160 if (typeof value === 'number' && isNaN(value)) {
161 C.toastFatal('"NaN" is not a valid value.')
162 }
163 const newKey = parentKey === '' ? key : `${parentKey}${delimiter}${key}`
164 if (typeof value === 'object' && !Array.isArray(value) && value != null) {
165 Object.assign(result, flattenObject(value, newKey))
166 } else {
167 result[newKey] = value
168 }
169 }
170 return result
171}
172
173export function unflattenObject(
174 flattened: Array<readonly [string, SettingEntity['value']]>,

Callers 1

bulkUploadSettingsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected