Skip to content

Commit 2aab167

Browse files
Add TypeScript support (#321)
1 parent e06aaf3 commit 2aab167

File tree

8 files changed

+579
-6
lines changed

8 files changed

+579
-6
lines changed

.github/workflows/ci.yml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,45 @@ on:
1111
jobs:
1212
test:
1313
runs-on: ubuntu-24.04
14+
strategy:
15+
matrix:
16+
node-version: ['18', '20', '22']
1417
steps:
1518
- name: Checkout code
1619
uses: actions/checkout@v3
17-
- name: Set up Node.js
20+
- name: Set up Node.js ${{ matrix.node-version }}
1821
uses: actions/setup-node@v3
1922
with:
20-
node-version: '22'
23+
node-version: ${{ matrix.node-version }}
2124
- name: Install dependencies
2225
run: npm install
2326
- name: Run lint
2427
run: npm run lint
2528
- name: Run tests
26-
run: npm run test
29+
run: npm run test
30+
31+
typescript:
32+
runs-on: ubuntu-24.04
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v3
36+
- name: Set up Node.js
37+
uses: actions/setup-node@v3
38+
with:
39+
node-version: '22'
40+
- name: Install dependencies
41+
run: npm install
42+
- name: Validate TypeScript declarations
43+
run: npm run validate-types
44+
- name: Type check TypeScript example
45+
run: npm run type-check
46+
- name: Test TypeScript example compilation and execution
47+
run: |
48+
npx tsc example.ts
49+
node example.js
50+
rm example.js
51+
- name: Verify TypeScript can consume the package
52+
run: |
53+
echo "import osrmTextInstructions = require('./index'); const compiler = osrmTextInstructions('v5'); console.log('TypeScript import works!');" > test-import.ts
54+
npx tsc --noEmit test-import.ts
55+
rm test-import.ts

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Change Log
22
All notable changes to this project will be documented in this file. For change log formatting, see http://keepachangelog.com/
33

4+
## Unreleased
5+
- Add TypeScript support. [#321](https://github.com/Project-OSRM/osrm-text-instructions/pull/321)
6+
47
## 0.15.0 2024-03-03
58

69
- This package now requires Node 16 and above. [#312](https://github.com/Project-OSRM/osrm-text-instructions/pull/312)

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ OSRM Text Instructions is a Node.js library that transforms route data generated
88
* **Customizable**: Flexible options allow you to format and tweak the results to your liking.
99
* **Cross-platform**: A data-driven approach facilitates implementations in other programming languages. OSRM Text Instructions is also available [in Swift and Objective-C](https://github.com/Project-OSRM/osrm-text-instructions.swift/) (for iOS, macOS, tvOS, and watchOS) and [in Java](https://github.com/Project-OSRM/osrm-text-instructions.java/) (for Android and Java SE).
1010
* **Well-tested**: A data-driven test suite ensures compatibility across languages and platforms.
11+
* **TypeScript Support**: Full TypeScript type definitions are included for enhanced development experience.
1112

1213
## Usage
1314

@@ -24,6 +25,32 @@ response.legs.forEach(function(leg) {
2425
});
2526
```
2627

28+
### TypeScript Usage
29+
30+
```typescript
31+
import osrmTextInstructions = require('osrm-text-instructions');
32+
33+
const compiler = osrmTextInstructions('v5');
34+
35+
const step: osrmTextInstructions.RouteStep = {
36+
maneuver: {
37+
type: 'turn',
38+
modifier: 'left'
39+
},
40+
name: 'Main Street'
41+
};
42+
43+
const options: osrmTextInstructions.CompileOptions = {
44+
legCount: 2,
45+
legIndex: 0,
46+
formatToken: (token: string, value: string) => {
47+
return token === 'way_name' ? `<strong>${value}</strong>` : value;
48+
}
49+
};
50+
51+
const instruction = compiler.compile('en', step, options);
52+
```
53+
2754
If you are unsure if the user's locale is supported by osrm-text-inustrctions, use [@mapbox/locale-utils](https://github.com/mapbox/locale-utils) for finding the best fitting language.
2855

2956
#### Parameters `require('osrm-text-instructions')(version)`

example.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Example usage of osrm-text-instructions with TypeScript
2+
3+
import osrmTextInstructions = require('./index');
4+
5+
// Initialize the compiler for OSRM v5
6+
const compiler = osrmTextInstructions('v5');
7+
8+
// Example route step data
9+
const step: osrmTextInstructions.RouteStep = {
10+
maneuver: {
11+
type: 'turn',
12+
modifier: 'left',
13+
bearing_after: 90
14+
},
15+
name: 'Main Street',
16+
ref: 'A1',
17+
mode: 'driving',
18+
intersections: [
19+
{
20+
lanes: [
21+
{ valid: false },
22+
{ valid: true }
23+
]
24+
}
25+
]
26+
};
27+
28+
// Compile options
29+
const options: osrmTextInstructions.CompileOptions = {
30+
legCount: 2,
31+
legIndex: 0,
32+
classes: ['primary'],
33+
formatToken: (token: string, value: string) => {
34+
if (token === 'way_name') {
35+
return `<strong>${value}</strong>`;
36+
}
37+
return value;
38+
}
39+
};
40+
41+
try {
42+
// Generate instruction
43+
const instruction = compiler.compile('en', step, options);
44+
console.log('Instruction:', instruction);
45+
46+
// Get way name
47+
const wayName = compiler.getWayName('en', step, options);
48+
console.log('Way name:', wayName);
49+
50+
// Get direction from degree
51+
const direction = compiler.directionFromDegree('en', 90);
52+
console.log('Direction:', direction);
53+
54+
// Ordinalize number
55+
const ordinal = compiler.ordinalize('en', 1);
56+
console.log('Ordinal:', ordinal);
57+
58+
// Generate lane configuration
59+
const laneConfig = compiler.laneConfig(step);
60+
console.log('Lane config:', laneConfig);
61+
62+
// Capitalize first letter
63+
const capitalized = compiler.capitalizeFirstLetter('en', 'hello world');
64+
console.log('Capitalized:', capitalized);
65+
66+
// Access abbreviations
67+
const abbreviations = compiler.abbreviations;
68+
console.log('Available abbreviations:', Object.keys(abbreviations));
69+
70+
} catch (error) {
71+
console.error('Error:', error);
72+
}
73+
74+
// Example with different maneuver types
75+
const departStep: osrmTextInstructions.RouteStep = {
76+
maneuver: {
77+
type: 'depart',
78+
bearing_after: 0
79+
},
80+
name: 'Start Street'
81+
};
82+
83+
const arriveStep: osrmTextInstructions.RouteStep = {
84+
maneuver: {
85+
type: 'arrive',
86+
modifier: 'right'
87+
},
88+
name: 'Destination Avenue'
89+
};
90+
91+
const roundaboutStep: osrmTextInstructions.RouteStep = {
92+
maneuver: {
93+
type: 'roundabout',
94+
modifier: 'right',
95+
exit: 2
96+
},
97+
rotary_name: 'Main Roundabout'
98+
};
99+
100+
// Compile different instruction types
101+
const departInstruction = compiler.compile('en', departStep);
102+
const arriveInstruction = compiler.compile('en', arriveStep, { waypointName: 'Home' });
103+
const roundaboutInstruction = compiler.compile('en', roundaboutStep);
104+
105+
console.log('Depart:', departInstruction);
106+
console.log('Arrive:', arriveInstruction);
107+
console.log('Roundabout:', roundaboutInstruction);

0 commit comments

Comments
 (0)