ob1 0.54.1 → 0.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # ob1
2
+
3
+ A small library for working with 0- and 1-based offsets in a type-checked way.
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "ob1",
3
- "version": "0.54.1",
4
- "description": "ob1",
5
- "main": "src/index.js",
3
+ "version": "0.55.0",
4
+ "description": "A small library for working with 0- and 1-based offsets in a type-checked way.",
5
+ "main": "src/ob1.js",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git@github.com:facebook/metro.git"
@@ -16,4 +16,4 @@
16
16
  ],
17
17
  "license": "MIT",
18
18
  "dependencies": {}
19
- }
19
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * strict-local
8
+ * @format
9
+ * @emails oncall+metro_bundler
10
+ */
11
+ "use strict";
12
+
13
+ const _require = require("../ob1"),
14
+ add = _require.add,
15
+ get0 = _require.get0,
16
+ get1 = _require.get1,
17
+ add1 = _require.add1,
18
+ sub1 = _require.sub1,
19
+ sub = _require.sub,
20
+ neg = _require.neg,
21
+ add0 = _require.add0,
22
+ inc = _require.inc;
23
+
24
+ const FORTY_TWO_0 = add0(42);
25
+ const FORTY_TWO_1 = add1(42);
26
+ module.exports = {
27
+ testSafeOps() {
28
+ add(FORTY_TWO_0, FORTY_TWO_0);
29
+ add(FORTY_TWO_0, FORTY_TWO_1);
30
+ add(FORTY_TWO_1, FORTY_TWO_0);
31
+ sub(FORTY_TWO_1, FORTY_TWO_1);
32
+ add(FORTY_TWO_0, 9000);
33
+ add(FORTY_TWO_0, 9000);
34
+ add(FORTY_TWO_1, 9000);
35
+ sub(FORTY_TWO_1, 9000);
36
+ get0(FORTY_TWO_0);
37
+ get1(FORTY_TWO_1);
38
+ neg(FORTY_TWO_0);
39
+ add1(FORTY_TWO_0);
40
+ sub1(FORTY_TWO_1);
41
+ inc(FORTY_TWO_0);
42
+ inc(FORTY_TWO_1);
43
+ },
44
+
45
+ testUnsafeOps() {
46
+ // $FlowExpectedError - adding two 1-based offsets.
47
+ add(FORTY_TWO_1, FORTY_TWO_1); // $FlowExpectedError - subtracting 1-based offset from 0-based offset.
48
+
49
+ sub(FORTY_TWO_0, FORTY_TWO_1); // $FlowExpectedError - direct computations with offsets are disallowed.
50
+
51
+ FORTY_TWO_0 - 1; // $FlowExpectedError - direct computations with offsets are disallowed.
52
+
53
+ FORTY_TWO_1 - 1; // $FlowExpectedError - extracting a 1-based offset as a 0-based number
54
+
55
+ get0(FORTY_TWO_1); // $FlowExpectedError - extracting a 0-based offset as a 1-based number
56
+
57
+ get1(FORTY_TWO_0); // $FlowExpectedError - negating a 1-based offset
58
+
59
+ neg(FORTY_TWO_1); // $FlowExpectedError - adding 1 to an offset that's already 1-based
60
+
61
+ add1(FORTY_TWO_1); // $FlowExpectedError - subtracting 1 from an offset that's already 0-based
62
+
63
+ sub1(FORTY_TWO_0); // $FlowExpectedError - extracting an arbitrary number as a 0-based number
64
+
65
+ get0(42); // $FlowExpectedError - extracting an arbitrary number as a 1-based number
66
+
67
+ get1(42);
68
+ }
69
+ };
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ * @emails oncall+metro_bundler
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ const {add, get0, get1, add1, sub1, sub, neg, add0, inc} = require('../ob1');
15
+
16
+ import type {Number0, Number1} from '../ob1';
17
+ const FORTY_TWO_0 = add0(42);
18
+ const FORTY_TWO_1 = add1(42);
19
+
20
+ module.exports = {
21
+ testSafeOps() {
22
+ (add(FORTY_TWO_0, FORTY_TWO_0): Number0);
23
+ (add(FORTY_TWO_0, FORTY_TWO_1): Number1);
24
+ (add(FORTY_TWO_1, FORTY_TWO_0): Number1);
25
+ (sub(FORTY_TWO_1, FORTY_TWO_1): Number0);
26
+ (add(FORTY_TWO_0, 9000): Number0);
27
+ (add(FORTY_TWO_0, 9000): Number0);
28
+ (add(FORTY_TWO_1, 9000): Number1);
29
+ (sub(FORTY_TWO_1, 9000): Number1);
30
+ (get0(FORTY_TWO_0): number);
31
+ (get1(FORTY_TWO_1): number);
32
+ (neg(FORTY_TWO_0): Number0);
33
+ (add1(FORTY_TWO_0): Number1);
34
+ (sub1(FORTY_TWO_1): Number0);
35
+ (inc(FORTY_TWO_0): Number0);
36
+ (inc(FORTY_TWO_1): Number1);
37
+ },
38
+ testUnsafeOps() {
39
+ // $FlowExpectedError - adding two 1-based offsets.
40
+ add(FORTY_TWO_1, FORTY_TWO_1);
41
+
42
+ // $FlowExpectedError - subtracting 1-based offset from 0-based offset.
43
+ sub(FORTY_TWO_0, FORTY_TWO_1);
44
+
45
+ // $FlowExpectedError - direct computations with offsets are disallowed.
46
+ FORTY_TWO_0 - 1;
47
+
48
+ // $FlowExpectedError - direct computations with offsets are disallowed.
49
+ FORTY_TWO_1 - 1;
50
+
51
+ // $FlowExpectedError - extracting a 1-based offset as a 0-based number
52
+ get0(FORTY_TWO_1);
53
+
54
+ // $FlowExpectedError - extracting a 0-based offset as a 1-based number
55
+ get1(FORTY_TWO_0);
56
+
57
+ // $FlowExpectedError - negating a 1-based offset
58
+ neg(FORTY_TWO_1);
59
+
60
+ // $FlowExpectedError - adding 1 to an offset that's already 1-based
61
+ add1(FORTY_TWO_1);
62
+
63
+ // $FlowExpectedError - subtracting 1 from an offset that's already 0-based
64
+ sub1(FORTY_TWO_0);
65
+
66
+ // $FlowExpectedError - extracting an arbitrary number as a 0-based number
67
+ get0(42);
68
+
69
+ // $FlowExpectedError - extracting an arbitrary number as a 1-based number
70
+ get1(42);
71
+ },
72
+ };
package/src/ob1.js ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * strict-local
8
+ * @format
9
+ */
10
+ "use strict";
11
+ /* eslint-disable no-redeclare */
12
+ // A type representing 0-based offsets.
13
+
14
+ // A type representing 1-based offsets.
15
+ // Add two offsets or numbers.
16
+ function add(a, b) {
17
+ return a + b;
18
+ } // Subtract a number or 0-based offset from a 1/0-based offset.
19
+
20
+ function sub(a, b) {
21
+ return a - b;
22
+ } // Get the underlying number of a 0-based offset, casting away the opaque type.
23
+
24
+ function get0(x) {
25
+ return x;
26
+ } // Get the underlying number of a 1-based offset, casting away the opaque type.
27
+
28
+ function get1(x) {
29
+ return x;
30
+ } // Add 1 to a 0-based offset, thus converting it to 1-based.
31
+
32
+ function add1(x) {
33
+ return x + 1;
34
+ } // Subtract 1 from a 1-based offset, thus converting it to 0-based.
35
+
36
+ function sub1(x) {
37
+ return x - 1;
38
+ } // Negate a 0-based offset.
39
+
40
+ function neg(x) {
41
+ return -x;
42
+ } // Cast a number to a 0-based offset.
43
+
44
+ function add0(x) {
45
+ return x;
46
+ } // Increment a 0-based offset.
47
+
48
+ function inc(x) {
49
+ return x + 1;
50
+ }
51
+
52
+ module.exports = {
53
+ add,
54
+ get0,
55
+ get1,
56
+ add1,
57
+ sub1,
58
+ sub,
59
+ neg,
60
+ add0,
61
+ inc
62
+ };
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ /* eslint-disable no-redeclare */
14
+
15
+ // A type representing 0-based offsets.
16
+ export opaque type Number0 = number;
17
+ // A type representing 1-based offsets.
18
+ export opaque type Number1 = number;
19
+
20
+ // Add two offsets or numbers.
21
+ declare function add(a: Number1, b: number): Number1;
22
+ declare function add(a: number, b: Number1): Number1;
23
+ declare function add(a: Number0, b: number): Number0;
24
+ declare function add(a: number, b: Number0): Number0;
25
+ declare function add(a: Number1, b: Number0): Number1;
26
+ declare function add(a: Number0, b: Number1): Number1;
27
+ declare function add(a: Number0, b: Number0): Number0;
28
+
29
+ function add(a, b) {
30
+ return a + b;
31
+ }
32
+
33
+ // Subtract a number or 0-based offset from a 1/0-based offset.
34
+ declare function sub(a: Number1, b: number): Number1;
35
+ declare function sub(a: Number0, b: number): Number0;
36
+ declare function sub(a: number, b: Number0): Number0;
37
+ declare function sub(a: Number0, b: number): Number0;
38
+ declare function sub(a: Number1, b: Number0): Number1;
39
+ declare function sub(a: Number0, b: Number0): Number0;
40
+ declare function sub(a: Number1, b: Number1): Number0;
41
+
42
+ function sub(a, b) {
43
+ return a - b;
44
+ }
45
+
46
+ // Get the underlying number of a 0-based offset, casting away the opaque type.
47
+ declare function get0(x: Number0): number;
48
+ declare function get0(x: void | null): void | null;
49
+ function get0(x) {
50
+ return x;
51
+ }
52
+
53
+ // Get the underlying number of a 1-based offset, casting away the opaque type.
54
+ declare function get1(x: Number1): number;
55
+ declare function get1(x: void | null): void | null;
56
+ function get1(x) {
57
+ return x;
58
+ }
59
+
60
+ // Add 1 to a 0-based offset, thus converting it to 1-based.
61
+ function add1(x: Number0 | number): Number1 {
62
+ return x + 1;
63
+ }
64
+
65
+ // Subtract 1 from a 1-based offset, thus converting it to 0-based.
66
+ function sub1(x: Number1): Number0 {
67
+ return x - 1;
68
+ }
69
+
70
+ // Negate a 0-based offset.
71
+ function neg(x: Number0): Number0 {
72
+ return -x;
73
+ }
74
+
75
+ // Cast a number to a 0-based offset.
76
+ function add0(x: number): Number0 {
77
+ return x;
78
+ }
79
+
80
+ // Increment a 0-based offset.
81
+ declare function inc(a: Number0): Number0;
82
+ // Increment a 1-based offset.
83
+ declare function inc(a: Number1): Number1;
84
+
85
+ function inc(x) {
86
+ return x + 1;
87
+ }
88
+
89
+ module.exports = {add, get0, get1, add1, sub1, sub, neg, add0, inc};
package/src/index.js DELETED
@@ -1 +0,0 @@
1
- // TODO