Skip to content

Shortcut Syntax

The :xxx shortcut automatically prepends a configurable prefix to format tokens.

Concept

:unit expands to _shortcut_prefix:unit.

Default prefix is !u, so :$ becomes !u:$.

Configuration

javascript
import { set_config } from "a-calc";

// Default: '!u'
set_config({ _shortcut_prefix: "!u" });

// Switch to thousands preset shortcut
set_config({ _shortcut_prefix: "!t" });

// Switch to compact format shortcut
set_config({ _shortcut_prefix: "!c" });

Valid values: '!u', '!t', '!c', '!n', '!e', '!i', '!g', '!ua', '!ub', '!um', '!uh'

Basic Usage

javascript
import { calc, set_config } from "a-calc";

set_config({
  _unit_convert_out: { $: { "¢": 0.01 } },
  _unit_default_out: "$",
  _shortcut_prefix: "!u", // default
});

// :$ is equivalent to !u:$
calc("10000 | =2 :$"); // '100.00$'
calc("10000 | =2 !u:$"); // '100.00$' — identical

Thousands Preset Shortcut

javascript
set_config({ _shortcut_prefix: "!t" });

calc("1234567.89 | =2 :en"); // '1,234,567.89'
calc("1234567.89 | =2 :eu"); // '1.234.567,89'

Compact Format Shortcut

javascript
set_config({ _shortcut_prefix: "!c" });

calc("1234567 | =2 :wan"); // '123.45万'

!u Smart Unit Handling

!u without arguments is smart:

  • With _unit_default_out: converts and shows the unit
  • Without _unit_default_out: strips the unit
javascript
// With default output unit
set_config({
  _unit_convert_out: { 元: { 分: 0.01 } },
  _unit_default_out: "元",
});
calc("10000 | !u"); // '100元'
calc("10000 | !uh"); // '100' (convert but hide unit)

// Without default output unit
set_config({ _unit_convert_out: { 元: { 分: 0.01 } } });
calc("100元 | !u"); // '100' (strip unit)

Released under the MIT License