Skip to content

Object Functions ​

camel ​

ts
obj.camel(): object

Recursively converts object keys to camel case and returns a new object. Useful when you need to pass data to JavaScript and want to convert keys before calling the json function. Doesn't mutate the original object.

Input Example: ​

textwire
{{ { First_Name: "Serhii", LastName: "Cho" }.camel() }}

Output: ​

html
{firstName: "Serhii", lastName: "Cho"}

Conversion Rules ​

  • Separators. _, -, . and space are removed, and the next letter is uppercased. Example: user_name becomes userName.
  • Edges. Leading and trailing spaces are trimmed.
  • First letter. The first letter is lowercased. Example: Title becomes title.
  • Uppercase runs. Uppercase letters after an uppercase letter are lowercased. Example: CreatedAt becomes createdAt, userID becomes userId, URL becomes url.
  • Digits. Digits are kept, and the letter after a digit is uppercased. Example: item_2nd becomes item2Nd.
  • Other characters. Any other character is removed and doesn't uppercase the next letter. Example: na$me becomes name, naïve becomes nave.
  • Nested objects. Objects inside objects are converted too. Arrays are returned as is, so objects inside arrays keep their original keys.
  • Key collisions. When several keys convert to the same key, only one is kept. A key that is already in camel case wins. Otherwise, the key that comes first in byte order wins. Example: {a_b: 1, aB: 2}.camel() returns {aB: 2}.

Important Notes ​

  • Use json() for JSON. camel() returns a Textwire object. Chain .json() if you need a JSON string, for example {{ obj.camel().json() }}.

json ​

ts
obj.json(): string

Converts object to a JSON string and returns it.

Input Example: ​

textwire
{{ { name: "Chiori", element: "Geo" }.json() }}

Output: ​

html
{"element":"Geo","name":"Chiori"}

Important Notes ​

  • Raw (non-encoded) JSON. To get non-encoded JSON, use raw embedding:
    textwire
    {!! myObj.json() !!}
    {{-- Output: {"name":"Chiori","element":"Geo"} --}}
  • Pass to JavaScript through props. Look at this Vue.js example showing how to pass JSON object from Textwire to JavaScript frameworks using props:
    textwire
    <post-likes :post="{{ post.camel().json() }}" />
  • Non-finite floats become null in JSON. +Inf, -Inf, and NaN are serialized as null.

get ​

ts
obj.get(path: string): any

get function retrieves values from the object using a dot notation. It accepts a string as an argument, which represents the path to the desired value within the object. If the specified path does not exist, it returns nil.

Input Example: ​

textwire
{{ obj = { game: {genshin: {char: {name: "Chiori"}}}} }}
{{ obj.get('game.genshin.char.name') }}

Output: ​

json
Chiori

Important Notes ​

  • Deeply nested keys. The get function is particularly useful when you need to access nested keys within an object without having to worry about whether each level of the object exists. It helps prevent errors that can occur when trying to access keys on nil. For example, if you try to access obj.game.genshin.char.name directly and any of those keys (game, genshin, name or char) do not exist, it would throw an error. Using get allows you to safely retrieve the value without risking an error, as it will simply return nil if the path does not exist.
  • Accessing non-valid keys. Another use-case is when you need to access a key that is not a valid identifier, like 1st, naïve, 日本, na$me or even an empty string.
  • Keys with dot. If your key contains a dot (.) in its name, you can access it simply by specifying the key name. Example: {{ {"x.y": "Y"}.get('x.y') }} will result in Y.