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()¶
Gives the unique identifier of this browser (a uuid). The function gives the
same uuid for all the life of the page (gobj-js 7.26.28). The yuno writes it in
its node_uuid attribute, and C_IEVENT_CLI sends it as
the id of the identity card and as the host of each message.
The local storage keeps the uuid in the key
host_uuid. The first page makes it and writes it there. The next pages read it.When the store cannot keep the uuid, the page makes a uuid and keeps it in memory. When the data of the site is blocked or the store is full, the function logs one WARNING. When there is no
localStorageor no browser (a test under node), it logs nothing.Without
crypto.randomUUID()(a page onhttpwithout TLS is not a secure context), the function makes a uuid v4 withcrypto.getRandomValues(). Withoutcrypto, it usesMath.random().A stored value that is not a uuid (a foreign or corrupt write:
"garbage","42") is replaced by a new uuid, written to the store, with one WARNING (gobj-js 7.26.29).
Before 7.26.28, the function gave a new uuid on each call when the store could
not keep it, and null without crypto.randomUUID().
const a = node_uuid(); // "3f2c9a6e-0d4b-4c1e-9a7f-5b8e2d1c4a90"
const b = node_uuid(); // the same uuid, also with the store blockeddebounce(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.