Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Code Examples

Derk Norton edited this page Nov 25, 2019 · 52 revisions

Logo

A Quick Example

The following nodejs session shows a simple example covering some of the more common components in the Bali Component Framework™.

// import the required modules
const debug = 1;  // debug level: [0..3]
const bali = require('bali-component-framework').api(debug);

// construct a transaction component from a javascript object
const transaction = bali.catalog({
    transaction: bali.tag(),  // generate a new unique tag
    timestamp: bali.moment(),  // now
    consumer: bali.text('Derk Norton'),
    merchant: bali.reference('https://www.starbucks.com/'),
    amount: bali.component('4.95($currency: $USD)')
});

// print it to the console as a Bali document
console.log(transaction.toString());
[
    $transaction: #T33WMRM58A66VFW0TW07N1RADFCRMAMB
    $timestamp: <2019-11-25T18:36:41.111>
    $consumer: "Derk Norton"
    $merchant: <https://www.starbucks.com/>
    $amount: 4.95($currency: $USD)
]

// create a list containing the the keys from the transaction
const list = bali.list(transaction.getKeys());

// print the list to the console as a Bali document
console.log(list.toString());
[
    $transaction
    $timestamp
    $consumer
    $merchant
    $amount
]

// create an ordered set from the list of keys
const set = bali.set(list);  // automatically ordered

// print the set to the console as a Bali document
console.log(set.toString());
[
    $amount
    $consumer
    $merchant
    $timestamp
    $transaction
]($type: /bali/collections/Set/v1)

// sort the items in the list
list.sortItems();

// print the sorted list to the console as a Bali document
console.log(list.toString());
[
    $amount
    $consumer
    $merchant
    $timestamp
    $transaction
]

// sort the associations in the transaction
transaction.sortItems();

// print the sorted transaction to the console as a Bali document
console.log(transaction.toString());
[
    $amount: 4.95($currency: $USD)
    $consumer: "Derk Norton"
    $merchant: <https://www.starbucks.com/>
    $timestamp: <2019-11-25T18:36:41.111>
    $transaction: #T33WMRM58A66VFW0TW07N1RADFCRMAMB
]

// compare the sorted list with the keys from the sorted transaction
const keys = transaction.getKeys();
const result = keys.isEqualTo(list);
console.log(result);
true

In a nutshell, we created a catalog component that captures the attributes associated with a payment transaction. Then we extracted the keys from the catalog as a list component that preserves the order of the keys in the catalog. We used the list to create a set component which automatically orders the items in their natural (alphabetical) order. And finally we sorted the items in both the list and the catalog alphabetically and showed that the sorted list is equal to the sorted keys from the catalog.

A few key things to notice:

  • The JavaScript toString() method for each component type returns the Bali Document Notation™ source code for that component. It is like JSON on steroids and makes troubleshooting much easier.
  • The catalog component used to capture the transaction attributes is made up of key-value pairs (like a JavaScript Object). The keys and values may be any type of component. In the example all keys are symbol components. Each value in the example demonstrates a different type of elemental component.
  • The Bali Component Framework™ supports a rich set of elemental component types. Six of the more common types are shown in the example, but there are a total of fourteen elemental types:
    • angle: ~pi, ~90($degrees), ~1.23($radians)
    • binary: '0110100100011110'($encoding: $base2), '5J5Q9Q76HW90CH'
    • duration: ~P3M2DT15H31M 3 months, 2 days, 15 hours and 31 minutes
    • moment: <2017-12-30T17:38:35.726>($city: "Madrid", $country: "Spain")
    • name: /bali/types, /bali/types/Text/v1.2, /acme/results/Q3/v1
    • number: 42, e, 1.23E-56, 5i, (3, 4i), (1 e^~pi i), infinity
    • pattern: none, any, "foo[bB]ar"?
    • percent: 25%, 1.2%
    • probability: false, .5, true includes boolean values at the extremes
    • reference: <https://google.com/>
    • symbol: $first, $target, $type
    • tag: #QBZZHC49, #D8QB12WSB2LSZW1ATD4D289KPV5ZK6A9
    • text: "This is a text string."
    • version: v1, v4.3.2
  • In addition to the three collection types shown in the example (catalog, list, and set), there is also support for stack and queue collection types.
  • Any two components can be compared to see if they are equal to each other. In fact, they can be compared for their natural order as well, which is how the set component orders its items and how the sortItems() method compares items within a collection.
Clone this wiki locally