Skip to content

Commit b0e4e4f

Browse files
authored
refactor(opentelemetry-core): simplify parseKeyPairsIntoRecord() (#5610)
1 parent 7749d0a commit b0e4e4f

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
3131
* test(sdk-metrics): fix multiple problematic assertRejects() calls [#5611](https://github.com/open-telemetry/opentelemetry-js/pull/5611) @cjihrig
3232
* refactor: replace assertRejects() with assert.rejects() [#5614](https://github.com/open-telemetry/opentelemetry-js/pull/5614) @cjihrig
3333
* refactor(core): migrate from deprecated semconv constants [#5575](https://github.com/open-telemetry/opentelemetry-js/pull/5575) @alumni55748
34+
* refactor(opentelemetry-core): simplify `parseKeyPairsIntoRecord()` [#5610](https://github.com/open-telemetry/opentelemetry-js/pull/5610) @cjihrig
3435

3536
## 2.0.0
3637

packages/opentelemetry-core/src/baggage/utils.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,17 @@ export function parsePairKeyValue(
8585
export function parseKeyPairsIntoRecord(
8686
value?: string
8787
): Record<string, string> {
88-
if (typeof value !== 'string' || value.length === 0) return {};
89-
return value
90-
.split(BAGGAGE_ITEMS_SEPARATOR)
91-
.map(entry => {
92-
return parsePairKeyValue(entry);
93-
})
94-
.filter(keyPair => keyPair !== undefined && keyPair.value.length > 0)
95-
.reduce<Record<string, string>>((headers, keyPair) => {
96-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
97-
headers[keyPair!.key] = keyPair!.value;
98-
return headers;
99-
}, {});
88+
const result: Record<string, string> = {};
89+
90+
if (typeof value === 'string' && value.length > 0) {
91+
value.split(BAGGAGE_ITEMS_SEPARATOR).forEach(entry => {
92+
const keyPair = parsePairKeyValue(entry);
93+
94+
if (keyPair !== undefined && keyPair.value.length > 0) {
95+
result[keyPair.key] = keyPair.value;
96+
}
97+
});
98+
}
99+
100+
return result;
100101
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import { parseKeyPairsIntoRecord } from '../../../src/baggage/utils';
19+
20+
describe('parseKeyPairsIntoRecord()', () => {
21+
it('returns an empty object when value is not a string', () => {
22+
assert.deepStrictEqual(parseKeyPairsIntoRecord(), {});
23+
});
24+
25+
it('returns an empty object when value is the empty string', () => {
26+
assert.deepStrictEqual(parseKeyPairsIntoRecord(''), {});
27+
});
28+
29+
it('parses a single key-value pair', () => {
30+
const value = 'key1=value1';
31+
const result = parseKeyPairsIntoRecord(value);
32+
assert.deepStrictEqual(result, { key1: 'value1' });
33+
});
34+
35+
it('parses multiple key-value pairs', () => {
36+
const value = 'key1=value1,key2=value2,key3=value3';
37+
const result = parseKeyPairsIntoRecord(value);
38+
assert.deepStrictEqual(result, {
39+
key1: 'value1',
40+
key2: 'value2',
41+
key3: 'value3',
42+
});
43+
});
44+
45+
it('ignores leading and trailing whitespace', () => {
46+
const value = ' key1=value1, key2=value2 ';
47+
const result = parseKeyPairsIntoRecord(value);
48+
assert.deepStrictEqual(result, { key1: 'value1', key2: 'value2' });
49+
});
50+
51+
it('filters out invalid pairs', () => {
52+
assert.deepStrictEqual(parseKeyPairsIntoRecord('key1,key2=value2'), {
53+
key2: 'value2',
54+
});
55+
});
56+
57+
it('filters out empty values', () => {
58+
assert.deepStrictEqual(parseKeyPairsIntoRecord('key1=,key2=value2'), {
59+
key2: 'value2',
60+
});
61+
});
62+
});

0 commit comments

Comments
 (0)