← All procedures

optional.fromOptional

This function changes an optional into a result: a present value becomes `ok`, and `none` becomes `err(error)`.

CLI
Package
@procedures/optional · README
Path
["optional","fromOptional"]
CLI
mark optional fromOptional --o <object | object> --error <any>
As data
{"$proc":["optional","fromOptional"],"input":{}}

Use it

In TypeScript

import { Client, LocalTransport, PROCEDURE_REGISTRY } from "@procedures/client";
import "@procedures/optional/register"; // registers its procedures

const client = new Client(new LocalTransport({ registry: PROCEDURE_REGISTRY }));
const result = await client.exec(["optional","fromOptional"], { o: "<o>", error: "<error>" });
console.log(JSON.stringify(result));

From the command line

$ mark optional fromOptional --o <object | object> --error <any>

As data, in a program

{
  "$proc": ["optional", "fromOptional"],
  "input": { "o": "<o>", "error": "<error>" }
}

Input

FieldTypeRequiredDefaultDescription
oobject | objectyes
erroranyyes

Output

The schema does not list fields (object | object).

JSON Schemas
{
  "input": {
    "type": "object",
    "properties": {
      "o": {
        "anyOf": [
          {
            "type": "object",
            "properties": {
              "tag": {
                "type": "string",
                "const": "none"
              }
            },
            "required": [
              "tag"
            ],
            "additionalProperties": {}
          },
          {
            "type": "object",
            "properties": {
              "tag": {
                "type": "string",
                "const": "some"
              },
              "value": {}
            },
            "required": [
              "tag",
              "value"
            ],
            "additionalProperties": {}
          }
        ]
      },
      "error": {}
    },
    "required": [
      "o",
      "error"
    ],
    "additionalProperties": false
  },
  "output": {
    "anyOf": [
      {
        "type": "object",
        "properties": {
          "tag": {
            "type": "string",
            "const": "err"
          },
          "error": {}
        },
        "required": [
          "tag",
          "error"
        ],
        "additionalProperties": {}
      },
      {
        "type": "object",
        "properties": {
          "tag": {
            "type": "string",
            "const": "ok"
          },
          "value": {}
        },
        "required": [
          "tag",
          "value"
        ],
        "additionalProperties": {}
      }
    ]
  }
}