jwidget

Core jWidget utilities.

Consumption

import * as jwidget from "jwidget";

Binding#

Binding: enum

jWidget binding modes. All options have shorthands, i.e. Binding.UPDATE can be imported separately as {UPDATE}.

UPDATE = 1

Bind element attribute to property.

    // Bind element value to property
    bindVal(el, property, UPDATE);

Always used as default binding. Hence, the next code is equivalent:

    bindVal(el, property);
WATCH = 2

Bind property to element attribute.

    // Bind property to element value
    bindVal(el, property, WATCH);

Always supplied with a no-argument method, which creates the property automatically.

    // Watch element value
    const property = bindVal(el);
TWOWAY = 3

Bind element attribute and property to each other.

UPDATE-binding is applied first, WATCH is applied last.

    // Assign element value to property and setup two-way binding
    bindVal(el, property, TWOWAY);

cmp#

(x: any, y: any): number

x
First value to compare.
y
Second value to compare.
returns
One of {-1, 0, 1} as comparison result.

Universal and sophisticated comparer for array sorting. The same as smartCmp, but without third argument. It comes in handy when a function takes a callback accepting 3 or more arguments - in this case, arguments of smartCmp won't match the callback definition:

const array = new BindableArray(["q", "w", "e"]);
array.sortComparing(cmp); // ok
array.sortComparing(smartCmp); // compilation error: callback arguments don't match

smartCmp#

(x: any, y: any, config?: CmpConfig): number

x
First value to compare.
y
Second value to compare.
config
Optional configuration.
returns
One of {-1, 0, 1} as comparison result.

Universal and sophisticated comparer for array sorting. Broadly speaking, it:

  • Returns 1, if x > y
  • Returns -1, if x < y
  • Returns 0, if x == y

In reality, it supports the next features:

  • Comparing of booleans, numbers, strings, subarrays, null, undefined.
  • Determined linear order, even for mixed arrays.
  • Optionally, case insensitive comparison of strings.
  • Optionally, comparison of digit sequences in strings as numbers.

Values of different types are compared as follows: undefined < null < array < boolean < number < string.

Example

Sort by color descending first, and by status ascending last. Both parameters are optional.

rows.sort((x, y) => {
    return smartCmp(x.color == null, y.color == null) ||
          -smartCmp(x.color, y.color) ||
           smartCmp(x.status == null, y.status == null) ||
           smartCmp(x.status, y.status);
});

CmpConfig#

Optional configuration of smartCmp function.
caseInsensitive#

readonly caseInsensitive?: boolean

Ignore case when comparing strings.

Example
expect(smartCmp("aB", "Ab")).equal(1);
expect(smartCmp("aB", "Ab", {caseInsensitive: true})).equal(0);
compareNumbersInStrings#

readonly compareNumbersInStrings?: boolean

Compare digit sequences as numbers when comparing strings.

Example
expect(smartCmp("ab2", "ab10")).equal(1);
expect(smartCmp("ab2", "ab10", {compareNumbersInStrings: true})).equal(-1);

destroy#

(obj: any): void

obj
Object to destroy.
returns
undefined.

Calls destroy method of obj if available. Can be used in configuration of any mapper.

const views = startMappingArray(models, model => new View(model), {destroy});