Stof Docs
Star on GitHubStof WebsiteDiscordContact Us
  • 🚀Welcome
  • â„šī¸Resources & Information
  • Book
    • Data Interface
    • Introduction & Design
    • Host Environments
      • Rust
      • TypeScript
        • Extend Stof
        • Import Statement
    • Data Interchange
      • JSON to Stof
      • Importing and Parsing Data
      • Exporting Data
      • REST and Restructure
    • Configuration Files
    • Schemas
      • Renaming Fields
      • Removing Fields
      • Validation & Transformation
        • Example Access
      • Nested Schemas
    • Orchestration
  • Common Concepts
    • Objects
    • Primitive Types
    • Functions
    • Fields
    • Object Types
    • Imports
    • Error Handling
    • Units
    • Casting/Conversions
  • Reference
    • CLI
      • 🏃Run
      • đŸ§ĒTest
      • 📚CLI Libraries
        • Filesystem Library
        • Time Library
        • HTTP Library
    • Stof Language
    • Libraries
      • Standard Library
      • Array/Vector Library
      • Number Library
      • String Library
      • Object Library
      • Function Library
      • Set Library
      • Map Library
      • Data Library
      • Tuple Library
      • Blob Library
      • Boolean Library
    • đŸĒ§Formats
Powered by GitBook
On this page

Was this helpful?

  1. Book
  2. Host Environments
  3. TypeScript

Import Statement

Enabling Stof import statements.

PreviousExtend StofNextData Interchange

Last updated 3 months ago

Was this helpful?

Stof import statements use the . Although this library is not enabled by default in a WebAssembly context, we can add it ourselves.

This is for non-browser JavaScript, where system access is available.

See Imports for more information on the Stof import statement.

Text Files

For the Stof import statement, we need the 'fs.read(..)' function. This function is used internally by the loaded formats in the document.

Note that if you are aware of the file to be included, it's probably easier to use the "importString(..)" function after reading the file into a string yourself.

import { Stof } from 'jsr:@formata/stof';

const doc = await Stof.create();

doc.insertLibrary('fs', [
    ['read', (path: string): string => Deno.readTextFileSync(path)]
]);
doc.importString('stof', `
    import 'import.toml'; // Calls fs.read('import.toml') in the background...

    #[main]
    fn main() {
        console.log('Hello, ' + self.name);
    }
`);

doc.run();
name = "Bob Smith"
age = 44
> deno run --allow-all example.ts
Hello, Bob Smith
file system library