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
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 |
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 |
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 |
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 |
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) |
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 |
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 |
Convert radians to degrees.
Name |
Type |
Description |
rad |
Float |
Angle in radians |
Returns |
Description |
Float |
Angle in degrees |
Convert degrees to radians.
Name |
Type |
Description |
deg |
Float |
Angle in degrees |
Returns |
Description |
Float |
Angle in radians |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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) |
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 |
Detect the image type from the first few bytes of image data.
Checks for PNG and JPEG magic bytes.
Returns |
Description |
ImageType |
The detected ImageType (PNG, JPEG) or null if unknown |
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 |
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 |
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 |
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