key-value-string
Version:
A simple key-value string parser with zero dependencies
184 lines (126 loc) • 4.8 kB
Markdown
A simple key-value string parser with zero dependencies. Designed for single-line key-value strings with support for numbers, quoted strings, and arrays. Perfect for configuration strings, URL parameters, and command-line arguments.
Thoroughly tested with 200+ unit tests to ensure reliability and edge case handling.
```bash
npm install key-value-string
```
```bash
npm install -g key-value-string
```
After global installation, the `kvs` command will be available in your terminal.
```javascript
const { parseKeyValueString, toKeyValueString } = require('key-value-string');
// Parse key-value string
const result = parseKeyValueString('name=John;age=30;tags=frontend,backend;');
console.log(result);
// { name: 'John', age: 30, tags: ['frontend', 'backend'] }
// Convert object to key-value string
const kvString = toKeyValueString({ name: 'John', age: 30, active: true });
console.log(kvString);
// name=John;age=30;active=true;
```
```javascript
import { parseKeyValueString, toKeyValueString } from 'key-value-string';
const parsed = parseKeyValueString('server=localhost;port=3000;debug=true;');
console.log(parsed);
// { server: 'localhost', port: 3000, debug: true }
```
```html
<script src="https://unpkg.com/key-value-string/dist/index.umd.js"></script>
<script>
const { parseKeyValueString, toKeyValueString } = KeyValueString;
const config = parseKeyValueString('theme=dark;fontSize=14;showLineNumbers=true;');
console.log(config);
</script>
```
```html
<script type="module">
import { parseKeyValueString } from 'https://unpkg.com/key-value-string/dist/index.esm.js';
const settings = parseKeyValueString('width=800;height=600;fullscreen=false;');
console.log(settings);
</script>
```
**Note**: Install globally first with `npm install -g key-value-string`
```bash
kvs parse "name=John;age=30;tags=frontend,backend;"
kvs stringify '{"name":"John","age":30,"tags":["frontend","backend"]}'
kvs validate "name=John;age=30;"
kvs parse -f config.kvs
kvs stringify -f data.json
kvs --help
```
The parser is designed for **single-line key-value strings** (though multiline works, comments are not supported):
- **Key-value pairs**: `key=value;`
- **Numbers**: Automatically parsed (`age=30` → `{ age: 30 }`)
- **Quoted strings**: `name="John Doe";` for values with spaces or special characters
- **Arrays**: `tags=frontend,backend;` → `{ tags: ['frontend', 'backend'] }`
- **Null values**: `optional=;` → `{ optional: null }`
- **Empty arrays**: `tags=,;` → `{ tags: [null] }`
**Note**: Comments (`
Parses a key-value string into an object.
- **input**: The key-value string to parse
- **Returns**: Parsed object
- **Throws**: Error if the string is malformed
Converts an object to a key-value string.
- **obj**: The object to convert
- **Returns**: Key-value string representation
```typescript
interface KeyValueObject {
[]: string | number | null | Array<string | number | null>;
}
```
```javascript
// Parse application config string
const configString = 'host=localhost;port=3000;database="my_app_db";timeout=5000;features=auth,logging,cache;debug=true;';
const config = parseKeyValueString(configString);
// Use the config
console.log(`Server running on ${config.host}:${config.port}`);
// Output: Server running on localhost:3000
```
```javascript
// Parse URL-like parameters
const params = parseKeyValueString('user=123;action=edit;theme=dark;');
console.log(params); // { user: 123, action: 'edit', theme: 'dark' }
```
```javascript
// Convert environment-like variables
const env = {
NODE_ENV: 'production',
PORT: 8080,
FEATURES: ['auth', 'ssl', 'cache']
};
const envString = toKeyValueString(env);
console.log(envString); // NODE_ENV=production;PORT=8080;FEATURES=auth,ssl,cache;
```
- **UMD build**: Supports ES2015+ (IE11+ with polyfills)
- **ESM build**: Supports ES2020+ (modern browsers)
- **CommonJS build**: Node.js 14+
MIT
---
*Built with ❤️ and way too much coffee in a single night of pure vibecoding.*