ob1.js.flow 2.5 KB
Newer Older
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
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict-local
 * @format
 */

'use strict';

/* eslint-disable no-redeclare */

// A type representing 0-based offsets.
export opaque type Number0 = number;
// A type representing 1-based offsets.
export opaque type Number1 = number;

// Add two offsets or numbers.
declare function add(a: Number1, b: number): Number1;
declare function add(a: number, b: Number1): Number1;
declare function add(a: Number0, b: number): Number0;
declare function add(a: number, b: Number0): Number0;
declare function add(a: Number1, b: Number0): Number1;
declare function add(a: Number0, b: Number1): Number1;
declare function add(a: Number0, b: Number0): Number0;

function add(a: number, b: number): number {
  return a + b;
}

// Subtract a number or 0-based offset from a 1/0-based offset.
declare function sub(a: Number1, b: number): Number1;
declare function sub(a: Number0, b: number): Number0;
declare function sub(a: number, b: Number0): Number0;
declare function sub(a: Number0, b: number): Number0;
declare function sub(a: Number1, b: Number0): Number1;
declare function sub(a: Number0, b: Number0): Number0;
declare function sub(a: Number1, b: Number1): Number0;

function sub(a: number, b: number): number {
  return a - b;
}

// Get the underlying number of a 0-based offset, casting away the opaque type.
declare function get0(x: Number0): number;
declare function get0(x: void | null): void | null;
function get0(x: number): number {
  return x;
}

// Get the underlying number of a 1-based offset, casting away the opaque type.
declare function get1(x: Number1): number;
declare function get1(x: void | null): void | null;
function get1(x: number): number {
  return x;
}

// Add 1 to a 0-based offset, thus converting it to 1-based.
function add1(x: Number0 | number): Number1 {
  return x + 1;
}

// Subtract 1 from a 1-based offset, thus converting it to 0-based.
function sub1(x: Number1): Number0 {
  return x - 1;
}

// Negate a 0-based offset.
function neg(x: Number0): Number0 {
  return -x;
}

// Cast a number to a 0-based offset.
function add0(x: number): Number0 {
  return x;
}

// Increment a 0-based offset.
declare function inc(a: Number0): Number0;
// Increment a 1-based offset.
declare function inc(a: Number1): Number1;

function inc(x: number): number {
  return x + 1;
}

module.exports = {add, get0, get1, add1, sub1, sub, neg, add0, inc};