All files / src/implementation fs.ts

61.54% Statements 16/26
50% Branches 7/14
100% Functions 3/3
58.33% Lines 14/24
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                                          5x             17x   17x     17x                   51x 17x 17x             17x 17x                               5x 6x   6x   6x 6x        
/**
 * 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.
 */
/**
 * @fileoverview Some methods copied from goog.fs.
 * We don't include goog.fs because it pulls in a bunch of Deferred code that
 * bloats the size of the released binary.
 */
import * as array from './array';
import * as type from './type';
 
declare var IBlobBuilder;
declare var BlobBuilder;
declare var WebKitBlobBuilder;
 
function getBlobBuilder(): (typeof IBlobBuilder) | undefined {
  Iif (typeof BlobBuilder !== 'undefined') {
    return BlobBuilder;
  } else Iif (typeof WebKitBlobBuilder !== 'undefined') {
    return WebKitBlobBuilder;
  } else {
    return undefined;
  }
}
 
/**
 * Concatenates one or more values together and converts them to a Blob.
 *
 * @param var_args The values that will make up the resulting blob.
 * @return The blob.
 */
export function getBlob(...var_args: (string | Blob | ArrayBuffer)[]): Blob {
  let BlobBuilder = getBlobBuilder();
  Iif (BlobBuilder !== undefined) {
    let bb = new BlobBuilder();
    for (let i = 0; i < var_args.length; i++) {
      bb.append(var_args[i]);
    }
    return bb.getBlob();
  } else {
    Eif (type.isNativeBlobDefined()) {
      return new Blob(var_args);
    } else {
      throw Error("This browser doesn't seem to support creating Blobs");
    }
  }
}
 
/**
 * Slices the blob. The returned blob contains data from the start byte
 * (inclusive) till the end byte (exclusive). Negative indices cannot be used.
 *
 * @param blob The blob to be sliced.
 * @param start Index of the starting byte.
 * @param end Index of the ending byte.
 * @return The blob slice or null if not supported.
 */
export function sliceBlob(blob: Blob, start: number, end: number): Blob | null {
  Iif ((blob as any).webkitSlice) {
    return (blob as any).webkitSlice(start, end);
  } else Iif ((blob as any).mozSlice) {
    return (blob as any).mozSlice(start, end);
  } else Eif (blob.slice) {
    return blob.slice(start, end);
  }
  return null;
}