codemelted.js Module
    Preparing search index...

    Function json_check_type

    • Utility to check parameters of a function to ensure they are of an expected type.

      Parameters

      • params: { count?: number; data: any; should_throw?: boolean; type: any }

        The named parameters

        • Optionalcount?: number

          Checks the v parameter function signature to ensure the appropriate number of parameters are specified.

        • data: any

          The parameter to be checked.

        • Optionalshould_throw?: boolean

          Whether to throw instead of returning a value upon failure.

        • type: any

      Returns boolean

      true if it meets the expectations, false otherwise.

      // Check if data is an expected type
      let is_expected = json_check_type({type: "string", data: some_data});
      if (is_expected) {
      // Do your processing
      }

      // Throw if not an expected type
      json_check_type({type: "string", data: some_data, should_throw: true});

      // Check complex data type
      let is_expected = json_check_type({
      type: Uint8Array,
      data: some_data,
      });

      // Check callback function follows expectations of 2 parameters.
      json_check_type({
      type: "function",
      data: some_callback,
      count: 2,
      should_throw: true,
      });