Skip to content

Latest commit

 

History

History
155 lines (115 loc) · 6.37 KB

variable-assignments.md

File metadata and controls

155 lines (115 loc) · 6.37 KB

literals

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let str = "string";
  let num = 42;
  let bool = true || false;
  let object = { str, num, bool };
  let object2 = { str: "string", num: 33, inner: object };
  let arrayOfNumbers = [1, 2, 3, 4, 5];
  let arrayOfObjects = [
    { left: 1, right: 2 },
    { left: 3, right: 4 },
    { left: 5, right: 6 },
  ];
  return { arrayOfNumbers, arrayOfObjects, object2 };
});

type of expressions

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let str = typeof "abcdef";
  let num = typeof 123;
  let bool = typeof true;
  let object = typeof { str, num, bool };
  let undef = typeof undefined;
  let _null = typeof null;

  if (
    str === "string" &&
    num === "number" &&
    bool === "boolean" &&
    object === "object" &&
    undef === "undefined" &&
    _null === "object"
  ) {
    return "ok";
  }

  return "not ok";
});

binary expression

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let str = "abcdef";
  let num = 123;
  let expr1 = str === "123";
  let expr2 = num === 456;
  let expr3 = expr1 === expr2;

  if (expr3) {
    return "ok";
  }
  return "not ok";
});

array with identifiers

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let str = "string";
  let num = 42;
  let bool = true || false;
  let object = { str, num, bool };
  let array = [str, num, bool, object];
  return array;
});

unassigned variable

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let arr: [];
  let two: string;
  return "ok";
});

assignment to undefined

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let _undefined = undefined;
  return "ok";
});

assignment to null

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let _null = null;
  return "ok";
});

array indexer

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let arr = [1, 2, 3, 4, 5];
  let two = arr[1];
  arr[1] = arr[3];
  arr[3] = two;
  return arr; //returns [1, 4, 3, 2, 5]
});

functions

Open in playground

export const main = asl.deploy.asStateMachine(async () => {
  let str = asl.states.format("hello {}", "world");
  let num = asl.states.format("answer is {}", 42);
  let combined = asl.states.format("1: {}\n 2: {}", str, num);
  let arr = asl.states.array(str, num, combined);
  return arr;
});