@gulujs/toml
Version:
TOML parser and serializer
31 lines (30 loc) • 1.21 kB
JavaScript
import { TYPE_ERROR_MESSAGE } from '../../errors/index.js';
import { stringifyKey } from '../utils.js';
import { tryStringifyKeyValue } from './stringify-value.js';
import { assertIsTable } from './table.js';
export function assertIsArrayOfTables(value, _options, path) {
if (Array.isArray(value)) {
return;
}
throw new TypeError(TYPE_ERROR_MESSAGE(path, 'is not an array'));
}
export function serializeArrayOfTables(arr, schema, options, path) {
const lines = [];
for (let i = 0; i < arr.length; i++) {
const obj = arr[i];
assertIsTable(obj, options, path);
lines.push(`[[${stringifyKey(schema.key, options.objectPath.keyPath)}]]`);
for (let j = 0; j < schema.items.length; j++) {
const itemSchema = schema.items[j];
const value = options.objectPath.get(obj, itemSchema.key);
if (typeof value === 'undefined' || value === null) {
if (!options.strict) {
continue;
}
}
tryStringifyKeyValue(value, itemSchema, options, [...path, i, itemSchema.key], lines);
}
lines.push('');
}
return lines.join(options.newline);
}