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);
});
}
fieldErrors[key] = ann;
}
}
}
// 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);
}
} catch (e) {
/* istanbul ignore else */
if (isAnnotation(e)) {
tuples.length = 0; // Clear the tuples array
errors.push([key, e]);
} else {
// Otherwise, simply rethrow it
/* istanbul ignore next */
throw e;
}
}
});
if (errors.length > 0) {
return Err(annotateFields(blob, errors));
} else {
return Ok(new Map(tuples));
}
}
);
Object.keys(blob).forEach((key: string) => {
const value: T = blob[key];
const result = decoder(value);
try {
const okValue = result.unwrap();
if (errors.length === 0) {
tuples.push([key, okValue]);
}
} catch (e) {
/* istanbul ignore else */
if (isAnnotation(e)) {
tuples.length = 0; // Clear the tuples array
errors.push([key, e]);
} else {
// Otherwise, simply rethrow it
/* istanbul ignore next */
throw e;
}
}
});
// NOTE: We're using .keys() here over .entries(), since .entries()
// will type the value part as "mixed"
for (const key of Object.keys(mapping)) {
const decoder = mapping[key];
const value = blob[key];
const result = decoder(value);
try {
record[key] = result.unwrap();
// If this succeeded, remove the key from the missing keys
// tracker
missing.delete(key);
} catch (ann) {
/* istanbul ignore next */
if (!isAnnotation(ann)) {
throw ann;
}
// Keep track of the annotation, but don't return just yet. We
// want to collect more error information.
if (value === undefined) {
// Explicitly add it to the missing set if the value is
// undefined. This covers explicit undefineds to be
// treated the same as implicit undefineds (aka missing
// keys).
missing.add(key);
} else {
fieldErrors[key] = ann;
}
}
}
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'));