NEW MODULE: Uize.Data.Combinations

14 views
Skip to first unread message

cvr

unread,
Mar 14, 2012, 2:59:28 PM3/14/12
to UIZE JavaScript Framework Group
The new Uize.Data.Combinations module provides methods for generating
object or array combinations from a combination specifier, with
support for an optional combination transformer and combination
matcher.

The Methods

The Uize.Data.Combinations module provides the following static
methods to deal with generated combinations...

- Uize.Data.Combinations.forEach - lets you iterate through generated
combinations, executing the specified iteration handler function for
each combination
- Uize.Data.Combinations.generate - produces an array containing the
generated combinations

A Real World Example

In this example, we are using the Uize.Data.Combinations.generate
method to generate an array of hex RGB color strings for the full
palette of Web safe colors.

If we were to approach this problem the traditional way, we would
write our code using three nested for loops - one for each of the
three RGB color channels. This approach would produce code as
follows...

THE TEDIOUS WAY

var
channelValues = ['00','33','66','99','CC','FF'],
webSafeColors = []
;
for (var redValueNo = 0; redValueNo < channelValues.length; redValueNo+
+) {
for (var greenValueNo = 0; greenValueNo < channelValues.length;
greenValueNo++) {
for (var blueValueNo = 0; blueValueNo < channelValues.length;
blueValueNo++) {
webSafeColors.push (
'#' +
channelValues [redValueNo] +
channelValues [greenValueNo] +
channelValues [blueValueNo]
);
}
}
}

If we instead use the Uize.Data.Combinations.generate method, we can
reduce our code down to something more concise and elegant, as
follows...

THE CONCISE WAY

var
channelValues = ['00','33','66','99','CC','FF'],
webSafeColors = Uize.Data.Combinations.generate (
[channelValues,channelValues,channelValues],
'"#" + value.join ("")'
)
;

In our solution, we create the variable channelValues, whose value is
an array representing all the possible values for a color channel in a
Web safe color. We then use this possible values array in a
combination specifier that provides a template for an RGB color tuple.
Then, to ensure that the array of generated values is an array of hex
formatted RGB color strings, we specify a combination transformer
expression string that takes a generated combination tuple and joins
its three elements and adds a "#" (pound character) prefix.

Comprehensively Documented and Tested

The Uize.Data.Combinations module is comprehensively documented and
has exhaustive unit tests in the Uize.Test.Uize.Data.Combinations test
module.

http://uize.com/reference/Uize.Data.Combinations.html
Reply all
Reply to author
Forward
0 new messages