Provides a set of static utility functions.
| Name | Description |
|---|---|
| Methods | |
| assert | Assert a condition and throw an Error if the condition isn't met. |
| assertNumber | Asserts that val is a number. |
| assertSomething | Asserts that val is defined and non-null. |
| countProperties | Replies the number of properties owned by o. |
| each | Iteraties over the elements of a collection |
| hasProperties | Replies true, if o owns at least one property. |
| isArguments | Replies true if val is a list of arguments. |
| isArray | Replies true if val is an array. |
| isCollection | Is a value a collection? |
| isDef | Returns true if val is defined. |
| isFunction | Replies true, if f is a function. |
| isNothing | Checks whether a value is null or undefined. |
| isNumber | Returns true if val is a number. |
| isSomething | Checks whether a value is neither null nor undefined. |
| isString | Returns true if val is a string. |
| javaEquals | Are two java objects equal. |
| mix | Mixes the properties of a list of objects into one object. |
| Prints a message to stdout (without newline). | |
| println | Prints a message to stdout (including newline). |
| trim | Trims leading and trailing whitespace from s. |
assert
Assert a condition and throw an Error if the condition isn't met.
Usage:
assert()assert(cond)cond. If it is false, throws an Error.assert(cond, msg)cond. If it is false, throws an Error, whose description property
is set to msg.assert(cond, msg, objs...)cond. If it is false, throws an Error, whose description property
is set to the formatted message msg. Internally uses java.text.MessageFormat to format the message.var util = require("josm/util");
// throws an Error
util.assert(false);
// throws an Error e, with e.description == "My message"
util.assert(false, "My message");
// throws an Error e, with e.description == "My message: test"
util.assert(false, "My message: {0}", "test"); assertNumber
Asserts that val is a number.
| Name | Type | Description |
|---|---|---|
| val | Anything | the value to check |
| msg | String | (optional) message if the assertion fails |
| values | (optional) additional values used in msg placeholders |
assertSomething
Asserts that val is defined and non-null.
| Name | Type | Description |
|---|---|---|
| val | any | the value to check |
| msg | string | (optional) message if the assertion fails |
| values | (optional) additional values used in msg placeholders |
var util = require("josm/util");
util.assertSomething(null); // -> throws an exception
util.assertSomething(void 0); // -> throws an exception
util.assertSomting("test"); // -> OK
util.assertSomething(5); // -> OK countProperties:number
Replies the number of properties owned by o.
| Name | Type | Description |
|---|---|---|
| o | any | the object |
var util = require("josm/util");
var o = {p1: "v1", p2: "v2"};
var c = util.countProperties(o); // -> 2
o = {};
c = util.countProperties(o); // -> 0
o = undefined;
c = util.countProperties(o); // -> undefined each
Iterates over the elements in collection and invokes
delegate() on each element.
| Name | Type | Description |
|---|---|---|
| collection | array | the collection of elements |
| delegate | function | the function to call on each elemennt |
hasProperties:boolean
Replies true, if o owns at least one property.
| Name | Type | Description |
|---|---|---|
| o | any | the object |
var util = require("josm/util");
var o = {p1: "v1", p2: "v2"};
var c = util.hasProperties(o); // -> true
o = {};
c = util.hasProperties(o); // -> false
o = undefined;
c = util.hasProperties(o); // -> false isArguments:boolean
Replies true if val is a list of arguments.
| Name | Type | Description |
|---|---|---|
| val | anything | the value to check |
isArray:boolean
Replies true if val is an array.
| Name | Type | Description |
|---|---|---|
| val | anything | the value to check |
isCollection
Replies true, if a value is an array, an arguments list or a Java collection.
| Name | Type | Description |
|---|---|---|
| value | object | the value to check |
isDef:boolean
Returns true if val is defined.
| Name | Type | Description |
|---|---|---|
| val | any | the value to check |
isFunction:boolean
Replies true, if f is a function.
| Name | Type | Description |
|---|---|---|
| f | any | the object |
isNothing:boolean
Checks whether a value is null or undefined.
| Name | Type | Description |
|---|---|---|
| value | object | the value to check |
isNumber:boolean
Returns true if val is a number.
| Name | Type | Description |
|---|---|---|
| val | any | the value to check |
isSomething:boolean
Checks whether a value is neither null nor undefined.
| Name | Type | Description |
|---|---|---|
| value | object | the value to check |
isString:boolean
Returns true if val is a string.
| Name | Type | Description |
|---|---|---|
| val | any | the value to check |
javaEquals:boolean
Checks whether two java objects are either both null or equal by calling o1.equals(o2).
| Name | Type | Description |
|---|---|---|
| o1 | object | a java object or null |
| o2 | object | a java object or null |
mix:object
Mixes the properties of a list of objects into one object.
Prints a message to stdout (without newline).
Supports string templates à la java.text.MessageFormat.
util.print("Hello world! My name is {0}", myname);
println
Prints a message to stdout (including newline).
Supports string templates à la java.text.MessageFormat.
util.println("Hello world! My name is {0}", myname);trim:string
Trims leading and trailing whitespace from s.
Replies s, if s is null or undefined. Any other value is converted to a string, then leading and trailing white space is removed.
| Name | Type | Description |
|---|---|---|
| s | string | the string to be trimmed |