@gulujs/toml
Version:
TOML parser and serializer
44 lines (43 loc) • 1.62 kB
JavaScript
import { TYPE_ERROR_MESSAGE } from '../../errors/index.js';
import { SchemaType } from '../schema.js';
import { tryStringifyKeyValue } from './stringify-value.js';
export function assertIsInlineTable(value, options, path) {
if ((typeof value === 'object' && value !== null)
|| (typeof value === 'function' && options.treatFunctionAsObject)) {
return;
}
throw new TypeError(TYPE_ERROR_MESSAGE(path, 'is not an object'));
}
export function serializeInlineTable(obj, schema, options, path) {
const keyValuePairs = [];
const propertyOptions = {
...options,
topLevel: false
};
if (schema.items === SchemaType.Any) {
const keys = Object.keys(obj);
for (const key of keys) {
const value = obj[key];
if (typeof value === 'undefined' || value === null) {
continue;
}
tryStringifyKeyValue(value, {
type: SchemaType.Any,
key: [key]
}, propertyOptions, [...path, key], keyValuePairs);
}
}
else {
for (let i = 0; i < schema.items.length; i++) {
const propertySchema = schema.items[i];
const value = options.objectPath.get(obj, propertySchema.key);
if (typeof value === 'undefined' || value === null) {
if (!options.strict) {
continue;
}
}
tryStringifyKeyValue(value, propertySchema, propertyOptions, [...path, propertySchema.key], keyValuePairs);
}
}
return `{ ${keyValuePairs.join(', ')} }`;
}