grpc-js-core: simplify Metadata clone()

This commit inlines the only use of cloneMetadataObject(). It
also eliminates an extra MetadataObject - the result of
cloneMetadataObject() was allocating a new MetadataObject to
replace the internal representation of the newly allocated
Metadata object in clone().
This commit is contained in:
cjihrig 2018-10-24 12:09:57 -04:00
parent 2f679031fa
commit a83c924801
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5

View File

@ -5,23 +5,6 @@ const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/;
export type MetadataValue = string|Buffer;
export type MetadataObject = Map<string, MetadataValue[]>;
function cloneMetadataObject(repr: MetadataObject): MetadataObject {
const result: MetadataObject = new Map<string, MetadataValue[]>();
repr.forEach((value, key) => {
const clonedValue: MetadataValue[] = value.map(v => {
if (v instanceof Buffer) {
return Buffer.from(v);
} else {
return v;
}
});
result.set(key, clonedValue);
});
return result;
}
function isLegalKey(key: string): boolean {
return LEGAL_KEY_REGEX.test(key);
}
@ -144,7 +127,20 @@ export class Metadata {
*/
clone(): Metadata {
const newMetadata = new Metadata();
newMetadata.internalRepr = cloneMetadataObject(this.internalRepr);
const newInternalRepr = newMetadata.internalRepr;
this.internalRepr.forEach((value, key) => {
const clonedValue: MetadataValue[] = value.map(v => {
if (v instanceof Buffer) {
return Buffer.from(v);
} else {
return v;
}
});
newInternalRepr.set(key, clonedValue);
});
return newMetadata;
}