Utils

ceramic.Utils (Class)

Various utilities. Some of them are used by ceramic itself or its backends.

This class provides a collection of utility functions for common tasks including:

  • Path manipulation and resolution
  • ID generation (unique, random, persistent)
  • String conversions (camelCase, UPPER_CASE)
  • Mathematical operations (interpolation, trigonometry)
  • Platform detection (iOS, Android)
  • Stack trace handling
  • Command execution
  • And many more general-purpose utilities

Most methods are static and can be called directly without instantiation.

Static Members

realPath(path: String): String

Convert a relative asset path to an absolute path. If the path is already absolute or is an HTTP(S) URL, it is returned unchanged. Otherwise, it is resolved relative to the app's assets path.

Name Type Description
path String The path to resolve
Returns Description
String The absolute path

getRtti(c: Class<getRtti.T>): haxe.rtti.Classdef

Get runtime type information (RTTI) for a class. This provides metadata about the class structure, fields, and methods.

Name Type Description
c Class<getRtti.T> The class to get RTTI for
Returns Description
haxe.rtti.Classdef The class definition metadata

uniqueId(): String

Provides an identifier which is guaranteed to be unique on this local device. It however doesn't guarantee that this identifier is not predictable.

The ID format is: base62-base62-base62-base62-base62-base62-base62

Thread-safe on platforms that support threading.

Returns Description
String A unique identifier string

randomId(?size: Int = 32): String

Provides a random identifier which should be fairly unpredictable and should have an extremely low chance to provide the same identifier twice.

Name Type Default Description
size Int 32 The length of the ID to generate (default: 32)
Returns Description
String A random identifier string of the specified length

persistentId(?slot: Int = 0, ?size: Int = 32): String

Return a persistent identifier for this device. The identifier is expected to stay the same as long as the user keeps the app installed. Multiple identifiers can be generated/retrieved by using different slots (default 0). Size of the persistent identifier can be provided, but will only have effect when generating a new identifier.

The ID is stored on disk and retrieved on subsequent calls.

Name Type Default Description
slot Int 0 The slot number for storing multiple IDs (default: 0)
size Int 32 The length of the ID when generating a new one (default: 32)
Returns Description
String The persistent identifier for this device and slot

resetPersistentId(?slot: Int = 0): Void

Reset (delete) a persistent identifier for the given slot. The next call to persistentId() for this slot will generate a new ID.

Name Type Default Description
slot Int 0 The slot number to reset (default: 0)

base62Id(?val: Int): String

Generate a base62 encoded string from an integer value. Base62 uses 0-9, A-Z, and a-z characters.

Name Type Default Description
val Int (optional) The integer to encode, or null to use a random value
Returns Description
String A base62 encoded string

println(data: String): Void

Print a line to the console/output. Handles different platforms appropriately (console.log on web, Sys.println on native).

Name Type Description
data String The string to print

printStackTrace(?returnOnly: Bool = false): String

Print or return the current stack trace. Useful for debugging to see the call hierarchy.

Name Type Default Description
returnOnly Bool false If true, only returns the stack trace string without printing
Returns Description
String The stack trace as a string

stackItemToString(item: Anonymous): String

Convert a stack trace item to a human-readable string. Handles source map resolution on JavaScript platforms.

Name Type Description
item Anonymous The stack item to convert
Returns Description
String A formatted string representation of the stack item

radToDeg(rad: Float): Float

Convert radians to degrees.

Name Type Description
rad Float Angle in radians
Returns Description
Float Angle in degrees

degToRad(deg: Float): Float

Convert degrees to radians.

Name Type Description
deg Float Angle in degrees
Returns Description
Float Angle in radians

round(value: Float, ?decimals: Int = 0): Float

Round a float value to a specified number of decimal places.

Name Type Default Description
value Float The value to round
decimals Int 0 Number of decimal places (default: 0)
Returns Description
Float The rounded value

hashCode(s: String): Int

Java's String.hashCode() method implemented in Haxe. Generates a 32-bit integer hash from a string.

source: https://github.com/rjanicek/janicek-core-haxe/blob/master/src/co/janicek/core/math/HashCore.hx

Name Type Description
s String The string to hash
Returns Description
Int A 32-bit integer hash code

uniformFrequencyList(values: Array<Int>, frequencies: Array<Float>, size: Int): Array<Int>

Generate a uniform list of the requested size, containing values uniformly distributed based on frequencies.

This creates a list where values appear proportionally to their frequencies, but distributed as evenly as possible throughout the list.

Name Type Description
values Array<Int> The values to put in the list
frequencies Array<Float> The corresponding frequency for each value
size Int The size of the final list
Returns Description
Array<Int> An array with values distributed according to their frequencies

upperCaseToCamelCase(input: String, ?firstLetterUppercase: Bool = true): String

Transforms SOME_IDENTIFIER to SomeIdentifier (PascalCase) or someIdentifier (camelCase).

