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

TypeScript

Embedding Stof in JS/TS.

PreviousRustNextExtend Stof

Last updated 3 months ago

Was this helpful?

We've provided a to embed Stof in JavaScript. This can be used in the browser or a JavaScript runtime like Deno or Bun.

Because this package is intended for a wide range of uses, it does not come with any sort of system interface.

The main document interface "Stof" adds a few of the console functions (log, trace, info, error, and warn), however, does not add anything else for you (network access, file system access, etc.).

As such, some of the standard library Stof functions are not available (pln, err, dbg, trace, etc.). The ones that involve printing and the standard system interface that Rust provides.

In these examples, we're using the JavaScript runtime.

Hello, World!

Much like other libraries in the JS world, Stof uses a "parse" function to parse a string into a Stof document, with a given format.

As mentioned above, the normal print function "pln" will not work in this WebAssembly context. However, when the Stof document is created using the provided interface, it adds a few "console" library functions (log, trace, info, error, and warn).

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

const doc = await Stof.parse('stof', `
    #[main]
    fn hello() {
        console.log('Hello, world!');
    }
`);

doc.run();
> deno run --allow-net example.ts
Hello, world!
package on JSR
Deno