Package detail

kdljs

kdl-org11.4kMIT0.3.0

KDL parser and serializer.

kdl, parser, serializer

readme

KDL-JS

A JavaScript library for the KDL Document Language.

Install

npm install kdljs

Usage

Parsing

const { parse } = require('kdljs')
parse(`
// Nodes can be separated into multiple lines
title \
  "Some title"
`)

// [
//   {
//     name: 'title',
//     properties: {},
//     values: [ 'Some title' ],
//     children: [],
//     tags: {
//       name: undefined,
//       properties: {},
//       values: [ undefined ]
//     }
//   }
// ]

parse(`
// Files must be utf8 encoded!
smile "😁"
`)

// [
//   {
//     name: 'smile',
//     properties: {},
//     values: [ '😁' ],
//     children: [],
//     tags: {
//       name: undefined,
//       properties: {},
//       values: [ undefined ]
//     }
//   }
// ]

parse(`
// Instead of anonymous nodes, nodes and properties can be wrapped
// in "" for arbitrary node names.
"!@#$@$%Q#$%~@!40" "1.2.3" "!!!!!"=#true
`)

// [
//   {
//     name: '!@#$@$%Q#$%~@!40',
//     properties: { '!!!!!': true },
//     values: [ '1.2.3' ],
//     children: [],
//     tags: {
//       name: undefined,
//       properties: { '!!!!!': undefined },
//       values: [ undefined ]
//     }
//   }
// ]

parse(`
// The following is a legal bare identifier:
foo123~!@$%^&*.:'|?+ "weeee"

// And you can also use unicode!
ノード お名前="☜(゚ヮ゚☜)"
`)

// [
//   {
//     name: "foo123~!@$%^&*.:'|?+",
//     properties: {},
//     values: [ 'weeee' ],
//     children: [],
//     tags: {
//       name: undefined,
//       properties: {},
//       values: [ undefined ]
//     }
//   },
//   {
//     name: 'ノード',
//     properties: { 'お名前': '☜(゚ヮ゚☜)' },
//     values: [],
//     children: [],
//     tags: {
//       name: undefined,
//       properties: {},
//       values: []
//     }
//   }
// ]

parse(`
// kdl specifically allows properties and values to be
// interspersed with each other, much like CLI commands.
foo bar=#true "baz" quux=#false 1 2 3
`)

// [
//   {
//     name: 'foo',
//     properties: { bar: true, quux: false },
//     values: [ 'baz', 1, 2, 3 ],
//     children: [],
//     tags: {
//       name: undefined,
//       properties: { bar: undefined, quux: undefined },
//       values: [ undefined, undefined, undefined, undefined ]
//     }
//   }
// ]

parse(`
// kdl also allows for annotationg values with types, and
// for denoting relations between nodes.
package {
  (author)person contact=(email)"example@example.org"
  (contributor)person homepage=(url)"https://example.org/example"
}
`)

// [
//   {
//     name: 'package',
//     properties: {},
//     values: [],
//     children: [
//       {
//         name: 'person',
//         properties: { contact: 'example@example.org' },
//         values: [],
//         children: [],
//         tags: {
//           name: 'author',
//           properties: { contact: 'email' },
//           values: []
//         }
//       },
//       {
//         name: 'person',
//         properties: { homepage: 'https://example.org/example' },
//         values: [],
//         children: [],
//         tags: {
//           name: 'contributor',
//           properties: { homepage: 'url' },
//           values: []
//         }
//       }
//     ],
//     tags: { name: undefined, properties: {}, values: [] }
//   }
// ]

Querying

const { parse, query } = require('kdljs')

const { output: document } = parse(`package {
    name "foo"
    version "1.0.0"
    dependencies platform="windows" {
        winapi "1.0.0" path="./crates/my-winapi-fork"
    }
    dependencies {
        miette "2.0.0" dev=#true
    }
}`)

query(document, 'package >> name') // or
query(document, 'top() > package >> name')

// [
//   {
//     name: 'name',
//     values: ['foo'],
//     ...
//   }
// ]

query(document, 'dependencies')

