UIZE JavaScript Framework

MODULES Uize.Data.Util

1. Introduction

The Uize.Data.Util module provides miscellaneous utility methods for working with data objects.

DEVELOPERS: Chris van Rensburg

1.1. Examples

There are no dedicated showcase example pages for the Uize.Data.Util module.

SEARCH FOR EXAMPLES

Use the link below to search for example pages on the UIZE Web site that reference the Uize.Data.Util module...

SEARCH

1.2. Implementation Info

The Uize.Data.Util module defines the Uize.Data.Util package under the Uize.Data namespace.

1.2.1. Features Introduced in This Module

The features listed in this section have been introduced in this module.

STATIC METHODS

Uize.Data.Util.filter | Uize.Data.Util.findRecords | Uize.Data.Util.getColumn | Uize.Data.Util.sortKeys

STATIC PROPERTIES

Uize.Data.Util.moduleName | Uize.Data.Util.pathToResources

1.2.2. Features Overridden in This Module

No features have been overridden in this module.

1.2.3. Features Inherited From Other Modules

This module has no inherited features.

1.2.4. Modules Directly Under This Namespace

There are no modules directly under this namespace.

1.2.5. Unit Tests

The Uize.Data.Util module is unit tested by the Uize.Test.Uize.Data.Util test module.

2. Static Methods

2.1. Uize.Data.Util.filter

Returns an object with only the properties of the source object that are specified by an array of property names.

SYNTAX

filteredOBJ = Uize.Data.Util.filter (sourceOBJ,propertyNamesARRAY);

This method can be useful when receiving an info package of which only a subset needs to be stored or passed on to a subsequent process.

EXAMPLE

var someNodeStyle = {
  color:'#fff',
  left:'10px',
  top:'-50px',
  position:'absolute',
  display:'none',
  width:'200px',
  height:'300px',
  overflow:'hidden'
};
Uize.Dom.Basics.setStyle (
  'someOtherNode',Uize.Data.Util.filter (someNodeStyle,['left','top','width','height'])
);

In this example, a node style object that is being used for some node is being filtered for just its dimensions and positioning properties, to then be applied to some other node.

Without this method, the setStyle method call would look like...

Uize.Dom.Basics.setStyle (
  'someOtherNode',
  {
    left:someNodeStyle.left,
    top:someNodeStyle.top,
    width:someNodeStyle.width,
    height:someNodeStyle.height
  }
);

IMPLEMENTATION INFO

this feature was introduced in this module

2.2. Uize.Data.Util.findRecords

Returns an array of records from the specified record set that match the specified criteria.

SYNTAX

matchingRecordsARRAY = Uize.Data.Util.findRecords (recordsARRAY,matchOBJ);

EXAMPLE

var
  employees = [
    {firstName:'John',lastName:'Wilkey',department:'engineering'},
    {firstName:'Marie',lastName:'Stevenson',department:'finance'},
    {firstName:'Craig',lastName:'Pollack',department:'finance'},
    {firstName:'Nick',lastName:'Arendsen',department:'engineering'},
    {firstName:'Mark',lastName:'Strathley',department:'engineering'}
  ],
  financeEmployees = Uize.Data.Util.findRecords (employees,{department:'finance'})
;

In the above example, the variable financeEmployees would be an array with the value...

[
  {firstName:'Marie',lastName:'Stevenson',department:'finance'},
  {firstName:'Craig',lastName:'Pollack',department:'finance'}
]

If the records in your record set are arrays, rather than objects, then you can specify a match object where the keys are numerical indexes representing the array element index / column index, as in...

EXAMPLE

var
  employees = [
    ['John','Wilkey','engineering'],
    ['Marie','Stevenson','finance'],
    ['Craig','Pollack','finance'],
    ['Nick','Arendsen','engineering'],
    ['Mark','Strathley','engineering']
  ],
  financeEmployees = Uize.Data.Util.findRecords (employees,{2:'finance'})
;

In the above example, the financeEmployees variable would have the same value as in the previous example.

NOTES

see also the Uize.findRecord, Uize.findRecordNo, and Uize.recordMatches static methods of the Uize.Class base class

IMPLEMENTATION INFO

this feature was introduced in this module

2.3. Uize.Data.Util.getColumn

Returns an array of the values for the specified column of the specified record set.

SYNTAX

columnValuesARRAY = Uize.Data.Util.getColumn (rowsARRAY,columnNameSTR);

EXAMPLE

