Lodash - language
About
These functions extend the 4D language-related commands by making it more convenient when working with values and types.
API
.bind()
.bind(func : Callable { ; thisObj : any { ; …partialParams : any }}) : cs.js.Binder
Returns an object whose .call()
, .apply()
and .f()
functions will call func.apply()
. If thisObj is null, the first parameter passed to .call()
or .apply()
will be used as the This
object, otherwise thisObj will be used as the This
object.
If partialParams are passed, they are prepended to whatever parameters are passed to the bound function.
Examples
// Bind `This` to $person
$person:=New object("first"; "Laurent"; "last"; "Ribardière")
$greeter:=_.bind(Formula($1+" "+This.first+" "+This.last+$2); $person; "Hello")
$greeting:=$greeter.call(Null; "!")
// => "Hello Laurent Ribardière!"
// `This` must be passed in
$person:=New object("first"; "Laurent"; "last"; "Ribardière")
$greeter:=_.bind(Formula($1+" "+This.first+" "+This.last+$2); Null; "Hello")
$greeting:=$greeter.call($person; "!")
// => "Hello Laurent Ribardière!"
// No `This`
$greeter:=_.bind(Formula($1+" "+$2+$3); Null; "Hello")
$greeting:=$func.call(Null; "Laurent"; "!")
// => "Hello Laurent!"
.bindRight()
.bindRight(func : Callable { ; thisObj : any { ; …partialParams : any }}) : cs.js.Binder
This is the same as .bind()
, but if partialParams are passed, they are appended rather than prepended to whatever parameters are passed to the bound function.
Examples
$person:=New object("first"; "Laurent"; "last"; "Ribardière")
$greeter:=_.bindRight(Formula($1+" "+This.first+" "+This.last+$2); $person; "!")
$greeting:=$greeter.call(Null; "Hello") // => "Hello Laurent Ribardière!"
.bool()
.bool(value : any { ; expect : Boolean }) : Boolean
Sometimes you want to know specifically if a value is the boolean value true or false. This function allows you to test for that in one line.
If expect is not passed, it defaults to true. If value is a boolean, it is tested against expect and the result is returned. Otherwise false is returned.
NOTE
Do not confuse _.bool()
with 4D’s Bool()
command!
.equal()
.equal(value : any; compare : any { ; strict : Boolean }) : Boolean
Compares value with compare and returns whether they have the same value.
The beauty of this function is you can throw anything at it, even types that are not compatible, and it will not generate an error. In addition, you can use this one function to compare values of any type, including ones that cannot be compared with =
.
Comparisons are performed as follows:
If value and compare are not type-compatible, false is returned.
Strings: If strict is true, case/diacritical comparison is done, otherwise a non-case/diacritical
=
comparison is done.Objects: They must be of the same class, have the same keys, and all of the values must recursively pass
_.equal()
. If strict is true, string comparisons are case/diacritical-sensitive.Collections:
Collection.equal
is used. If strict is true,ck diacritical
is passed.Blobs: Size and
SHA256 digest
must match.Pictures:
Equal pictures
is used.Other types: They must be type-compatible and pass 4D's
=
test.
NOTE
Times are converted to numbers and thus may by compared with numbers.
.falsey()
.falsey(value : any) : Boolean
Returns the opposite of .truthy()
.
.getCallStack()
.getCallStack() : Collection
Gets the call stack at any point of execution, even outside of an error handler. The top of the stack will point to the caller of this function. The items in the stack are in the form returned by Get call chain
.
.instanceof()
.instanceof(value : any; class : 4D.Class) : Boolean
Returns whether value is an instance of class. This differs from OB Instance of
in that it will not generate an error if value is not an object. This allows you to test a value of any type on one line.
TIP
.instanceOf()
is an alias for this function.
.isArrayPointer()
.isArrayPointer(pointer : Pointer) : Boolean
Returns true if pointer points to an array.
.isCallable()
.isCallable(value : any) : Boolean
Returns true if value is an object which has both .call()
and .apply()
properties that are functions.
.isCyclic()
.isCyclic(objectOrCollection : Object | Collection) : Boolean
Returns true if objectOrCollection contains a cyclic reference, otherwise false.
.isNumber()
.isNumber(value : any) : Boolean
Returns true if the value type of value is any of 4D’s numeric types.
.isNumericType()
.isNumericType(type : Integer) : Boolean
Returns true if the type is any of 4D’s numeric types.
.max()
.max(value : any { ; …valueN : any }) : any
.max(values : Collection) : any
Returns the maximum of the passed values.
NOTE
All of the values must be of a type that can be compared with the >
operator.
.min()
.min(value : any { ; …valueN : any }) : any
.min(values : Collection) : any
Returns the minimum of the passed values.
NOTE
All of the values must be of a type that can be compared with the <
operator.
.respondsTo()
.respondsTo(value : Object ; name : Text }) : Boolean
Returns true if value has the property name which is a function.
NOTE
You cannot test computed properties (get/set) with this function.
.sequence()
.sequence(expression : any { ; ….expressionN : any}) : any
Returns the value of the last expression passed to it. expression need not return a value; it can be a command, method call, or class function call with side effects.
This is especially useful when used with formulas, as it gives you the ability to effectively create a multiline formula.
If expression is a Callable, expression.call()
is called.
Example
$it.should(\
"get a dotted key path value"; \
Formula(This.expect(_.sequence(_.loadConfig(); _.getConfig("foo")))\
.to.strict.equal("bar")))
.strictEqual()
.strictEqual(value : any; compare : any) : Boolean
This is a convenience function that calls _.equal(value; compare; True)
.
.truthy()
.truthy(value : any) : Boolean
This is an implementation of the JavaScript truthy test — which is IMHO usually much more useful than 4D’s Bool
. The results are as follows:
Type | Result |
---|---|
Text | false if empty, true otherwise |
Object | false if null, true otherwise |
Collection | false if null, true otherwise |
Boolean | value |
Null | false |
Undefined | false |
Number | false if 0, true otherwise |
Anything else | false |
.typeName()
.typeName(type : any) : Text
Returns a text description of the given non-array type.
type | Result |
---|---|
Numeric 4D type constant (e.g. Is longint ) | See the list of type names below |
Instance of a class | The instance’s class name |
Class | The class name |
Formula | "function" |
Null | "null" |
Unknown type constant | "" |
Type constant names
If a numeric type constant is passed, the value returned is the lowercased English constant name without the leading "Is ", except for the cases listed below. For example, if type is Is BLOB
, "blob" is returned.
type | Result |
---|---|
Is alpha field | "string" |
Is integer 64 bits | "integer 64" |
Is string var | "string" |
Is variant | "any" |
.valueIs()
.valueIs(value : any; expectedType : any) : Boolean
Returns whether value’s type is expectedType. expectedType may be a type name or a class. Valid type names are:
blob
boolean
callable (.isCallable()
returns true)
class
collection
date
formula (same as function)
function (same as formula)
null
number
object
picture
pointer
string
undefined