Heading 2 Recipes class mt-12 Heading 3 Read object properties class mb-2 Markdown Read known properties from a Terrace block and write them to an object. CodeBlock javascript // Provides simple convenience functions over the core parser // CommonJS: const { useDocument } = require('@terrace-lang/js/document') import { useDocument } from '@terrace-lang/js/document' // A helper for iterating over a string line-by-line // CommonJS: const { createStringReader } = require('@terrace-lang/js/readers/js-string') import { createStringReader } from '@terrace-lang/js/readers/js-string' const input = ` object string_property An example string numeric_property 4 ` const output = { string_property: null, numeric_property: null } // useDocument returns convenience functions const { next, level, head, tail, match } = useDocument(createStringReader(input)) // next() parses the next line in the document while (await next()) { // match('object') is equivalent to head() === 'object' // Essentially: "If the current line starts with 'object'" if (match('object')) { const objectLevel = level() // When we call next with a parent level it, // only iterates over lines inside the parent block while (await next(objectLevel)) { // tail() returns the part of the current line after the first space if (match('string_property')) output.string_property = tail() // parseFloat() here the string tail() to a numeric float value if (match('numeric_property')) output.numeric_property = parseFloat(tail()) } } } console.dir(output) // { string_property: 'An example string', numeric_property: 4 } Markdown Read *all* properties as strings from a Terrace block and write them to an object. CodeBlock javascript // Provides simple convenience functions over the core parser // CommonJS: const { useDocument } = require('@terrace-lang/js/document') import { useDocument } from '@terrace-lang/js/document' // A helper for iterating over a string line-by-line // CommonJS: const { createStringReader } = require('@terrace-lang/js/readers/js-string') import { createStringReader } from '@terrace-lang/js/readers/js-string' const input = ` object property1 Value 1 property2 Value 2 random_property igazi3ii4quaC5OdoB5quohnah1beeNg ` const output = {} // useDocument returns convenience functions const { next, level, head, tail, match } = useDocument(createStringReader(input)) // next() parses the next line in the document while (await next()) { // match('object') is equivalent to head() === 'object' // Essentially: "If the current line starts with 'object'" if (match('object')) { const objectLevel = level() // When we call next with a parent level, // it only iterates over lines inside the parent block while (await next(objectLevel)) { // Skip empty lines if (!line()) continue // Add any properties to the object as strings using the // line head() as the key and tail() as the value output[head()] = tail() } } } console.dir(output) // { property1: 'Value 1', property2: 'Value 2', random_property: 'igazi3ii4quaC5OdoB5quohnah1beeNg' }