var
  peopleNames = [
    {first:'John',last:'Wilkey'},
    {first:'Marie',last:'Stevenson'},
    {first:'Craig',last:'Pollack'}
  ],
  firstNames = Uize.Data.Util.getColumn (peopleNames,'first'),
  lastNames = Uize.Data.Util.getColumn (peopleNames,'last')
;

In the above example, the variable firstNames would be an array with the value ['John','Marie','Craig'] and the variable lastNames would be an array with the value ['Wilkey','Stevenson','Pollack'].

The records / rows in the record set do not need to be objects - they can also be arrays.

EXAMPLE

var
  peopleNames = [
    ['John','Wilkey'],
    ['Marie','Stevenson'],
    ['Craig','Pollack']
  ],
  firstNames = Uize.Data.Util.getColumn (peopleNames,0),
  lastNames = Uize.Data.Util.getColumn (peopleNames,1)
;

In the above example, the firstNames and lastNames variables would have the same values as in the previous example.

VARIATION

columnValuesARRAY = Uize.Data.Util.getColumn (rowsARRAY,columnNameSTR,onlyUniquesBOOL);

When the value true is specified for the optional onlyUniquesBOOL parameter, then this method will only return the unique values for the specified column.

EXAMPLE

var
  employees = [
    {firstName:'John',lastName:'Wilkey',department:'engineering'},
    {firstName:'Marie',lastName:'Stevenson',department:'finance'},
    {firstName:'Craig',lastName:'Pollack',department:'finance'},
    {firstName:'Nick',lastName:'Arendsen',department:'engineering'},
    {firstName:'Mark',lastName:'Strathley',department:'engineering'}
  ],
  departments = Uize.Data.Util.getColumn (employees,'department',true)
;

In the above example, the variable departments would be an array with the value ['engineering','finance'].

IMPLEMENTATION INFO

this feature was introduced in this module

2.4. Uize.Data.Util.sortKeys

Returns an object that is a copy of the specified source object, but with keys that are sorted.

DIFFERENT USAGES

Create a Sorted Keys Copy of an Object, Using the Default Comparator Function

sortedKeysOBJ = Uize.Data.Util.sortKeys (sourceOBJ);

Create a Sorted Keys Copy of an Object, Specifying a Custom Comparator Function

sortedKeysOBJ = Uize.Data.Util.sortKeys (sourceOBJ,comparatorFUNC);

2.4.1. Create a Sorted Keys Copy of an Object, Using the Default Comparator Function

In the simplest use case, a copy of a source object but with sorted keys can be created by specifying the source object as the single argument.

SYNTAX

sortedKeysOBJ = Uize.Data.Util.sortKeys (sourceOBJ);

With this usage, the default comparator function will be used for sorting the keys. Consider the following example...

EXAMPLE

Uize.Data.Util.sortKeys ({
  hello:'world',
  foo:'bar',
  baz:'qux'
});

With the above example, the following result would be produced...

RESULT

{
  baz:'qux',
  foo:'bar',
  hello:'world'
}

2.4.2. Create a Sorted Keys Copy of an Object, Specifying a Custom Comparator Function

When the default comparator function is not suitable, a custom comparator function can be specified using the optional second argument.

SYNTAX

sortedKeysOBJ = Uize.Data.Util.sortKeys (sourceOBJ,comparatorFUNC);

The comparator function that is specified should follow the rules for a comparator function supplied to JavaScript's built-in sort instance method of the Array object. Consider the following example...

EXAMPLE

Uize.Data.Util.sortKeys (
  {
    file25:'foo.txt',
    file15:'bar.txt',
    file2:'baz.txt',
    file1:'qux.txt'
  },
  function (a,b) {return a.match (/\d+/) [0] - b.match (/\d+/) [0]}
);

In the above example, the keys for the properties of the specified source object contain numerical suffixes. If we relied simply on the default comparator function, we would get a resulting object with ASCIIbetically sorted keys in the order "file1", "file15", "file2", "file25".

Since we want the keys to be sorted by the value of the numerical suffix, we specify a custom comparator function that performs a regular expression match on the keys in order to obtain the numerical portion and then compare those in an expression that produces a numerical result by coercing the numerical portions to numbers.

Using the custom comparator, this following result would be produced...

RESULT

{
  file1:'qux.txt',
  file2:'baz.txt',
  file15:'bar.txt',
  file25:'foo.txt'
}

IMPLEMENTATION INFO

this feature was introduced in this module

3. Static Properties

3.1. Uize.Data.Util.moduleName

IMPLEMENTATION INFO

this feature was introduced in this module

3.2. Uize.Data.Util.pathToResources

IMPLEMENTATION INFO

this feature was introduced in this module