Skip to content

Commit 82d466d

Browse files
committed
Merge branch 'v2'
2 parents b0ff22a + 7117ea0 commit 82d466d

11 files changed

+362
-364
lines changed

.github/workflows/nodejs.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
pull_request:
9+
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
matrix:
17+
node-version: [12.x, 14.x, 16.x]
18+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
with:
23+
submodules: true
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v2
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
cache: 'npm'
29+
- run: npm ci
30+
- run: make clean all
31+
- run: npm test
32+

Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
.PHONY: all test publish clean
1+
.PHONY: check publish clean
22

3-
all: lib/grammar.js dist/index.js dist/classes.js
3+
all: lib/grammar.js dist/index.js
44

55
dist/%.js: lib/%.ts
66
@npx tsc
77

8+
check:
9+
@npx tsc --noEmit
10+
811
lib/grammar.js: lib/grammar.pegjs
9-
@npx pegjs lib/grammar.pegjs ./lib/grammar.js
12+
@npx peggy lib/grammar.pegjs
1013

1114
test: all
1215
@node ./test/index.js

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# URI Template
22

3-
[![Build Status](https://api.travis-ci.com/grncdr/uri-template.svg?branch=master&status=passed)](https://app.travis-ci.com/grncdr/uri-template)
3+
[![Node.js CI](https://github.com/grncdr/uri-template/actions/workflows/nodejs.yml/badge.svg)](https://github.com/grncdr/uri-template/actions/workflows/nodejs.yml)
44

55
This is a node.js implementation of the URI template draft standard
66
defined at http://tools.ietf.org/html/rfc6570
@@ -29,5 +29,6 @@ queryTpl.expand({
2929
// /search?q=Bigger%20office&prefer=Sterling%27s%20office&accept=Crane%27s%20office
3030
```
3131

32-
For more thorough coverage of the syntax, see `test.js` or the
33-
[RFC](http://tools.ietf.org/html/rfc6570).
32+
For more thorough coverage of the syntax, see the [RFC](https://datatracker.ietf.org/doc/html/rfc6570).
33+
34+
##

lib/ast.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { abort } from "process";
2+
3+
export interface Template {
4+
type: "template";
5+
parts: (Literal | Expression)[];
6+
}
7+
8+
export interface Literal {
9+
type: "literal";
10+
value: string;
11+
}
12+
13+
export interface Expression {
14+
type: "expression";
15+
operator: Operator;
16+
variables: Variable[];
17+
}
18+
19+
export type Operator = "/" | ";" | "." | "?" | "&" | "+" | "#" | "";
20+
21+
export interface Variable {
22+
type: "variable";
23+
name: string;
24+
modifier?: SubstrModifier | ExplodeModifier;
25+
extension?: string;
26+
}
27+
28+
export interface ExplodeModifier {
29+
type: "explode";
30+
}
31+
32+
export interface SubstrModifier {
33+
type: "substr";
34+
length: number;
35+
}
36+
37+
export function toString(
38+
node: Template | Literal | Expression | Variable
39+
): string {
40+
switch (node.type) {
41+
case "template":
42+
return node.parts.map(toString).join("");
43+
case "literal":
44+
return node.value;
45+
case "expression":
46+
return `{${node.operator}${node.variables.map(toString).join(",")}}`;
47+
case "variable":
48+
let out = node.name;
49+
if (node.modifier?.type == "explode") {
50+
out += "*";
51+
} else if (node.modifier?.type == "substr") {
52+
out += `:${node.modifier.length}`;
53+
}
54+
if (node.extension) {
55+
out += `(${node.extension})`;
56+
}
57+
return out;
58+
}
59+
}

0 commit comments

Comments
 (0)