valve-kv
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

Valve KV for Node

Node.js implementation of serialization and deserialization of Valve's KeyValues (KV or VDF) format.

This parser is based on PHPValveKV.

Installation

npm install valve-kv

Example deserialize usage

Deserialize a string

import { deserialize } from "valve-kv";

const kv = `"A" 
            { 
                "B"  "C" 
            }`;
const kvObject = deserialize(kv);
const b = kvObject["A"]["B"]; // string "C"

Deserialize a file (supports #base includes):

import { deserializeFile } from "valve-kv";

const itemsFileObject = deserializeFile("items.txt");
// Default encoding is utf-8, but it can be specified
const utf16FileObject = deserializeFile("utf16File.txt", "utf16le");

Example serialize usage

Serialize an object to KV:

import { serialize } from "valve-kv";

const myObj = { A: { B: "C" } };
const kv = serialize(myObj);
/* kv value:
"A"
{
    "B"    "C"
}
*/

Serialize arrays to KV:

import { serialize } from "valve-kv";

const myObj = { A: ["C", "D", "E"] };
const kv = serialize(myObj);
/* kv value:
"A"
{
    "1"    "C"
    "2"    "D"
    "3"    "E"
}
*/

Available methods

+ deserialize(kvstring, [encoding = utf8])

Deserialize a string to a KVObject.

+ deserializeFile(filepath, [encoding = utf8])

Deserialize a file to a KVObject. This supports the usage of #base includes in the file.

+ serialize(kvobject)

Serialize an object to a KV string.

+ isKvObject(kvValue)

Check if a KVValue (could be object or basic value) is a KVObject.

+ arrayToKvObject(array)

Transform an array into a KV object. Example:

const kvobject = arrayToKvObject(["A", "B", "C"]);
// { ["1"]: "A", ["2"]: "B", ["3"]: "C" }

+ arrayFromKvObject(array)

Transform a KV object to an array. Example:

const kvobject = arrayFromKvObject({ ["1"]: "A", ["2"]: "B", ["3"]: "C" });
// ["A", "B", "C"]

Package Sidebar

Install

npm i valve-kv

Weekly Downloads

2

Version

1.2.1

License

MIT

Unpacked Size

18.4 kB

Total Files

8

Last publish

Collaborators

  • perryvw