View source

class thx.Objects

Available on all platforms

Helper methods for generic objects.

Class Fields

static function assign(to:{}, from:{}, ?replacef:String -> Dynamic -> Dynamic -> Dynamic):{}

Copies the values from the fields of from to to. If to already contains those fields, then it replaces those values with the return value of the function replacef.

If not set, replacef always returns the value from the from object.

static function clone<T>(src:T, cloneInstances:Bool = false):T

Clone the current object by creating a new object and using copyTo to clone each field.

static function combine(first:{}, second:{}):{}

Copies the values from the fields of first and second to a new object. If first and second contain fields with the same name, the returned object will use the fields from second. Both objects passed to this function will be unmodified.

The combine operation is not recursive and does a shallow merge of the two objects.

static function compare(a:{}, b:{}):Int

Compares two objects assuming that the object with less fields will come first.

If both objects have the same number of fields, each field value is compared using thx.Dynamics.compare.

static function copyTo(src:{}, dst:{}, cloneInstances:Bool = false):{}

copyTo copies the fields from src to dst using Reflect.setField() and Dynamics.clone(). Anonymous objects are entered into and copied recursively.

static inline function exists(o:{}, name:String):Bool

exists returns true if o contains a field named name.

static inline function fields(o:{}):Array<String>

fields returns an array of string containing the field names of the argument object.

static function getPath(o:{}, path:String):Dynamic

Gets a value from an object by a string path. The path can contain object keys and array indices separated by ".". Returns null for a path that does not exist.

E.g. { key1: { key2: [1, 2, 3] } }.getPath("key1.key2.2") -> returns 3

static function getPathOr(o:{}, path:String, alt:Dynamic):Dynamic

Gets a value from an object by a string path. The path can contain object keys and array indices separated by ".". Returns alt for a path that does not exist.

  E.g. { key1: { key2: [1, 2, 3] } }.getPath("key1.key2.2") -> returns 3
  E.g. { key1: { key2: [1, 2, 3] } }.getPath("key1.key2.5", 7) -> returns 7

static function hasPath(o:{}, path:String):Bool

Determines whether an object has fields represented by a string path. The path can contain object keys and array indices separated by ".".

E.g. { key1: { key2: [1, 2, 3] } }.hasPath("key1.key2.2") -> returns true

static function hasPathValue(o:{}, path:String):Bool

Like hasPath, but will return false for null values, even if the key exists.

E.g. { key1 : { key2: null } }.hasPathValue("key1.key2") -> returns false

static inline function isEmpty(o:{}):Bool

isEmpty returns true if the object doesn't have any field.

static function merge(first:{}, second:{}):Dynamic

Merges first and second using the combine strategy to return a merged object. The returned object is typed as an object containing all of the fields from both first and second.

static function removePath(o:{}, path:String):{}

Delete an object's property, given a string path to that property.

E.g. { foo : 'bar' }.removePath('foo') -> returns {}

static function setPath<T>(o:{}, path:String, val:T):{}

Sets a value in an object by a string path. The path can contain object keys and array indices separated by ".". Returns the original object, for optional chaining of other object methods.

Inner objects and arrays will be created as needed when traversing the path.

E.g. { key1: { key2: [1, 2, 3] } }.setPath("key1.key2.2", 4) -> returns { key1: { key2: [ 1, 2, 4 ] } }

static inline function size(o:{}):Int

size returns how many fields are present in the object.

static function string(o:{}):String

Returns a string representation of the object containing each field and value.

The function is recursive so it might generate infinite loops if used with circular references.

static function toMap(o:{}):Map<String, Dynamic>

objectToMap transforms an anonymous object into an instance of Map<String, Dynamic>.

static function tuples(o:{}):Array<Tuple2<String, Dynamic>>

Converts an object into an Array> where the left value (_0) of the tuple is the field name and the right value (_1) is the field value.

static inline function values(o:{}):Array<Dynamic>

values returns an array of dynamic values containing the values of each field in the argument object.

Toggle inherited fields