refactor(marshal): Use more standard patterns in helpers
MODIFY
packages/marshal/src/helpers/copyRecord.js
packages/marshal/src/helpers/copyRecord.js
@@ -26,9 +26,9 @@ export const CopyRecordHelper = harden({
);
}
- for (const key of ownKeys(candidate)) {
- const valid =
- (typeof key === 'string' ||
+ return ownKeys(candidate).every(key => {
+ return (
+ ((typeof key === 'string' ||
(!!reject &&
reject(
X`Records can only have string-named properties: ${candidate}`,
@@ -38,12 +38,9 @@ export const CopyRecordHelper = harden({
reject(
// TODO: Update message now that there is no such thing as "implicit Remotable".
X`Records cannot contain non-far functions because they may be methods of an implicit Remotable: ${candidate}`,
- )));
- if (!valid) {
- return false;
- }
- }
- return true;
+ ))))
+ );
+ });
},
assertValid: (candidate, passStyleOfRecur) => {
@@ -52,6 +49,8 @@ export const CopyRecordHelper = harden({
checkNormalProperty(candidate, name, true, assertChecker);
}
// Recursively validate that each member is passable.
- values(candidate).every(v => !!passStyleOfRecur(v));
+ for (const val of values(candidate)) {
+ passStyleOfRecur(val);
+ }
},
});

MODIFY
packages/marshal/src/helpers/remotable.js
packages/marshal/src/helpers/remotable.js
@@ -188,23 +188,22 @@ export const RemotableHelper = harden({
canBeValid: (candidate, check = undefined) => {
const reject = !!check && (details => check(false, details));
- if (!isObject(candidate)) {
- return (
+ const validType =
+ (isObject(candidate) ||
(reject &&
- reject(X`cannot serialize non-objects as Remotable ${candidate}`))
- );
- } else if (isArray(candidate)) {
- return (
- (reject && reject(X`cannot serialize arrays as Remotable ${candidate}`))
- );
+ reject(X`cannot serialize non-objects as Remotable ${candidate}`))) &&
+ (!isArray(candidate) ||
+ (reject &&
+ reject(X`cannot serialize arrays as Remotable ${candidate}`)));
+ if (!validType) {
+ return false;
}
const descs = getOwnPropertyDescriptors(candidate);
if (typeof candidate === 'object') {
// Every own property (regardless of enumerability)
// must have a function value.
- const keys = ownKeys(descs);
- return keys.every(key => {
+ return ownKeys(descs).every(key => {
return (
// Typecast needed due to https://github.com/microsoft/TypeScript/issues/1863
((hasOwnPropertyOf(descs[/** @type {string} */ (key)], 'value') ||
@@ -246,9 +245,8 @@ export const RemotableHelper = harden({
X`Far functions unexpected properties besides .name and .length ${restKeys}`,
))))
);
- } else {
- return reject && reject(X`unrecognized typeof ${candidate}`);
}
+ return reject && reject(X`unrecognized typeof ${candidate}`);
},
assertValid: candidate => checkRemotable(candidate, assertChecker),

refactor(marshal): Use more standard patterns in helpers