- Index
- »
- fan.haystack
- »
- Etc
Etc
@Js
const class Etc : Obj
Etc is the utility methods for Haystack.
Apply the given map function to each name/value pair to construct a new Dict
Remove a name/val pair from an exisiting dict, or if the name isn't found then return original dict
Convenience for makeDictsGrid
Coerce a value to a record Dict
Construct a grid for a Dict row
Construct a grid for a list of rows, where each row is a list of cells
Empty dict singleton
Make a Dict with four name/value pairs
Create a Dict with one non-nullable name/value pair
Set a name/val pair in an existing dict or if dict is null then create a new dict with given name/val pair
Make a Dict with five name/value pairs
Make a Dict with six name/value pairs
Coerce a value to one of the Haystack types
Unescape tag name from "__{utf-8 hex}" format
Create a Dict with four non-nullable name/value pairs
Create a Dict with five non-nullable name/value pairs
Create a Dict with two non-nullable name/value pairs
Create a Dict with three non-nullable name/value pairs
Return if any of the tag name/value pairs match the given function
Given a list of dictionaries, find all the common names used
Construct a grid with one column for a list
Make a Dict with one name/value pair
Get all the non-null values mapped by a dictionary
Make a Dict with two name/value pairs
Return if the given string is a legal kind name
Make a Dict with three name/value pairs
Construct an empty grid with just the given grid level meta-data
Return if two dicts are equal with same name/value pairs
Given a dict, attempt to find the best display string
Construct an object which wraps a dict and is suitable to use for a hash key in a Map
Coerce an object to a DateSpan
Return if the given string is a legal tag name
Make a Dict instance where val is one of the following
Map an exception to its standard tags
Coerce a value to a list of Ref identifiers
Convert a Dict to a read/write map
Construct a grid for a list of Dict rows
Iterate a discrete period string formatted in base64
Get a read/write list of the dict's name keys
Return a new Dict containing the name/value pairs for which f returns true
Coerce a value to a Ref identifier
Given two display strings, return 1, 0, or -1 if a is less than, equal to, or greater than b
Get the localized string for the given tag name for the current locale
Remove all names from the given dict
Convenience for makeDictGrid
Coerce a value to a list of record Dicts
Take an arbitrary string and convert into a safe tag name
Rename given name if its defined in the dict, otherwise return original
Return if all of the tag name/value pairs match the given function
Coerce an object to a Span with optional timezone
Coerce a value to a Grid
Flatten a list of grids into a single grid
Return the list of variable tag names used in the given macro pattern
Coerce dict to Haystack types, see toHaystack
Return if tag name starts with "__" as escaped name
Make a list of Dict instances using makeDict
Convert arbitary value to dispaly
Add/set all the name/value pairs in a with those defined in b
Construct a grid for an error response
Process macro pattern with given scope of variable name/value pairs
Escape tag name into "__{utf-8 hex}" format
Create a Dict with six non-nullable name/value pairs
Get a relative display name
static Void addArg(StrBuf b, Str name, Obj? arg)
static Int compareDis(Str a, Str b)
Given two display strings, return 1, 0, or -1 if a is less than, equal to, or greater than b. The comparison is case insensitive and takes into account trailing digits so that a dis str such as "Foo-10" is greater than "Foo-2".
static Dict dict0()
Empty dict singleton
static Dict dict1(Str n, Obj v)
Create a Dict with one non-nullable name/value pair
static Dict dict2(Str n0, Obj v0, Str n1, Obj v1)
Create a Dict with two non-nullable name/value pairs
static Dict dict3(Str n0, Obj v0, Str n1, Obj v1, Str n2, Obj v2)
Create a Dict with three non-nullable name/value pairs
static Dict dict4(Str n0, Obj v0, Str n1, Obj v1, Str n2, Obj v2, Str n3, Obj v3)
Create a Dict with four non-nullable name/value pairs
static Dict dict5(Str n0, Obj v0, Str n1, Obj v1, Str n2, Obj v2, Str n3, Obj v3, Str n4, Obj v4)
Create a Dict with five non-nullable name/value pairs
static Dict dict6(Str n0, Obj v0, Str n1, Obj v1, Str n2, Obj v2, Str n3, Obj v3, Str n4, Obj v4, Str n5, Obj v5)
Create a Dict with six non-nullable name/value pairs
static Bool dictAll(Dict d, |Obj?,Str->Bool| f)
Return if all of the tag name/value pairs match the given function.
static Bool dictAny(Dict d, |Obj?,Str->Bool| f)
Return if any of the tag name/value pairs match the given function.
static Bool dictEq(Dict a, Dict b)
Return if two dicts are equal with same name/value pairs. Value are compared via the Obj.equals method. Ordering of the dict tags is not considered.
static Dict dictFindAll(Dict d, |Obj?,Str->Bool| f)
Return a new Dict containing the name/value pairs for which f returns true. If f returns false for every pair, then return an empty Dict.
static Obj dictHashKey(Dict d)
Construct an object which wraps a dict and is suitable to use for a hash key in a Map. The key provides implementations of Obj.hash and Obj.equals based on the the name/value pairs in the dict. Hash keys do not support Dicts which contain anything but scalar values (nested lists, dicts, and grids are silently ignored for hash/equality purposes).
static Dict dictMap(Dict d, |Obj?,Str->Obj?| f)
Apply the given map function to each name/value pair to construct a new Dict. NOTE: use Dict.map now
static Dict dictMerge(Dict a, Obj? b)
Add/set all the name/value pairs in a with those defined in b. If b defines a remove value then that name/value is removed from a. The b parameter may be any value accepted by makeDict
static Str[] dictNames(Dict d)
Get a read/write list of the dict's name keys.
static Dict dictRemove(Dict d, Str name)
Remove a name/val pair from an exisiting dict, or if the name isn't found then return original dict.
static Dict dictRemoveAll(Dict d, Str[] names)
Remove all names from the given dict. Ignore any name not defined as a tag.
static Dict dictRename(Dict d, Str oldName, Str newName)
Rename given name if its defined in the dict, otherwise return original.
static Dict dictSet(Dict? d, Str name, Obj? val)
Set a name/val pair in an existing dict or if dict is null then create a new dict with given name/val pair. If val is null this is semantically equivalent to dictRemove(d, name).
static Str? dictToDis(Dict dict, Str? def)
Given a dict, attempt to find the best display string:
distagdisMacrotag returns macro using dict as scopedisKeymaps to qname locale keynametagtagtagidtag- default
static Dict dictToHaystack(Dict dict, Dict? opts)
Coerce dict to Haystack types, see toHaystack.
static Str:Obj? dictToMap(Dict? d)
Convert a Dict to a read/write map. This method is expensive, when possible you should instead use Dict.each.
static Obj[] dictVals(Dict d)
Get all the non-null values mapped by a dictionary.
static Str[] dictsNames(Dict?[] dicts)
Given a list of dictionaries, find all the common names used. Return the names in standard sorted order. Any null dicts are skipped.
static Void discretePeriods(Str str, |Int,Int| f)
Iterate a discrete period string formatted in base64. Call the iterator function for each period where time is offset in minutes from base timestamp and dur is duration of period in minutes (assuming a minutely interval). This method may also be used discreteEnumPeriods() in which case the dur parameter will be the enum ordinal.
static Str escapeTagName(Str n)
Escape tag name into "__{utf-8 hex}" format
static Grid gridFlatten(Grid[] grids)
Flatten a list of grids into a single grid. Each grid's rows are appended to a single grid in the order passed. The resulting grid columns will be the intersection of all the individual grid columns. Grid meta and column merged together.
static Bool isEscapedTagName(Str n)
Return if tag name starts with "__" as escaped name
static Bool isKindName(Str n)
Return if the given string is a legal kind name:
- first char must be ASCII upper case letter:
a-z - rest of chars must be ASCII letter or digit:
a-z,A-Z,0-9, or_
static Bool isTagName(Str n)
Return if the given string is a legal tag name:
- first char must be ASCII lower case letter or underbar:
a-zor_ - rest of chars must be ASCII letter,digit, or underbar:
a-z,A-Z,0-9, or_ - if first char is underbar, then it must have a at least one additional alpha-num character
- or if first two chars are underbar, then must be followed by a hexdecimal characters for a UTF-8 encoded string
static Str macro(Str pattern, Dict scope)
Process macro pattern with given scope of variable name/value pairs. The pattern is a Unicode string with embedded expressions:
$tag: resolve tag name from scope, variable name ends with first non-tag character, see Etc.isTagName${tag}: resolve tag name from scope$<pod::key>: localization key
Any variables which cannot be resolved in the scope are returned as-is (such $name) in the result string.
If a tag resolves to Ref, then we use Ref.dis for string.
static Str[] macroVars(Str pattern)
Return the list of variable tag names used in the given macro pattern. This includes "$tag" and "${tag}" variables, but does not include "$<pod::key>" localization keys.
static Dict makeDict(Obj? val)
Make a Dict instance where val is one of the following:
- Dict: return
val - null: return dict0
- Str:Obj?: wrap map as Dict
- Str[]: dictionary of key/Marker value pairs
@Deprecated { msg="Use dict1" }
static Dict makeDict1(Str n, Obj? v)
Make a Dict with one name/value pair. Backward compatibility only for nullable values, use dict1 now.
@Deprecated { msg="Use dict2" }
static Dict makeDict2(Str n0, Obj? v0, Str n1, Obj? v1)
Make a Dict with two name/value pairs. Backward compatibility only for nullable values, use dict2 now.
@Deprecated { msg="Use dict3" }
static Dict makeDict3(Str n0, Obj? v0, Str n1, Obj? v1, Str n2, Obj? v2)
Make a Dict with three name/value pairs. Backward compatibility only for nullable values, use dict3 now.
@Deprecated { msg="Use dict4" }
static Dict makeDict4(Str n0, Obj? v0, Str n1, Obj? v1, Str n2, Obj? v2, Str n3, Obj? v3)
Make a Dict with four name/value pairs. Backward compatibility only for nullable values, use dict4 now.
@Deprecated { msg="Use dict5" }
static Dict makeDict5(Str n0, Obj? v0, Str n1, Obj? v1, Str n2, Obj? v2, Str n3, Obj? v3, Str n4, Obj? v4)
Make a Dict with five name/value pairs. Backward compatibility only for nullable values, use dict5 now.
@Deprecated { msg="Use dict6" }
static Dict makeDict6(Str n0, Obj? v0, Str n1, Obj? v1, Str n2, Obj? v2, Str n3, Obj? v3, Str n4, Obj? v4, Str n5, Obj? v5)
Make a Dict with six name/value pairs. Backward compatibility only for nullable values, use dict6 now.
static Grid makeDictGrid(Obj? meta, Dict row)
Construct a grid for a Dict row. The meta parameter can be any makeDict value.
static Dict[] makeDicts(Obj?[] maps)
Make a list of Dict instances using makeDict.
static Grid makeDictsGrid(Obj? meta, Dict?[] rows)
Construct a grid for a list of Dict rows. The meta parameter can be any makeDict value. Any null dicts result in an empty row of all nulls. If no non-null rows, then return makeEmptyGrid.
static Grid makeEmptyGrid(Obj? meta)
Construct an empty grid with just the given grid level meta-data. The meta parameter can be any makeDict value.
static Grid makeErrGrid(Err e, Obj? meta)
Construct a grid for an error response.
static Grid makeListGrid(Obj? meta, Str colName, Obj? colMeta, Obj?[] rows)
Construct a grid with one column for a list. The meta and colMeta parameters can be any makeDict value.
static Grid makeListsGrid(Obj? meta, Str[] colNames, Obj?[]? colMetas, Obj?[][] rows)
Construct a grid for a list of rows, where each row is a list of cells. The meta and colMetas parameters can be any makeDict value.
static Grid makeMapGrid(Obj? meta, Str:Obj? row)
Convenience for makeDictGrid
static Grid makeMapsGrid(Obj? meta, Str:Obj?[] rows)
Convenience for makeDictsGrid
static Str relDis(Str parent, Str child)
Get a relative display name. If the child display name starts with the parent, then we can strip that as the common suffix.
static Str tagToLocale(Str name)
Get the localized string for the given tag name for the current locale. See hx.doc.skyspark::Localization#tags.
static DateSpan toDateSpan(Obj? val, HaystackContext? cx)
Coerce an object to a DateSpan:
Func: function which evaluates to date range (must be run in a context)DateSpan: return itselfDate: one day rangeSpan: return Span.toDateSpanStr: evaluates to DateSpan.fromStrDate..Date: starting and ending date (inclusive)Date..Number: starting date and num of days (day unit required)DateTime..DateTime: use starting/ending dates; if end is midnight, then use previous dateNumber: convert as year
static Dict toErrMeta(Err e)
Map an exception to its standard tags:
dis: error display stringerr: markererrTrace: Str stack dumpaxonTrace: Axon stack dump (if applicable)errType: exception type qname
static Grid toGrid(Obj? val, Dict? meta)
Coerce a value to a Grid:
- if grid just return it
- if row in grid of size, return row.grid
- if scalar return 1x1 grid
- if dict return grid where dict is only
- if list of dict return grid where each dict is row
- if list of non-dicts, return one col grid with rows for each item
- if non-zinc type return grid with cols val, type
static Obj? toHaystack(Obj? val, Dict? opts)
Coerce a value to one of the Haystack types. Recursively coerce dicts and lists. This method does not support grids. Options:
- checked: marker to throw err for non-haystack values; coerce otherwise
static Ref toId(Obj? val)
Coerce a value to a Ref identifier:
- Ref returns itself
- Row or Dict, return
idtag - Grid return first row id
static Ref[] toIds(Obj? val)
Coerce a value to a list of Ref identifiers:
- Ref returns itself as list of one
- Ref[] returns itself
- Dict return
idtag - Dict[] return
idtags - Grid return
idcolumn
static Dict toRec(Obj? val, HaystackContext? cx)
Coerce a value to a record Dict:
- Row or Dict returns itself
- Grid returns first row (must have at least one row)
- List returns first item (must have at least one item which is Ref or Dict)
- Ref will make a call to read database (must be run in a context)
static Dict[] toRecs(Obj? val, HaystackContext? cx)
Coerce a value to a list of record Dicts:
- null return empty list
- Ref or Ref[] will read database (must be run in a context)
- Row or Row[] returns itself
- Dict or Dict[] returns itself
- Grid is mapped to list of rows
static Span toSpan(Obj? val, TimeZone? tz, HaystackContext? cx)
Coerce an object to a Span with optional timezone:
Span: return itselfSpan+tz: update timezone using same dates only if aligned to midnightStr: return Span.fromStr using current timezoneStr+tz: return Span.fromStr using given timezoneDateTime..DateTime: range of two DateTimesDate..DateTime: start day for date until the end timestampDateTime..Date: start timestamp to end of day for end dateDateTime: span of a single timestampDateSpan: anything accepted by toDateSpan in current timezoneDateSpan+tz: anything accepted by toDateSpan using given timezone
static Str toTagName(Str n)
Take an arbitrary string and convert into a safe tag name. Do not assume any specific conversion algorithm as it might change in the future. The empty string is not supported.
static Str unescapeTagName(Str n)
Unescape tag name from "__{utf-8 hex}" format
static Str valToDis(Obj? val, Dict? meta, Bool clip)
Convert arbitary value to dispaly