Skip to content

Commit

Permalink
Create ob1 package
Browse files Browse the repository at this point in the history
Reviewed By: cpojer

Differential Revision: D15972397

fbshipit-source-id: 589cb663775c3560705c501a37c6be4559238015
  • Loading branch information
motiz88 authored and facebook-github-bot committed Jul 2, 2019
1 parent a681b18 commit 437374f
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/ob1/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BUCK
**/__mocks__/**
**/__tests__/**
build
src.real
yarn.lock
3 changes: 3 additions & 0 deletions packages/ob1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ob1

A small library for working with 0- and 1-based offsets in a type-checked way.
19 changes: 19 additions & 0 deletions packages/ob1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "ob1",
"version": "0.54.1",
"description": "A small library for working with 0- and 1-based offsets in a type-checked way.",
"main": "src/ob1.js",
"repository": {
"type": "git",
"url": "git@github.com:facebook/metro.git"
},
"scripts": {
"prepare-release": "test -d build && rm -rf src.real && mv src src.real && mv build src",
"cleanup-release": "test ! -e build && mv src build && mv src.real src"
},
"keywords": [
"metro"
],
"license": "MIT",
"dependencies": {}
}
72 changes: 72 additions & 0 deletions packages/ob1/src/__flowtests__/ob1-flowtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* 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
* @emails oncall+metro_bundler
*/

'use strict';

const {add, get0, get1, add1, sub1, sub, neg, add0, inc} = require('../ob1');

import type {Number0, Number1} from '../ob1';
const FORTY_TWO_0 = add0(42);
const FORTY_TWO_1 = add1(42);

module.exports = {
testSafeOps() {
(add(FORTY_TWO_0, FORTY_TWO_0): Number0);
(add(FORTY_TWO_0, FORTY_TWO_1): Number1);
(add(FORTY_TWO_1, FORTY_TWO_0): Number1);
(sub(FORTY_TWO_1, FORTY_TWO_1): Number0);
(add(FORTY_TWO_0, 9000): Number0);
(add(FORTY_TWO_0, 9000): Number0);
(add(FORTY_TWO_1, 9000): Number1);
(sub(FORTY_TWO_1, 9000): Number1);
(get0(FORTY_TWO_0): number);
(get1(FORTY_TWO_1): number);
(neg(FORTY_TWO_0): Number0);
(add1(FORTY_TWO_0): Number1);
(sub1(FORTY_TWO_1): Number0);
(inc(FORTY_TWO_0): Number0);
(inc(FORTY_TWO_1): Number1);
},
testUnsafeOps() {
// $FlowExpectedError - adding two 1-based offsets.
add(FORTY_TWO_1, FORTY_TWO_1);

// $FlowExpectedError - subtracting 1-based offset from 0-based offset.
sub(FORTY_TWO_0, FORTY_TWO_1);

// $FlowExpectedError - direct computations with offsets are disallowed.
FORTY_TWO_0 - 1;

// $FlowExpectedError - direct computations with offsets are disallowed.
FORTY_TWO_1 - 1;

// $FlowExpectedError - extracting a 1-based offset as a 0-based number
get0(FORTY_TWO_1);

// $FlowExpectedError - extracting a 0-based offset as a 1-based number
get1(FORTY_TWO_0);

// $FlowExpectedError - negating a 1-based offset
neg(FORTY_TWO_1);

// $FlowExpectedError - adding 1 to an offset that's already 1-based
add1(FORTY_TWO_1);

// $FlowExpectedError - subtracting 1 from an offset that's already 0-based
sub1(FORTY_TWO_0);

// $FlowExpectedError - extracting an arbitrary number as a 0-based number
get0(42);

// $FlowExpectedError - extracting an arbitrary number as a 1-based number
get1(42);
},
};
46 changes: 46 additions & 0 deletions packages/ob1/src/__tests__/ob1-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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
* @emails oncall+metro_bundler
*/

'use strict';

const {add, get0, get1, add1, sub1, sub, neg, add0, inc} = require('../ob1');

const FORTY_TWO_0 = add0(42);
const FORTY_TWO_1 = add1(41);

describe('ob1', () => {
test('add', () => {
expect(get0(add(FORTY_TWO_0, 3))).toBe(45);
expect(get1(add(FORTY_TWO_1, 3))).toBe(45);
});

test('sub', () => {
expect(get0(sub(FORTY_TWO_0, 3))).toBe(39);
expect(get1(sub(FORTY_TWO_1, 3))).toBe(39);
});

test('neg', () => {
expect(get0(neg(FORTY_TWO_0))).toBe(-42);
});

test('inc', () => {
expect(get0(inc(FORTY_TWO_0))).toBe(43);
expect(get1(inc(FORTY_TWO_1))).toBe(43);
});

test('add1', () => {
expect(get1(add1(FORTY_TWO_0))).toBe(43);
});

test('sub1', () => {
expect(get0(sub1(FORTY_TWO_1))).toBe(41);
});
});
89 changes: 89 additions & 0 deletions packages/ob1/src/ob1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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, b) {
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, b) {
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) {
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) {
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) {
return x + 1;
}

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

0 comments on commit 437374f

Please sign in to comment.