// [
//   {
//     name: 'dependencies',
//     properties: { platform: 'windows' },
//     ...
//   },
//   {
//     name: 'dependencies',
//     ...
//   }
// ]

query(document, 'dependencies[platform]') // or
query(document, 'dependencies[prop(platform)]')

// [
//   {
//     name: 'dependencies',
//     properties: { platform: 'windows' },
//     ...
//   }
// ]

query(document, 'dependencies > []')

// [
//   {
//     name: 'winapi',
//     properties: { path: './crates/my-winapi-fork' },
//     values: [ '1.0.0' ],
//     ...
//   },
//   {
//     name: 'miette',
//     properties: { dev: 'true' },
//     values: [ '2.0.0' ],
//     ...
//   }
// ]

// MAP OPERATOR
// ============

query(document, 'package >> name => val()')
// ['foo'].

query(document, 'dependencies[platform] => platform')
// ['windows']

query(document, 'dependencies > [] => (name(), val(), path)')
// [('winapi', '1.0.0', './crates/my-winapi-fork'), ('miette', '2.0.0', None)]

query(document, 'dependencies > [] => (name(), values(), props())')
// [('winapi', ['1.0.0'], {'platform': 'windows'}), ('miette', ['2.0.0'], {'dev': true})]

Formatting

const { format } = require('kdljs')

format([
  {
    name: 'title',
    properties: {},
    values: [ 'Some title' ],
    children: [],
    tags: { properties: {}, values: [] }
  },
  {
    name: 'smile',
    properties: {},
    values: [ '😁' ],
    children: [],
    tags: { properties: {}, values: [] }
  },
  {
    name: '!@#$@$%Q#$%~@!40',
    properties: { '!!!!!': true },
    values: [ '1.2.3' ],
    children: [],
    tags: { properties: {}, values: [] }
  },
  {
    name: "foo123~!@#$%^&*.:'|/?+",
    properties: {},
    values: [ 'weeee' ],
    children: [],
    tags: { properties: {}, values: [] }
  },
  {
    name: 'ノード',
    properties: { 'お名前': '☜(゚ヮ゚☜)' },
    values: [],
    children: [],
    tags: { properties: {}, values: [] }
  },
  {
    name: 'foo',
    properties: { bar: true, quux: false },
    values: [ 'baz', 1, 2, 3 ],
    children: [],
    tags: { properties: {}, values: [] }
  }
])

`title "Some title"
smile "😁"
"!@#$@$%Q#$%~@!40" "1.2.3" !!!!!=true
foo123~!@#$%^&*.:'|/?+ "weeee"
ノード お名前="☜(゚ヮ゚☜)"
foo "baz" 1 2 3 bar=true quux=false
`

License

The code is available under the MIT license. The example above is made available from https://github.com/kdl-org/kdl under Creative Commons Attribution-ShareAlike 4.0 International. The submodule in test/kdl4j is licensed according to its LICENSE.md file.

changelog

0.3.0 (2025-01-08)

Bug Fixes

  • formatter: update to KDL 2.0 (e2b73c5)
  • parser: add vertical tab (0x0B) as newline (cdccd9f)
  • parser: add whitespace escapes to quoted strings (d04352c)
  • parser: allow bare identifier strings (d531b07)
  • parser: disallow banned identifier values (a867a74)
  • parser: disallow escaped unicode scalar values (401e612)
  • parser: disallow more literal code points (c099947)
  • parser: disallow more numeric-like bare identifiers (8ba3e15)
  • parser: disallow some literal code points (5256331)
  • parser: fix disallowed code point regexes (2cf1c39)
  • parser: fix escline behavior (89e0585)
  • parser: fix handling of surrogate pairs (e191c69)
  • parser: fix parsing of multiline strings (fcddb84)
  • parser: fix slashdash behavior (f033654)
  • parser: fix slashdash behavior (622fbd8)
  • parser: fix unterminated nodes at the end of children blocks (48b4307)
  • parser: implement bare identifier strings (846f7c0)
  • parser: implement float keywords (7250cd1)
  • parser: implement multiline strings (127f9a2)
  • parser: only allow U+FEFF at start of file (e1e3769)
  • parser: remove '#' as identifier character (c632bd1)
  • parser: remove raw string prefix (a4ffec2)
  • parser: un-reserve ",", "<", and ">" from identifier strings (375d8b8)
  • parser: update boolean/null literals (a0ccaee)
  • parser: update escaped characters for quoted strings (89eb557)
  • parser: update optional whitespace limitations (7d12d1d)
  • query: update KQL to 2.0 (719f966)
  • types: update to KDL 2.0 (3712172)

