npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vdf-parser

v1.2.1

Published

Package for (de)serialization of Valve's KeyValue format (VDF)

Downloads

815

Readme

vdf-parser

vdf-parser is a library that can convert VDF to JSON and vice versa.

It is mostly based on rossengeorgiev/vdf-parser and includes some features inspired by RoyalBingBong/vdfplus (which lacked some features supported by the former...), but contains many new features and fixes.

Format: https://developer.valvesoftware.com/wiki/KeyValues

VDF may contain comments. However, they are not preserved during decoding.

Features

  • Supports unquoted keys and values
  • Supports keys that appear multiple times by "arrayifying" them – whether they contain child objects or other values
  • Supports uppercase characters in keys and values (and unquoted floats in values)
  • Supports value type recognition and automatic conversion (booleans, integers, floats)
  • Supports non well formed objects (missing newlines), for example: "1" { "label" "#SFUI_WinMatchColon" "value" "#SFUI_Rounds" }
  • Supports conditionals (ex. [$WIN32||$X360])
  • Includes TypeScript types

Methods

/**
 * Parse a VDF string into a JavaScript object
 * @param text VDF text
 * @param options Parsing options. Accepts a boolean for backwards compatibility ("types" option defaulting to true)
 * @returns Parsed object
 */
export function parse<T>(text: string, options?: VDFParseOptions | boolean): T;

/**
 * Parse a JavaScript object into a VDF string
 * @param obj The object to stringify
 * @param options Parsing options. Accepts a boolean for backwards compatibility ("pretty" option defaulting to false)
 * @returns VDF string
 */
export function stringify(obj: object, options?: VDFStringifyOptions | boolean): string;

where options are as follows:

interface VDFParseOptions {
    /**
     * Attempt to automatically convert numbers and booleans to their correct types, defaults to true
     * @default true
     */
    types: boolean;

    /**
     * Arrayify the values if they appear multiple times.
     * Enabled by default, because Source does support multiple values with the same key (as separate entries).
     * One may want to disable it if they expect a single value and their code is not prepared for different cases.
     * In such case, the existing text value would be replaced with the new one, and existing object patched with the new values.
     * @default true
     */
    arrayify: boolean;

    /**
     * If defined, conditionals will be taken into account while parsing the VDF.
     * Provide a list of defined conditionals without leading dollar sign and any found conditionals will be validated against this list.
     * If you provide an empty array, everything requiring any conditional defined will be dropped.
     * Conditions in VDF are processed from left to right.
     * See README and test.js for examples of usage.
     */
    conditionals?: string[];
}

interface VDFStringifyOptions {
    /**
     * Add indentation to the resulting text, defaults to false
     * @default false
     */
    pretty: boolean;

    /**
     * Indent with the following characters, defaults to a tabulator, requires "pretty" to be set to true
     * @default "\t"
     */
    indent: string;
}

Conditionals

This library has support for conditionals. For example, if you parse the following text:

"test1" "test" [$WIN32||$X360]  // preserved
"test2" "test" [!$PS3]          // preserved
"test3" "test" [!$WIN32&&!$OSX] // removed
"test4" "test" [$X360]          // removed

with the following line:

VDF.parse(text, { conditionals: ['WIN32'] });

the first two lines will be preserved and the last two ones will get removed.

The conditional strings must follow the format, be written with capitalized letters, start with a dollar sign and contain no spaces, otherwise parsing will fail. You can open an issue if you encounter any valid VDF file in the wild that does not meet these conditions.

Installation

npm install vdf-parser

Usage

const VDF = require('vdf-parser');
var parsed = VDF.parse(text);

or

import * as VDF from 'vdf-parser';
let parsed = VDF.parse(text);