Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small script to test all examples at once #30

Open
xabru opened this issue Sep 16, 2022 · 0 comments
Open

Small script to test all examples at once #30

xabru opened this issue Sep 16, 2022 · 0 comments

Comments

@xabru
Copy link

xabru commented Sep 16, 2022

#!/usr/bin/env bash

print_examples() {
  json_example() {
    cat <<END
{
  "menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {
          "value": "New",
          "onclick": "CreateNewDoc()"
        },
        {
          "value": "Open",
          "onclick": "OpenDoc()"
        },
        {
          "value": "Close",
          "onclick": "CloseDoc()"
        }
      ]
    }
  }
}
END
  }

  :

  declare select_example="${select_example:+}"

  declare -a iarr_jj_examples=(
    "getting_a_value"
    "get_a_block_of_json"
    "try_to_get_a_non__existent_key"
    "get_the_raw_string_value"
    "get_an_array_value_by_index"
    "json_lines_1"
    "json_lines_2"
    "json_lines_3"
    "json_lines_4"
    "update_a_value"
    "set_a_new_value"
    "set_a_new_nested_value"
    "replace_an_array_value_by_index"
    "append_an_array"
    "set_a_raw_block_of_json"
    "start_new_json_document"
    "delete_a_value"
    "delete_an_array_value_by_index"
    "delete_last_item_in_array"
    "optimistically_update_a_value"
    "pretty_printing"
    "ugly_printing"
  )

  declare -A aarr_jj_examples=(
    [getting_a_value]="json_example | jj menu.id"
    [get_a_block_of_json]="json_example | jj menu.popup.menuitem"
    [try_to_get_a_non__existent_key]="json_example | jj menu.popuppet"
    [get_the_raw_string_value]="json_example | jj -r menu.popup"
    [get_an_array_value_by_index]="json_example | jj menu.popup.menuitem.2"
    [json_lines_1]="json_example | jj ..#"
    [json_lines_2]="json_example | jj ..0"
    [json_lines_3]="json_example | jj ..#.value"
    [json_lines_4]="json_example | jj menu.popup.menuitem..#[value=Open]"
    [update_a_value]="json_example | jj -v Test menu.popup.menuitem"
    [set_a_new_value]="json_example | jj -v Value_Test Key_Test"
    [set_a_new_nested_value]="json_example | jj -v Value_Test Key_Test.test_nested"
    [replace_an_array_value_by_index]="json_example | jj -v Value_Test menu.popup.menuitem.1"
    [append_an_array]="json_example | jj -v Value_Test menu.popup.menuitem.-1"
    [set_a_raw_block_of_json]="json_example | jj -r -v ['Tom','Andy'] menu.popup.menuitem.0"
    [start_new_json_document]="printf '' | jj -v 'Sam' name.first"
    [delete_a_value]="json_example | jj -D menu.popup"
    [delete_an_array_value_by_index]="json_example | jj -D menu.popup.menuitem.0"
    [delete_last_item_in_array]="json_example | jj -D menu.popup.menuitem.-1"
    [optimistically_update_a_value]="json_example | jj -v Tim -O menu.popup.menuitem.-1"
    [pretty_printing]="json_example | jj -p menu.popup.menuitem"
    [ugly_printing]="json_example | jj -u menu.popup.menuitem"
  )

  :

  json_example | bat -P --language json

  for select_example in "${iarr_jj_examples[@]}"; do
    : "${select_example//_/ }"
    printf "\e[1;93m%s\e[0m\n\n" "${_^}"
    printf "\e[91m\$_ %s\e[0m\n\n" "${aarr_jj_examples["${select_example}"]}"

    time eval "${aarr_jj_examples["${select_example}"]}"

    printf "\n\n"
  done
}

print_examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant