Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

JS: String, list and general helpers

String, list and general helpers

Source code: src/helpers.js


String comparison

These four keep the names and the return values of the C library, so a gclass that moves between the two languages keeps its comparisons.

strcmp(str1, str2)

Compares two strings. Returns a negative number, 0 or a positive number.

strncmp(str1, str2, lgth)

Compares the first lgth characters of two strings.

strcasecmp(str1, str2)

Compares two strings, and ignores the case.

strstr(haystack, needle, bool)

Finds needle in haystack. Returns the part of haystack that begins at the first occurrence. With bool set to true it returns the part before the occurrence instead. Returns false when there is no occurrence.

cmp_two_simple_json(jn_var1, jn_var2)

Compares two simple JSON values, such as a string or a number. Returns a negative number, 0 or a positive number.


Lists

str_in_list(list, str, ignore_case)

Tells if a string is in a list.

strs_in_list(list, strs, ignore_case)

Tells if one of the strings of strs is in a list.

index_in_list(list, elm)

Gives the index of an element in a list. Returns -1 when the list does not hold it.

id_index_in_obj_list(list, id)

Gives the index of the record with the field id in a list of records. Returns -1 when the list does not hold it.

delete_from_list(list, elm)

Takes an element out of a list, in place.

list2options(list, field_id, field_value)

Changes a list of strings, a list of records or an object of records into a list of options with the form [{id: "", value: ""}, …]. A select element and a combo box take that form.


Time

current_timestamp()

Gives the time of now as an ISO 8601 string with the milliseconds and the time zone, such as "2024-02-24T14:05:30.123+0200".

get_now()

Gives the UTC time of now.

timeTracker(tracker_name, verbose)

Builds a small tracker of times, for a measurement in development.

const tracker = timeTracker("Track1");
tracker.mark("Step 1");
tracker.mark("Step 2");

General utilities

build_path(...segments)

Builds a path or a URL from its segments, with exactly one / between each pair. The first segment keeps its own leading /, so an absolute path stays absolute. The function removes a / at the end of each segment.

Use it for every path. A path that is built with a template string gives two slashes, or none, as soon as one segment changes.

clean_name(name)

Gives a name without the characters that a name must not hold.

node_uuid()

Builds a unique identifier for this browser, and keeps it.

debounce(func, wait, immediate)

Gives a function that waits wait milliseconds after the last call before it runs func. With immediate set to true it runs func on the first call instead of the last.

Use it for a rate that the user controls, such as the keys of a search box. Do not use it to delay work inside a gclass. Use gobj_post_event() for that.

get_function_name(func)

Gives the name of a function. A trace of an action uses it.

create_json_record(json_desc, value)

Builds a record from a description of its fields, and puts the default value of each field in it.