Function parseKeyValue
Source: ParseKeyValue.ts
Parses a key-value expression such as key=value into a labeled tuple [key, value].
This function is useful for parsing simple configuration strings. It splits only at the first occurrence of the assign sign, so an expression like a=b=c correctly yields ['a', 'b=c'] instead of being split at every =.
Import
import { parseKeyValue } from '@litert/utils-string';Signature
function parseKeyValue(
expr: string,
opts?: IParseKeyValueOptions,
): [key: string, value: string] | null;Parameters
Parameter
expr: stringThe expression string to parse. Must contain the assign sign (e.g.,
key=value) for a non-nullresult to be returned.Parameter
opts: IParseKeyValueOptions(Optional)Optional settings to customize parsing behavior. See
IParseKeyValueOptionsfor the full list of available options.Option Default assignSign'='trimKeytruetrimValuetrue
Return Value
Returns a labeled tuple [key: string, value: string] containing the parsed key and value when the assign sign is found in expr.
Returns null when expr does not contain the assign sign at all.
Scoped Types
Interface IParseKeyValueOptions
Source: ParseKeyValue.ts
import type { IParseKeyValueOptions } from '@litert/utils-string';Options for the parseKeyValue function.
| Property | Type | Default | Description |
|---|---|---|---|
assignSign | string | '=' | The string used to separate the key and value. Change this for formats like key:value or key=>value. |
trimKey | boolean | true | Whether to trim whitespace from the key before returning it. |
trimValue | boolean | true | Whether to trim whitespace from the value before returning it. |
Examples
import { parseKeyValue } from '@litert/utils-string';
parseKeyValue('a=b'); // ['a', 'b']
parseKeyValue('a=b=c'); // ['a', 'b=c']
parseKeyValue('a = b', { trimKey: true, trimValue: true }); // ['a', 'b']
parseKeyValue('a = b', { trimKey: false, trimValue: false }); // ['a ', ' b']
parseKeyValue('a:b', { assignSign: ':' }); // ['a', 'b']
parseKeyValue('a=>b', { assignSign: '=>' }); // ['a', 'b']
parseKeyValue('abc'); // null