Name Type Default Description
input String The UPPER_CASE string to convert
firstLetterUppercase Bool true If true, produces PascalCase; if false, produces camelCase
Returns Description
String The converted string

camelCaseToUpperCase(input: String, ?firstLetterUppercase: Bool = true): String

Transforms SomeIdentifier/someIdentifier/some identifier to SOME_IDENTIFIER.

Name Type Default Description
input String The camelCase/PascalCase string to convert
firstLetterUppercase Bool true Not used in this function (kept for API compatibility)
Returns Description
String The UPPER_CASE string

functionEquals(functionA: Dynamic, functionB: Dynamic): Bool

Check if two function references are equal. Platform-specific implementation for optimal performance.

Name Type Description
functionA Dynamic First function reference
functionB Dynamic Second function reference
Returns Description
Bool true if the functions are the same reference

decodeUriParams(raw: String): Map

Decode URL-encoded parameters into a key-value map.

Example: "foo=bar&hello=world" becomes {"foo" => "bar", "hello" => "world"}

Name Type Description
raw String The raw URL parameter string
Returns Description
Map A map of decoded parameter names to values

sinRatio(value: Float): Float

Transforms a value between 0 and 1 to another value between 0 and 1 following a sinusoidal curve. Useful for creating smooth, wave-like animations.

Name Type Description
value Float A value between 0 and 1. If giving a value > 1, its modulo 1 will be used.
Returns Description
Float A value between 0 and 1 following a sine wave pattern

cosRatio(value: Float): Float

Transforms a value between 0 and 1 to another value between 0 and 1 following a cosinusoidal curve. Useful for creating smooth, wave-like animations.

Name Type Description
value Float A value between 0 and 1. If giving a value > 1, its modulo 1 will be used.
Returns Description
Float A value between 0 and 1 following a cosine wave pattern

valueFromInterpolatedKey(keys: Array<Float>, values: Array<Float>, interpolatedKey: Float): Float

Given an array of keys and an array of matching values, interpolate a new value from interpolatedKey. Performs linear interpolation between adjacent key-value pairs.

Example: keys=[0, 10, 20], values=[100, 200, 150], interpolatedKey=5 returns 150

Name Type Description
keys Array<Float> A sorted list of keys
values Array<Float> A list of values corresponding to each key
interpolatedKey Float The key to interpolate a value for
Returns Description
Float The interpolated value

yFromInterpolatedX(points: Array<Float>, interpolatedX: Float): Float

Given an array of X and Y values, interpolate a new Y value from interpolated X. The points array should contain alternating X and Y values: [x0, y0, x1, y1, x2, y2, ...]

Name Type Description
points Array<Float> A list of X and Y values (must have even length)
interpolatedX Float The X value to interpolate a Y value for
Returns Description
Float The interpolated Y value

command(cmd: String, ?args: Array<String>, ?options: AnonStruct, result: Function): Void

Execute a system command asynchronously. Platform-specific implementation using Process on native platforms or child_process on Node.js.

Name Type Default Description
cmd String The command to execute
args Array<String> (optional) Optional array of command arguments
options AnonStruct (optional) Optional execution options: - cwd: Working directory for the command - detached: Whether to detach the process
result Function Callback with (exitCode, stdout, stderr)

replaceIdentifier(str: String, word: String, replacement: String): String

Replace whole-word occurrences of an identifier in a string. Only replaces the word when it's not part of a larger identifier.

Example: replaceIdentifier("foo + fooBar", "foo", "bar") returns "bar + fooBar"

Name Type Description
str String The string to search in
word String The identifier to replace
replacement String The replacement string
Returns Description
String The string with identifiers replaced

imageTypeFromBytes(bytes: haxe.io.Bytes): ImageType

Detect the image type from the first few bytes of image data. Checks for PNG and JPEG magic bytes.

Name Type Description
bytes haxe.io.Bytes The image file bytes
Returns Description
ImageType The detected ImageType (PNG, JPEG) or null if unknown

lerp(a: Float, b: Float, t: Float): Float

Linear interpolation between two values.

Name Type Description
a Float Start value (returned when t=0)
b Float End value (returned when t=1)
t Float Interpolation factor (0 to 1)
Returns Description
Float The interpolated value

isIos(): Bool

Returns true if the current platform is iOS, which is the case when we are running a native iOS app or when we are running on web from an iOS mobile browser.

The result is cached for performance on web platforms.

Returns Description
Bool true if running on iOS

isAndroid(): Bool

Returns true if the current platform is Android, which is the case when we are running a native Android app or when we are running on web from an Android mobile browser.

The result is cached for performance on web platforms.

Returns Description
Bool true if running on Android

sign(value: Float): Float

Returns the sign of a number.

Name Type Description
value Float The number to check
Returns Description
Float 1 if the value is above or equal to zero, -1 otherwise

Private Members

RE_ASCII_CHAR: EReg

RE_MODULE_AT: EReg