All files / src/implementation args.ts

95.24% Statements 60/63
89.74% Branches 35/39
100% Functions 17/17
95.16% Lines 59/62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154                              3x   3x 3x               3x 170x 170x 170x 167x 71x 71x     170x 170x 22x             148x 150x 150x   33x     33x                 3x         333x 333x 171x     171x   333x   3x   3x 68x 54x 50x       3x         78x 7x       94x 68x   26x   94x     3x     11x     11x 2x     12x     3x 29x     3x   4x 4x 4x     8x     3x         23x 23x     23x 20x     72x     3x   44x 44x 4x     118x    
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import * as errorsExports from './error';
import { errors } from './error';
import * as MetadataUtils from './metadata';
import * as type from './type';
 
/**
 * @param name Name of the function.
 * @param specs Argument specs.
 * @param passed The actual arguments passed to the function.
 * @throws {fbs.Error} If the arguments are invalid.
 */
export function validate(name: string, specs: ArgSpec[], passed: IArguments) {
  let minArgs = specs.length;
  let maxArgs = specs.length;
  for (let i = 0; i < specs.length; i++) {
    if (specs[i].optional) {
      minArgs = i;
      break;
    }
  }
  let validLength = minArgs <= passed.length && passed.length <= maxArgs;
  if (!validLength) {
    throw errorsExports.invalidArgumentCount(
      minArgs,
      maxArgs,
      name,
      passed.length
    );
  }
  for (let i = 0; i < passed.length; i++) {
    try {
      specs[i].validator(passed[i]);
    } catch (e) {
      Iif (e instanceof Error) {
        throw errorsExports.invalidArgument(i, name, e.message);
      } else {
        throw errorsExports.invalidArgument(i, name, e);
      }
    }
  }
}
 
/**
 * @struct
 */
export class ArgSpec {
  validator: (p1: any) => void;
  optional: boolean;
 
  constructor(validator: (p1: any) => void, opt_optional?: boolean) {
    let self = this;
    this.validator = function(p: any) {
      Iif (self.optional && !type.isJustDef(p)) {
        return;
      }
      validator(p);
    };
    this.optional = !!opt_optional;
  }
}
 
export function and_(v1: (p1: any) => void, v2: Function): (p1: any) => void {
  return function(p) {
    v1(p);
    v2(p);
  };
}
 
export function stringSpec(
  opt_validator?: (p1: any) => void | null,
  opt_optional?: boolean
): ArgSpec {
  function stringValidator(p: any) {
    if (!type.isString(p)) {
      throw 'Expected string.';
    }
  }
  let validator;
  if (opt_validator) {
    validator = and_(stringValidator, opt_validator);
  } else {
    validator = stringValidator;
  }
  return new ArgSpec(validator, opt_optional);
}
 
export function uploadDataSpec(): ArgSpec {
  function validator(p: any) {
    let valid =
      p instanceof Uint8Array ||
      p instanceof ArrayBuffer ||
      (type.isNativeBlobDefined() && p instanceof Blob);
    if (!valid) {
      throw 'Expected Blob or File.';
    }
  }
  return new ArgSpec(validator);
}
 
export function metadataSpec(opt_optional?: boolean): ArgSpec {
  return new ArgSpec(MetadataUtils.metadataValidator, opt_optional);
}
 
export function nonNegativeNumberSpec(): ArgSpec {
  function validator(p: any) {
    let valid = type.isNumber(p) && p >= 0;
    Eif (!valid) {
      throw 'Expected a number 0 or greater.';
    }
  }
  return new ArgSpec(validator);
}
 
export function looseObjectSpec(
  opt_validator?: ((p1: any) => void) | null,
  opt_optional?: boolean
): ArgSpec {
  function validator(p: any) {
    let isLooseObject = p === null || (type.isDef(p) && p instanceof Object);
    Iif (!isLooseObject) {
      throw 'Expected an Object.';
    }
    if (opt_validator !== undefined && opt_validator !== null) {
      opt_validator(p);
    }
  }
  return new ArgSpec(validator, opt_optional);
}
 
export function nullFunctionSpec(opt_optional?: boolean): ArgSpec {
  function validator(p: any) {
    let valid = p === null || type.isFunction(p);
    if (!valid) {
      throw 'Expected a Function.';
    }
  }
  return new ArgSpec(validator, opt_optional);
}