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 Feb 22, 2019 · 52 revisions

A Quick Example

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

const bali = require('bali-component-framework');

const transaction = bali.catalog({
    $transactionId: 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.parse('4.95($USD)')
});

console.log('transaction: ' + transaction);
  transaction: [
      $transactionId: #KXTXMM5ZTGNV3DD8FZP4MFMDR8WQBJ6Y
      $timestamp: <2019-02-22T15:20:32.465>
      $consumer: "Derk Norton"
      $merchant: <https://www.starbucks.com/>
      $amount: 4.95($USD)
  ]

const list = bali.list(transaction.getKeys());

console.log('list: ' + list);
  list: [
      $transactionId
      $timestamp
      $consumer
      $merchant
      $amount
  ]

const set = bali.set(list);  // automatically ordered

console.log('set: ' + set);
  set: [
      $amount
      $consumer
      $merchant
      $timestamp
      $transactionId
  ]($type: $Set)

list.sortItems();

console.log('list: ' + list);
  list: [
      $amount
      $consumer
      $merchant
      $timestamp
      $transactionId
  ]

transaction.sortItems();

console.log('transaction: ' + transaction);
  transaction: [
      $amount: 4.95($USD)
      $consumer: "Derk Norton"
      $merchant: <https://www.starbucks.com/>
      $timestamp: <2019-02-22T15:20:32.465>
      $transactionId: #KXTXMM5ZTGNV3DD8FZP4MFMDR8WQBJ6Y
  ]

transaction.getKeys().isEqualTo(list);
  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 is a different type of 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 thirteen 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")
    • 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.
Clone this wiki locally