Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ jobs:
steps:
# Set up
- name: Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7
with:
persist-credentials: false
submodules: true
- uses: yarnpkg/setup-action@c9778d0717d1b46cf29f06ae5ae5ad41c1bb1c1a
- name: Setup Node
uses: actions/setup-node@v6
uses: actions/setup-node@v7
with:
node-version: 24.x
registry-url: "https://registry.npmjs.org"
Expand Down Expand Up @@ -54,7 +56,7 @@ jobs:

# Release to GitHub Packages
- name: Setup Node for GitHub Packages
uses: actions/setup-node@v6
uses: actions/setup-node@v7
with:
node-version: 24.x
registry-url: "https://npm.pkg.github.com"
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ jobs:
# Set up

- name: Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7
with:
persist-credentials: false
fetch-depth: 0
fetch-tags: true
- uses: yarnpkg/setup-action@c9778d0717d1b46cf29f06ae5ae5ad41c1bb1c1a
- name: Setup Node
uses: actions/setup-node@v6
uses: actions/setup-node@v7
with:
node-version: 24.x

Expand All @@ -37,8 +39,8 @@ jobs:
run: |
yarn book
- name: Upload website
uses: actions/upload-pages-artifact@v4
uses: actions/upload-pages-artifact@v5
with:
path: documentation/book
- name: Deploy to GitHub Pages
uses: actions/deploy-pages@v4
uses: actions/deploy-pages@v5
23 changes: 18 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: yarnpkg/setup-action@c9778d0717d1b46cf29f06ae5ae5ad41c1bb1c1a
- uses: actions/setup-node@v7
with:
node-version: 24.x
cache: yarn
Expand All @@ -32,7 +35,7 @@ jobs:

- if: ${{ github.event_name == 'pull_request' }}
name: Review Dependencies
uses: actions/dependency-review-action@v4
uses: actions/dependency-review-action@v5

- name: 'Check for formatting errors (fix w/ "yarn prettier --write path/to/file")'
run: |
Expand All @@ -54,10 +57,12 @@ jobs:
- 26.x

steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v7
with:
submodules: true
- uses: actions/setup-node@v6
persist-credentials: false
- uses: yarnpkg/setup-action@c9778d0717d1b46cf29f06ae5ae5ad41c1bb1c1a
- uses: actions/setup-node@v7
with:
node-version: ${{matrix.node}}
cache: yarn
Expand All @@ -69,3 +74,11 @@ jobs:
- name: Run tests
run: |
yarn test

- name: Run kdl-test
run: |
gh release download -O- --repo kdl-org/kdl-test \
--pattern '*-linux-x86_64.tar.gz' \
| tar xvz

scripts/kdl-test.sh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/out
/lcov.info
/coverage
/kdl-test
934 changes: 0 additions & 934 deletions .yarn/releases/yarn-4.6.0.cjs

This file was deleted.