chore

  • bump development dependencies (f07cc25)

Code Refactoring

BREAKING CHANGES

  • This library is now ESM-only.
  • Drop support for Node.js v16

0.2.0 (2023-10-16)

Bug Fixes

  • types: remove kdljs typescript namespace (eb85a26), closes #19

BREAKING CHANGES

  • types: for typescript users.

0.1.5 (2022-09-26)

Bug Fixes

  • parser: align type annotations with values (a6c8694)
  • validator: validate node type annotations (1cb05ad)

0.1.4 (2022-04-25)

Features

  • queryEngine: support tags (832ccb4)

0.1.3 (2022-04-25)

Bug Fixes

  • formatter: fix node tag escapes (7ab1bef)
  • parser: fix 6e59505 (93856e5)
  • parser: force to parse until EOF (6e59505)
  • parser: handle boolean/null prefix in identifier (a274ade), closes #12
  • parser: handle lexer errors (ec672d4), closes #11 #10
  • parser: limit unicode escapes (1981017), closes #10

Features

  • fully support type annotations (63a008f)

0.1.2 (2022-04-23)

Bug Fixes

  • fix require stack (c94f5c8)
  • parser: correct BOM from U+FFEF to U+FEFF (f07927e)

Features

  • queryEngine: support queries (ec79f78)

0.1.1 (2021-09-16)

Bug Fixes

  • formatter: fix bare idents in output (09a6ba6)

0.1.0 (2021-09-16)

Bug Fixes

  • parser: allow line comment as node terminator (fb8d9e1)
  • parser: do not include non ident chars in idents (#8) (e5e38ef)
  • parser: treat standalone carriage return as newline (#7) (6f00292)

0.1.0-rc.2 (2021-09-11)

Bug Fixes

  • formatter: remove debug statement (95fd47f)
  • parser: allow _ in decimal parts of numbers (541ab30)
  • parser: allow escaped forward slash (159d9ba)
  • parser: allow line comment at EOF (1c72d65)
  • parser: allow signed non-decimal integers (2f0ab9b)
  • parser: fix node-space and whitespace (54ccc28)
  • parser: require one hex character in \u{} (0d6304b)

Features

  • parser: support (type) annotation (54dbb53)

0.1.0-rc.1 (2021-09-03)

Bug Fixes

  • parser: fix multiline comments behavior (48c2bba)

Features

  • validator: extract document validator (9ec20f9)

0.1.0-rc.0 (2021-08-31)

  • feat(formatter)!: support KDL serialization (4c4c94e)

Bug Fixes

  • parser: allow raw strings as identifiers (8cea257)
  • parser: allow true, false, null as identifiers (3813139)
  • parser: fix edge cases in node-space behavior (441c967)
  • parser: parse nodes with only node-space (de00d21)
  • ts: update types (3049abe)

Features

  • formatter: support same output options as kdl4j (d37df28)
  • ts: update ts types for output formatting (c3dd78d)

BREAKING CHANGES

  • module exports is no longer the function 'parse', but an object with two entries: 'parse', and 'format'.

0.0.3 (2021-02-24)

Bug Fixes

  • allow number separators ('_') in decimals (412a36b)
  • change radix-16 prefix to 'x' (1650427)
  • parse integer before float (15f2d6d)
  • parser: ignore leading zero in base-10 ints (c4dc96e)

Features

0.0.2 (2020-12-22)

0.0.1 (2020-12-22)

Bug Fixes

Features

  • parser: implement chevrotain parser (40c0bdb)