Sprites.Logic

Boolean logic over reactive values. Each combinator reads its arguments and returns a reactive value that follows them, so a condition built from several values stays live and composes with the reactive structure.

Sprites.Logic

Sprites.Logic.And Sprites.Reactive.Value

Sprites.Logic.And(...args)

True when every argument is true. With no arguments it is true. Each argument is a constant or a reactive value, so the result follows every one.

InputTypeDescription
...argsAny or Sprites.Reactive.ValueThe conditions to combine, each read with Get.

Returns a read-only Sprites.Reactive.Value, true while every argument is true.

const a = Sprites.Reactive.Value(true);
const b = Sprites.Reactive.Value(false);
const both = Sprites.Logic.And(a, b);
Sprites.Ui.Input.Checkbox(a);
Sprites.Ui.Input.Checkbox(b);
Sprites.Ui.Dom.Text(' both = ', both);
True only while both boxes are checked.

Sprites.Logic.Equals Sprites.Reactive.Value

Sprites.Logic.Equals(a, b)

True when two values are equal by ===. Either may be a constant or a reactive value, so the result follows both.

InputTypeDescription
aAny or Sprites.Reactive.ValueThe first value.
bAny or Sprites.Reactive.ValueThe second value.

Returns a read-only Sprites.Reactive.Value, true while the two values are equal.

const tool = Sprites.Reactive.Value('move');
const moving = Sprites.Logic.Equals(tool, 'move');
Sprites.Ui.Input.Dropdown(tool, () => {
  Sprites.Ui.Input.Option('move', 'Move');
  Sprites.Ui.Input.Option('draw', 'Draw');
});
Sprites.Ui.Dom.Text(' moving = ', moving);
True while the tool equals move.

Sprites.Logic.Greater Sprites.Reactive.Value

Sprites.Logic.Greater(a, b)

True when a is greater than b. Either may be a constant or a reactive value, so the result follows both.

InputTypeDescription
aNumber or Sprites.Reactive.ValueThe value on the left of the comparison.
bNumber or Sprites.Reactive.ValueThe value on the right of the comparison.

Returns a read-only Sprites.Reactive.Value, true while a is greater than b.

const score = Sprites.Reactive.Value(7);
const pass = Sprites.Logic.Greater(score, 5);
Sprites.Ui.Input.Number(score);
Sprites.Ui.Dom.Text(' pass = ', pass);
True while the score is above five.

Sprites.Logic.Less Sprites.Reactive.Value

Sprites.Logic.Less(a, b)

True when a is less than b. Either may be a constant or a reactive value, so the result follows both.

InputTypeDescription
aNumber or Sprites.Reactive.ValueThe value on the left of the comparison.
bNumber or Sprites.Reactive.ValueThe value on the right of the comparison.

Returns a read-only Sprites.Reactive.Value, true while a is less than b.

const level = Sprites.Reactive.Value(3);
const low = Sprites.Logic.Less(level, 5);
Sprites.Ui.Input.Number(level);
Sprites.Ui.Dom.Text(' low = ', low);
True while the level is below five.

Sprites.Logic.Not Sprites.Reactive.Value

Sprites.Logic.Not(a)

The reactive negation of one condition: true when the value is false.

InputTypeDescription
aAny or Sprites.Reactive.ValueThe condition to negate.

Returns a read-only Sprites.Reactive.Value, true while the value is false.

const on = Sprites.Reactive.Value(true);
const off = Sprites.Logic.Not(on);
Sprites.Ui.Input.Checkbox(on);
Sprites.Ui.Dom.Text(' off = ', off);
The mirror of the checkbox.

Sprites.Logic.Or Sprites.Reactive.Value

Sprites.Logic.Or(...args)

True when any argument is true. With no arguments it is false. Each argument is a constant or a reactive value, so the result follows every one.

InputTypeDescription
...argsAny or Sprites.Reactive.ValueThe conditions to combine, each read with Get.

Returns a read-only Sprites.Reactive.Value, true while any argument is true.

const a = Sprites.Reactive.Value(false);
const b = Sprites.Reactive.Value(false);
const either = Sprites.Logic.Or(a, b);
Sprites.Ui.Input.Checkbox(a);
Sprites.Ui.Input.Checkbox(b);
Sprites.Ui.Dom.Text(' either = ', either);
True while either box is checked.

Sprites.Logic.Select Sprites.Reactive.Value

Sprites.Logic.Select(condition, whenTrue, whenFalse)

One of two values by a condition: whenTrue while it holds, else whenFalse. The reactive form of a ternary, so a composite chooses a value with no raw if. Each argument is a constant or a reactive value, so the result follows them.

InputTypeDescription
conditionAny or Sprites.Reactive.ValueThe condition to test, read with Get.
whenTrueAny or Sprites.Reactive.ValueThe value chosen while the condition is true.
whenFalseAny or Sprites.Reactive.ValueThe value chosen while the condition is false.

Returns a read-only Sprites.Reactive.Value holding whenTrue or whenFalse as the condition changes.

const on = Sprites.Reactive.Value(true);
const label = Sprites.Logic.Select(on, 'Yes', 'No');
Sprites.Ui.Input.Checkbox(on);
Sprites.Ui.Dom.Text(' label = ', label);
The label switches between the two values with the box.