Code Monkey home page Code Monkey logo

js-fast-vdf's Introduction

fast-vdf

Your average javascript KeyValues processing library, as fast as parsable!

Installation

npm i fast-vdf

Usage

import { vdf, KeyV, KeyVSet } from 'fast-vdf';

const root = vdf.parse(`
SteamAppId      620
SearchPaths
{
    Game        |gameinfo_path|.
    Game        portal2_dlc2
}
`);

console.log(root.value('SteamAppId'));
// 620

console.log(root.dir('SearchPaths').pair('Game').value);
// "portal2_dlc2"

try { root.pair('DoesntExist'); }
catch(e) { console.warn(e.message) }
// Pair with key "doesntexist" does not exist in set!

// Strict behaviour is enabled by default with all KeyVSet methods.
// Since this pair does not exist, this call throws an error.

console.log(root.pair('DoesntExist', null));

// The default value can be set to null to
// disable this behaviour, instead returning null.


root.dir('SearchPaths').factory()
    .pair('Game', 'portal2_dlc1')
    .pair('Game', 'portal2')
    .pair('Game', 'platform')
    .exit();

// Factory objects can be used to quickly create keyvalue structures.
// The above code is equivalent to the below:

const sp = root.dir('SearchPaths');
sp.add(new KeyV('Game', 'portal2_dlc1'));
sp.add(new KeyV('Game', 'portal2'));
sp.add(new KeyV('Game', 'platform'));


// After you've created your structure, you can dump it as a formatted
// string with the dump function.

root.dump({
    quote: 'auto',
    escapes: false
});

API

Breaking Changes

2.0.0

  • The types and multiline options now default to false.
  • The auto quoting mode has been split into auto and auto-typed.
    • auto behaves normally, quoting only values which strictly need to be quoted.
    • auto-typed allows fast-vdf to quote string values that might be confused with non-string values. (ex. "true", "123")
  • The KeyVRoot/KeyVSet .value(...) method has been reworked to be less strict.
  • Methods for reading type-strict values have been added to KeyV. (.int(...), .float(...), .string(), .bool(), .vector(...))

Imports

Note: This package, while written as an ES module, is compiled to CommonJS for backwards-compatibility. As such, the default export is emulated by including the contents of the vdf object in the main module.

import vdf from 'fast-vdf';             // vdf.parse(), vdf.json(), vdf.KeyV, vdf.KeyVSet, ...
import { vdf } from 'fast-vdf';         // vdf.parse(), vdf.json(), KeyV, KeyVSet, ...
import { parse } from 'fast-vdf';       // parse(), json(), KeyV, KeyVSet, ...

const vdf = require('fast-vdf');        // vdf.parse(), vdf.json(), vdf.KeyV, vdf.KeyVSet, ...
const { vdf } = require('fast-vdf');    // vdf.parse(), vdf.json(), KeyV, KeyVSet, ...
const { parse } = require('fast-vdf');  // parse(), json(), KeyV, KeyVSet, ...

Functions

vdf.parse(data: string, options?: SharedParseOptions): KeyVRoot

Parses data into a tree of KeyV objects.

Parameters

data The string to parse.

options The parser configuration.

vdf.json(data: string, env: Record<string, boolean>, options?: SharedParseOptions): Object

Parses data into a regular javascript object.

Parameters

data The string to parse.

env An object containing condition values. (Ex. {'$XBOX': false} will cause keys with the condition [$XBOX] to be ignored.)

options The parser configuration.

core.parse(text: string, options: ParseOptions): void

The internal API used by the parse.xyz functions.

Parameters

text The string to parse.

options The parser configuration.

Types

SharedParseOptions

interface SharedParseOptions {
    escapes?:    boolean; // true
    multilines?: boolean; // true
    types?:      boolean; // true
}

DumpFormatOptions

interface DumpFormatOptions {
    indent?:  string;                       // '\t'
    quote?:   'always'|'auto'|'auto-typed'; // 'always'
    escapes?: boolean;                      // true
}

ParseOptions

interface ParseOptions {
    on_key:     (key: string, value: string, query?: string) => void;
    on_enter:   (key: string) => void;
    on_exit:    () => void;
    escapes:    boolean;
    multilines: boolean;
    types:      boolean;
}

js-fast-vdf's People

Contributors

koerismo avatar tsa96 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

tsa96

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.