-
Notifications
You must be signed in to change notification settings - Fork 74
/
typeGuards.js
87 lines (79 loc) · 2.16 KB
/
typeGuards.js
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
import { Fail, q } from '@endo/errors';
import { passStyleOf } from './passStyleOf.js';
/** @import {CopyArray, CopyRecord, Passable, RemotableObject} from './types.js' */
/**
* Check whether the argument is a pass-by-copy array, AKA a "copyArray"
* in @endo/marshal terms
*
* @param {any} arr
* @returns {arr is CopyArray<any>}
*/
const isCopyArray = arr => passStyleOf(arr) === 'copyArray';
harden(isCopyArray);
/**
* Check whether the argument is a pass-by-copy record, AKA a
* "copyRecord" in @endo/marshal terms
*
* @param {any} record
* @returns {record is CopyRecord<any>}
*/
const isRecord = record => passStyleOf(record) === 'copyRecord';
harden(isRecord);
/**
* Check whether the argument is a remotable.
*
* @param {Passable} remotable
* @returns {remotable is RemotableObject}
*/
const isRemotable = remotable => passStyleOf(remotable) === 'remotable';
harden(isRemotable);
/**
* @param {any} arr
* @param {string=} optNameOfArray
* @returns {asserts arr is CopyArray<any>}
*/
const assertCopyArray = (arr, optNameOfArray = 'Alleged array') => {
const passStyle = passStyleOf(arr);
passStyle === 'copyArray' ||
Fail`${q(optNameOfArray)} ${arr} must be a pass-by-copy array, not ${q(
passStyle,
)}`;
};
harden(assertCopyArray);
/**
* @param {any} record
* @param {string=} optNameOfRecord
* @returns {asserts record is CopyRecord<any>}
*/
const assertRecord = (record, optNameOfRecord = 'Alleged record') => {
const passStyle = passStyleOf(record);
passStyle === 'copyRecord' ||
Fail`${q(optNameOfRecord)} ${record} must be a pass-by-copy record, not ${q(
passStyle,
)}`;
};
harden(assertRecord);
/**
* @param {Passable} remotable
* @param {string=} optNameOfRemotable
* @returns {asserts remotable is RemotableObject}
*/
const assertRemotable = (
remotable,
optNameOfRemotable = 'Alleged remotable',
) => {
const passStyle = passStyleOf(remotable);
passStyle === 'remotable' ||
Fail`${q(optNameOfRemotable)} ${remotable} must be a remotable, not ${q(
passStyle,
)}`;
};
harden(assertRemotable);
export {
assertRecord,
assertCopyArray,
assertRemotable,
isRemotable,
isRecord,
isCopyArray,
};