Skip to main content

Home > @jsplumb/util

util package#

This package contains various utility functions used by both the Community and Toolkit editions of jsPlumb.

Classes#

ClassDescription
EventGeneratorBase class for classes that wish to support binding and firing of events.
OptimisticEventGeneratorSubclass of EventGenerator with a default implementation of shouldFireEvent, which returns true always.

Functions#

FunctionDescription
add(p1, p2)Adds the x and y values of the two points and returns a new point.
addToDictionary(map, key, value, insertAtStart)Adds an item to a dictionary whose values consists of array of some type. This method is used internally by jsPlumb and is not intended as part of the public API, and will likely be removed at some point in the future when the code that depends upon it has been refactored.
addToList(map, key, value, insertAtStart)Add an item to a list that is stored inside some map. This method is used internally.
addWithFunction(list, item, hashFunction)Adds an item to a list if the given hash function determines that the item is not already in the list
clone(a)Makes a copy of the given object.
each(obj, fn)Iterates through the given obj and applies the given function. if obj is not ArrayLike then the function is executed directly on obj.
encloses(r1, r2, allowSharedEdges)Calculates whether or not r2 is completely enclosed by r1.
extend(o1, o2, keys)Equivalent of Object.assign, which IE11 does not support.
fastTrim(s)Trims whitespace from the given string.
filterList(list, value, missingIsFalse)Internal method used to filter lists, supporting wildcards.
filterNull(obj)Returns a copy of the given object that has no null values. Note this only operates one level deep.
findAllWithFunction(a, predicate)Find all entries in the given array like object for which the given predicate returns true.
findWithFunction(a, f)Search each entry in the given array for an entry for which the function f returns true. This is a stand-in replacement for the findIndex method which is available on Array in modern browsers, but not IE11.
forEach(a, f)Stand-in for the forEach method which is available on modern browsers but not on IE11.
fromArray(a)A shim for the fromArray method, which is not present in IE11. This method falls back to fromArray if it is present.
getAllWithFunction(a, f)Find all entries in the given array for which the function f returns true
getFromSetWithFunction(s, f)Extract a value from the set where the given predicate returns true for that value.
getsert(map, key, valueGenerator)Get, or insert then get, a value from the map.
getWithFunction(a, f)Find the entry in the given array for which the function f returns true. This is a stand-in replacement for the find method which is available on Array in modern browsers, but not IE11.
gradient(p1, p2)Calculates the gradient of a line between the two points.
insertSorted(value, array, comparator, sortDescending)Inserts the given value into the given array at a sorted location.
intersects(r1, r2)Calculates whether or not the two rectangles intersect.
isAssignableFrom(object, cls)Returns true if the given object can be considered to be an instance of the class cls. This is done by testing the proto chain of the object and checking at each level to see if the proto is an instance of the given class.
isBoolean(s)Returns whether or not the given value is of boolean type.
isDate(o)Returns whether or not the given value is of type Date
isEmpty(o)Returns whether or not the given object - which may be ArrayLike, or an object - is empty.
isFunction(o)Returns whether or not the given value is of type Function
isNamedFunction(o)Returns whether or not the given value is of type Function and is a named Function.
isNull(s)Returns whether or not the given value is null.
isNumber(n)Returns whether or not the given value is of number type.
isObject(o)Returns whether or not the given value is of type object
isString(s)Returns whether or not the given value is of string type.
lineIntersection(l1, l2)Compute the intersection of the two lines.
lineLength(p1, p2)Calculates the length of a line between the two points.
lineRectangleIntersection(line, r)Finds all points where the given line intersects the given rectangle.
log(args)Logs a console message.
map(obj, fn)Maps some ArrayLike object. This is of course a copy of a method that typescript offers. It will likely fall out of use in the jsPlumb codebase.
merge(a, b, collations, overwrites)Merge the values from b into the values from a, resulting in c. b and a are unchanged by this method. Not every datatype can be merged - arrays can, and objects can, but primitives (strings/booleans/numbers/functions) cannot, and are overwritten in c by the value from b, if present.
normal(p1, p2)Calculates the gradient of a normal to a line between the two points.
perpendicularLineTo(fromPoint, toPoint, length)Calculates a line of length length that is perpendicular to the line from fromPoint to toPoint and passes through toPoint.
pointOnLine(fromPoint, toPoint, distance)Calculates a point on the line from fromPoint to toPoint that is distance units along the length of the line.
populate(model, values, functionPrefix, doNotExpandFunctions)Take the given model and expand out any parameters.
quadrant(p1, p2)Calculates the quadrant in which the angle between the two points lies.
remove(l, v)Remove an item from an array
removeWithFunction(a, f)Remove the entry from the array for which the function f returns true.
replace(inObj, path, value)Replace a value inside some object with another value.
rotatePoint(point, center, rotation)Rotate the given point around the given center, by the given rotation (in degrees)
setToArray(s)Convert a set into an array. This is not needed for modern browsers but for IE11 compatibility we use this in jsplumb.
snapToGrid(pos, grid, thresholdX, thresholdY)Snap the given x,y to a point on the grid defined by gridX and gridY, using the given thresholds to calculate proximity to the grid.
sortHelper(_array, _fn)Sorts an array. Seems quite unnecessary. Only used in one place in the codebase. Marking deprecated.
subtract(p1, p2)Subtracts p2 from p1, returning a new point.
suggest(list, item, insertAtHead)Add the given item to the given list if it does not exist on the list already.
theta(p1, p2)Calculates the angle between the two points.
uuid()Generate a v4 UUID.
wrap(wrappedFunction, newFunction, returnOnThisValue)Wraps one function with another, creating a placeholder for the wrapped function if it was null. This is used to wrap the various drag/drop event functions - to allow jsPlumb to be notified of important lifecycle events without imposing itself on the user's drag/drop functionality.

Interfaces#

InterfaceDescription
DictionarySimple definition of a map. This construct is being replaced across the codebase with Record<string, T> and also Map<..>
ExtentsDefinition of the extends of some set of elements: the min/max values in each axis.
GridDefinition of a grid - the width/height of each cell, and, optionally, a threshold value for each axis to use when trying to snap some coordinate to the grid.
PointXYDefines an x/y location.
RotationDefines the current rotation of some element - its rotation (in degrees) and the center point around which it is rotated.
SizeDefines the width and height of some element.

Variables#

VariableDescription
logEnabledDetermines whether or not logging is currently enabled.
TWO_PIDefinition of 2 PI

Type Aliases#

Type AliasDescription
BoundingBoxDefines the bounding box for some element - its x/y location, width and height, and optionally the computed center, but that can of course be calculated from the other values. Internally there are times when the code has this to hand so we include it here.
ConstructableDefines an object that has a constructor. Used internally to create endpoints/connectors/overlays from their names.
LineXYDefines a line from some point to another.
MergeA copy of a concept from a later version of Typescript than jsPlumb can currently use.
Omit_2A copy of a concept from a later version of Typescript than jsPlumb can currently use.
Quadrant
RectangleXYThis is an alias for BoundingBox.
RotationsA set of compound rotations - used when nesting elements/groups inside other groups.
SortFunctionDefines a function that can be used to sort an array.