2 changes: 0 additions & 2 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.6.0.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"type": "module",
"private": true,
"sideEffects": false,
"packageManager": "yarn@4.6.0",
"packageManager": "yarn@4.18.0",
"exports": {
".": "./src/index.js",
"./json": "./src/json.js",
Expand Down
2 changes: 1 addition & 1 deletion scripts/book.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

cd $(dirname "$0")/..
cd "$(dirname "$0")/.."

if ! command -v mdbook >/dev/null 2>&1; then
echo "Please install mdbook first" >&2
Expand Down
1 change: 1 addition & 0 deletions scripts/coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -euo pipefail
cd "$(dirname "$0")/.."

if ! command -v genhtml &>/dev/null; then
# shellcheck disable=SC2016
echo 'Missing command genhtml, run `brew install lcov` or whatever alternative exists for your system' >&2
exit 1
fi
Expand Down
19 changes: 19 additions & 0 deletions scripts/kdl-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")/.."

if command -v kdl-test &>/dev/null; then
kdl_test=kdl-test
elif [[ -x ./kdl-test ]]; then
kdl_test="$PWD/kdl-test"
else
echo "Place kdl-test on the path or in the root of this repository" >&2
fi

exec "$kdl_test" run \
--skip valid/hex.kdl \
--skip valid/hex_int.kdl \
--skip valid/sci_notation_large.kdl \
--skip valid/sci_notation_small.kdl \
--decoder "$PWD/scripts/kdl-test/decoder.js"
159 changes: 159 additions & 0 deletions scripts/kdl-test/decoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/usr/bin/env node
// @ts-check

import {Writable} from "node:stream";
import {parse, Entry, Node} from "../../src/index.js";

/**
* @typedef {object} JsonStringValue
* @prop {"string"} type
* @prop {string} value
*/

/**
* @typedef {object} JsonNumberValue
* @prop {"number"} type
* @prop {string} value
*/

/**
* @typedef {object} JsonBooleanValue
* @prop {"boolean"} type
* @prop {"true" | "false"} value
*/

/**
* @typedef {object} JsonNullValue
* @prop {"null"} type
*/

/**
* @typedef {object} JsonValue
* @prop {string | null} type
* @prop {JsonStringValue | JsonNumberValue | JsonBooleanValue | JsonNullValue} value
*/

/**
* @typedef {object} JsonNode
* @prop {string | null} type
* @prop {string} name
* @prop {JsonValue[]} args
* @prop {Record<string, JsonValue>} props
* @prop {JsonNode[]} [children]
*/

/** @param {number} value */
function stringifyNumber(value) {
if (Number.isNaN(value)) {
return "nan";
}

if (!Number.isFinite(value)) {
return value > 0 ? "inf" : "-inf";
}

if (Number.isInteger(value)) {
return BigInt(value).toString() + ".0";
}

const str = value.toString();

const exponentIndex = str.indexOf("e");
if (exponentIndex === -1) {
return str;
}

const sign = str.charAt(0) === "-" ? "-" : "";
let exponent = Number.parseInt(str.slice(exponentIndex + 1));
let val = str.slice(sign.length, exponentIndex);

// Note this is far from general enough for a regular toString
// functionality for a number with an exponent, but in this scenario
// we're only thinking about numbers stringified to exponent notation
// by JavaScript's builtin Number.prototype.toString()
// --> proper scientific notation with a single non-zero digit before the dot
// + only happens for very large or very small numbers, so we don't have to
// consider the case where the exponent would result in a large number
// with digits behind the decimal point.

if (val.charAt(1) === ".") {
val = val.charAt(0) + val.slice(2);
exponent -= val.length - 1;
}

if (exponent >= 0) {
return `${sign}${val}${"0".repeat(exponent)}`;
} else {
return `${sign}0.${"0".repeat(-(exponent + val.length))}${val}`;
}
}

/**
* @param {Entry} entry
* @returns {JsonValue}
*/
function mapValue(entry) {
/** @type {JsonValue['value']} */
let value;
switch (typeof entry.value.value) {
case "string":
value = {type: "string", value: entry.value.value};
break;
case "boolean":
value = {type: "boolean", value: `${entry.value.value}`};
break;
case "number":
value = {type: "number", value: stringifyNumber(entry.value.value)};
break;
default:
value = {type: "null"};
}

return {
type: entry.getTag(),
value,
};
}

/**
* @param {Node} node
* @returns {JsonNode}
*/
function mapNode(node) {
return {
type: node.getTag(),
name: node.getName(),
args: node.getArgumentEntries().map(mapValue),
props: Object.fromEntries(
node
.getPropertyEntries()
.map((entry) => [entry.getName(), mapValue(entry)]),
),
children: node.children?.nodes.map(mapNode) ?? [],
};
}

try {
/** @type {Buffer[]} */
const inputParts = [];

process.stdin.pipe(
new Writable({
writev(chunks, cb) {
inputParts.push(...chunks.map(({chunk}) => chunk));

cb();
},
}),
);

await new Promise((resolve) => process.stdin.on("end", resolve));

process.stdout.write(
JSON.stringify(parse(Buffer.concat(inputParts)).nodes.map(mapNode)),
);
process.stdout.write("\n");
} catch (e) {
process.stderr.write(String(e));
process.exit(1);
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
version: 10
cacheKey: 10c0

"@bgotink/kdl-v1@npm:@bgotink/kdl@0.1.7":
Expand Down