Module josm/util

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.
print Prints a message to stdout (without newline).
println Prints a message to stdout (including newline).
trim Trims leading and trailing whitespace from s.

Functions

assert

Assert a condition and throw an Error if the condition isn't met.

Usage:

assert()
Does nothing
assert(cond)
Checks the condition cond. If it is false, throws an Error.
assert(cond, msg)
Checks the condition cond. If it is false, throws an Error, whose description property is set to msg.
assert(cond, msg, objs...)
Checks the condition 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.

Examples

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.

Parameters

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.

Parameters

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

Examples

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.

Parameters

Name Type Description
o any the object

Examples

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.

Parameters

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.

Parameters

Name Type Description
o any the object

Examples

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.

Parameters

Name Type Description
val anything the value to check

isArray:boolean

Replies true if val is an array.

Parameters

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.

Parameters

Name Type Description
value object the value to check

isDef:boolean

Returns true if val is defined.

Parameters

Name Type Description
val any the value to check

isFunction:boolean

Replies true, if f is a function.

Parameters

Name Type Description
f any the object

isNothing:boolean

Checks whether a value is null or undefined.

Parameters

Name Type Description
value object the value to check

isNumber:boolean

Returns true if val is a number.

Parameters

Name Type Description
val any the value to check

isSomething:boolean

Checks whether a value is neither null nor undefined.

Parameters

Name Type Description
value object the value to check

isString:boolean

Returns true if val is a string.

Parameters

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).

Parameters

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.

print

Prints a message to stdout (without newline).

Supports string templates à la java.text.MessageFormat.

Examples

util.print("Hello world! My name is {0}", myname);
 

println

Prints a message to stdout (including newline).

Supports string templates à la java.text.MessageFormat.

Examples

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.

Parameters

Name Type Description
s string the string to be trimmed