luafan

fan.utils

Utility functions for common operations including string generation, time handling, weak references, and string manipulation.

Functions

random_string(letters, count, join, joingroupcount)

Generate a random string with specified parameters.

Parameters:

Returns:

Example:

local utils = require "fan.utils"

-- Simple random string
local str1 = utils.random_string(utils.LETTERS_W, 10)
-- Output: "aB3xK9mL2p"

-- Grouped random string
local str2 = utils.random_string(utils.LETTERS_W, 12, "-", 4)
-- Output: "aB3x-K9mL-2p5Q"

gettime()

Get high-precision current time as a floating-point number.

Returns:

Example:

local utils = require "fan.utils"

local start_time = utils.gettime()
-- ... do some work ...
local elapsed = utils.gettime() - start_time
print("Elapsed time:", elapsed, "seconds")

Note: On LuaJIT, this function uses FFI for better performance via direct gettimeofday() call.

split(str, pat)

Split a string into an array using a pattern delimiter.

Parameters:

Returns:

Example:

local utils = require "fan.utils"

local parts = utils.split("hello,world,lua", ",")
-- Result: {"hello", "world", "lua"}

local paths = utils.split("/usr/local/bin", "/")
-- Result: {"usr", "local", "bin"}

weakify(…)

Create weak references to objects to prevent strong reference cycles.

Parameters:

Returns:

Example:

local utils = require "fan.utils"

local obj = {name = "test", data = {1, 2, 3}}
local weak_obj = utils.weakify(obj)

-- Access through weak reference
print(weak_obj.name)  -- "test"
weak_obj.new_field = "added"

-- Multiple objects
local obj1, obj2 = {a = 1}, {b = 2}
local weak1, weak2 = utils.weakify(obj1, obj2)

weakify_object(target)

Create a weak reference to a single object.

Parameters:

Returns:

Example:

local utils = require "fan.utils"

local original = {name = "example"}
local weak_ref = utils.weakify_object(original)

print(weak_ref.name)  -- "example"
-- If original goes out of scope, weak_ref operations will return nil

Constants

LETTERS_W

Pre-defined character set containing alphanumeric characters (both cases) and digits.

Value: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

Example:

local utils = require "fan.utils"

-- Generate random alphanumeric string
local token = utils.random_string(utils.LETTERS_W, 16)
print(token)  -- e.g., "aB3xK9mL2p5QwRtY"

Implementation Notes

Use Cases