Skip to main content
  1. Lumesh Document/
  2. Lumesh Libs/Modules/

Lumesh Into Module

834 words·4 mins
Table of Contents

The Into module provides comprehensive data type conversion capabilities, supporting conversions between different data types, including basic type conversions, time parsing, data serialization, and parsing third-party command outputs. All conversion functions offer detailed error handling and type validation.

Function Overview
#

Function CategoryMain FunctionsPurpose
Basic Type Conversionstr, int, float, boolean, filesizeBasic data type conversions
Time ConversiontimeString to date-time conversion
Table ConversiontableConvert command output to table format
Serializationtoml, json, csvSerialize data into different formats
HighlighthighlightedConvert to Ansi Highlighted code

Basic Type Conversion Functions
#

str <value> - Format an expression as a string

  • Parameters: value (required): Any - The value to convert
  • Returns: String - The formatted string representation
  • Example: Into.str(123) returns "123"

int <value> - Convert a float or string to an integer

  • Parameters: value (required): Float|String - The value to convert
  • Returns: Integer - The converted integer
  • Error: Throws an error if conversion fails
  • Example:
    • Into.int("123") returns 123
    • Into.int(3.14) returns 3

float <value> - Convert an integer or string to a float

  • Parameters: value (required): Integer|String - The value to convert
  • Returns: Float - The converted float
  • Error: Throws an error if conversion fails
  • Example:
    • Into.float("3.14") returns 3.14
    • Into.float(123) returns 123.0

boolean <value> - Convert a value to a boolean

  • Parameters: value (required): Any - The value to convert
  • Returns: Boolean - The converted boolean
  • Logic: Uses Lumesh's truthiness rules

filesize <size_str> - Parse a file size string into bytes

  • Parameters: size_str (required): String - File size string (e.g., "1KB", "2MB", "3GB")
  • Returns: Integer - Corresponding byte count
  • Supported Units: B, KB, MB, GB, TB, PB
  • Example:
    • Into.filesize("1KB") returns 1024
    • Into.filesize("2.5MB") returns 2621440

Time Conversion Functions
#

time <datetime_str> [datetime_template] - Convert a string to a date-time

  • Parameters:
    • datetime_str (required): String - Date-time string
    • datetime_template (optional): String - Date-time format template
  • Returns: DateTime - The parsed date-time object
  • Example:
    • Into.time("2023-12-25") - Parses ISO format date
    • Into.time("25/12/2023", "%d/%m/%Y") - Uses custom format

Table Conversion Functions
#

table [headers|header...] <command_output> - Convert third-party command output to a table

  • Parameters:
    • headers (optional): List[String]|String... - Table header definitions
    • command_output (required): String - Command output string
  • Returns: List[Map] - Structured table data
  • Purpose: Parse outputs from commands like ps, ls, df into structured data
  • Example:
    1ps aux | Into.table()
    2ls -l | Into.table("mode", "links", "owner", "group", "size", "date", "name")

Serialization Functions
#

toml <expr> - Serialize a Lumesh expression to TOML

  • Parameters: expr (required): Any - Expression to serialize
  • Returns: String - TOML format string
  • Supported Types: Maps, lists, basic types
  • Example: Into.toml({"name": "Alice", "age": 30})

json <expr> - Serialize a Lumesh expression to JSON

  • Parameters: expr (required): Any - Expression to serialize
  • Returns: String - JSON format string
  • Supported Types: Maps, lists, basic types
  • Example: Into.json([1, 2, 3]) returns "[1,2,3]"

csv <expr> - Serialize a Lumesh expression to CSV

  • Parameters: expr (required): List[Map] - Table data to serialize
  • Returns: String - CSV format string
  • Requirements: Input must be a list of maps (table format)
  • Example:
    1[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}] | Into.csv()

Highlight
#

highlighted <script_string> - Syntax highlight a script string

  • Parameters: script_string (required): String - Script string to highlight
  • Returns: String - Highlighted string
  • Example:
    1Into.highlighted "let x = 10; print x"
    2# Returns a highlighted string with ANSI color codes
    3
    4Fs.read "script.lm" | Into.highlighted() | print
    5# Highlight and display the content of the script file

striped <string> - Remove all ANSI escape codes

  • Parameters: string (required): String - String containing ANSI escape codes
  • Returns: String - Plain text without escape codes

Usage Examples
#

Basic Type Conversion
#

1# Number conversion
2Into.int("42")        # Returns 42
3Into.float("3.14")    # Returns 3.14
4Into.str(123)         # Returns "123"
5
6# Boolean conversion
7Into.boolean(1)       # Returns true
8Into.boolean("")      # Returns false

File Size Conversion
#

1# Parse file sizes
2Into.filesize("1KB")    # Returns 1024
3Into.filesize("2.5MB")  # Returns 2621440
4Into.filesize("1GB")    # Returns 1073741824

Data Serialization
#

 1# JSON serialization
 2let data = {"users": [{"name": "Alice"}, {"name": "Bob"}]}
 3Into.json(data)
 4
 5# TOML serialization
 6let config = {"database": {"host": "localhost", "port": 5432}}
 7Into.toml(config)
 8
 9# CSV serialization
10Fs.ls("-l") | Into.csv()

Command Output Parsing
#

1# Parse system command output
2ps aux | Into.table() | where(cpu > 5.0)
3df -h | Into.table() | select("filesystem", "used", "available")

Pipeline Operation Examples
#

 1# Data processing pipeline
 2"123.45" | Into.float() | Math.round() | Into.str()
 3# Result: "123"
 4
 5# File size processing
 6Fs.ls("-l") | List.map((f) -> Into.filesize(f.size)) | List.sum()
 7# Calculate total size of the directory
 8
 9# Configuration file generation
10{"server": {"port": 8080, "host": "0.0.0.0"}} | Into.toml() | Fs.write("config.toml")

Notes
#

The Into module is an important data conversion tool in Lumesh, providing type-safe conversion mechanisms. All conversion functions include detailed error handling to ensure clear error messages when conversions fail. Parameter type descriptions indicate that <> denotes required parameters, and [] denotes optional parameters. Notably, file size conversions support common storage units, and time conversions support various date formats.

Most functions are already connected to the corresponding types and can be used with chained calls, such as:

1"36".to_int()
2'2000K'.to_filesize()

Related