Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
clone.splice(
index,
1,
annotate(ann, ann.annotation !== undefined ? `${ann.annotation} (at index ${index})` : `index ${index}`)
);
// const errValue = [];
// if (index > 0) {
// errValue.push('...'); // TODO: make special mark, not string!
// }
// errValue.push(
// );
// if (index < iterable.length - 1) {
// errValue.push('...'); // TODO: make special mark, not string!
// }
return Err(annotate(clone));
}
index++;
}
return Ok(results);
}
function all(iterable: Array>, blobs: Array): DecodeResult> {
const results: Array = [];
let index = 0;
for (const result of iterable) {
try {
const value = result.unwrap();
results.push(value);
} catch (ann) {
// Rewrite the annotation to include the index information, and inject it into the original blob
const clone = [...blobs];
clone.splice(
index,
1,
annotate(ann, ann.annotation !== undefined ? `${ann.annotation} (at index ${index})` : `index ${index}`)
);
// const errValue = [];
// if (index > 0) {
// errValue.push('...'); // TODO: make special mark, not string!
// }
// errValue.push(
// );
// if (index < iterable.length - 1) {
// errValue.push('...'); // TODO: make special mark, not string!
// }
return Err(annotate(clone));
}
index++;
}
return Ok(results);
export const poja: Decoder> = (blob: mixed) => {
if (!Array.isArray(blob)) {
return Err(annotate(blob, 'Must be an array'));
}
return Ok(
// NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray
// instead of Array. For rationale, see
// https://github.com/facebook/flow/issues/7684. In this case, we
// don't want to output read-only types because it's up to the user of
// decoders to determine what they want to do with the decoded output.
// If they want to write items into the array, that's fine!
// The fastest way to turn a read-only array into a normal array in
// Javascript is to use .slice() on it, see this benchmark:
// http://jsben.ch/lO6C5
blob.slice()
);
};
}
}
// Deal with errors now. There are two classes of errors we want to
// report. First of all, we want to report any inline errors in this
// object. Lastly, any fields that are missing should be annotated on
// the outer object itself.
const fieldsWithErrors = Object.keys(fieldErrors);
if (fieldsWithErrors.length > 0 || missing.size > 0) {
let err;
if (fieldsWithErrors.length > 0) {
const errorlist = fieldsWithErrors.map(k => [k, fieldErrors[k]]);
err = annotateFields(blob, errorlist);
} else {
err = annotate(blob);
}
if (missing.size > 0) {
const errMsg = Array.from(missing)
.map(key => `"${key}"`)
.join(', ');
const pluralized = missing.size > 1 ? 'keys' : 'key';
err = annotate(err, `Missing ${pluralized}: ${errMsg}`);
}
return Err(err);
}
return Ok(record);
});
}
export const date: Decoder = (value: mixed) =>
isDate(value) ? Ok(((value: cast): Date)) : Err(annotate(value, 'Must be a Date'));
export const undefined_: Decoder = (blob: mixed) =>
blob === undefined ? Ok(blob) : Err(annotate(blob, 'Must be undefined'));
export const boolean: Decoder = (blob: mixed) => {
return typeof blob === 'boolean' ? Ok(blob) : Err(annotate(blob, 'Must be boolean'));
};
export const null_: Decoder = (blob: mixed) => (blob === null ? Ok(blob) : Err(annotate(blob, 'Must be null')));
export const anyNumber: Decoder = (blob: anything) => {
return typeof blob === 'number' && !Number.isNaN(blob) ? Ok(blob) : Err(annotate(blob, 'Must be number'));
};