Skip to content

Commit 75b6573

Browse files
committed
chore(examples): add "typescript.module" demo;
- Similar to "typescript" example, but uses `"type": "module"` and source files only include ".js" files with ESM syntax. This matches the scenario(s) presented in #123 Also includes two approaches for solving this problem; see npm "scripts" section. Closes #123
1 parent 44a437d commit 75b6573

File tree

8 files changed

+112
-0
lines changed

8 files changed

+112
-0
lines changed

examples/typescript.module/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

examples/typescript.module/loadr.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// NOTE: used for "test:alt" script only
2+
// @see https://github.com/lukeed/loadr
3+
export const loaders = ['tsm'];
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"private": true,
3+
"type": "module",
4+
"scripts": {
5+
"test": "tsm node_modules/uvu/bin.js tests",
6+
"test:alt": "loadr -- uvu tests"
7+
},
8+
"devDependencies": {
9+
"loadr": "^0.1.1",
10+
"tsm": "^2.0.0",
11+
"uvu": "^0.5.1"
12+
}
13+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} a
3+
* @param {number} b
4+
*/
5+
export const sum = (a, b) => a + b;
6+
7+
/**
8+
* @param {number} a
9+
* @param {number} b
10+
*/
11+
export const div = (a, b) => a / b;
12+
13+
/**
14+
* @param {number} a
15+
* @param {number} b
16+
*/
17+
export const mod = (a, b) => a % b;
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {string} str
3+
*/
4+
export function capitalize(str) {
5+
return str[0].toUpperCase() + str.substring(1);
6+
}
7+
8+
/**
9+
* @param {string} str
10+
*/
11+
export function dashify(str) {
12+
return str.replace(/([a-zA-Z])(?=[A-Z\d])/g, '$1-').toLowerCase();
13+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
import * as math from '../src/math.js';
4+
5+
test('sum', () => {
6+
assert.type(math.sum, 'function');
7+
assert.is(math.sum(1, 2), 3);
8+
assert.is(math.sum(-1, -2), -3);
9+
assert.is(math.sum(-1, 1), 0);
10+
});
11+
12+
test('div', () => {
13+
assert.type(math.div, 'function');
14+
assert.is(math.div(1, 2), 0.5);
15+
assert.is(math.div(-1, -2), 0.5);
16+
assert.is(math.div(-1, 1), -1);
17+
});
18+
19+
test('mod', () => {
20+
assert.type(math.mod, 'function');
21+
assert.is(math.mod(1, 2), 1);
22+
assert.is(math.mod(-3, -2), -1);
23+
assert.is(math.mod(7, 4), 3);
24+
});
25+
26+
test.run();
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
import * as utils from '../src/utils.js';
4+
5+
test('capitalize', () => {
6+
assert.type(utils.capitalize, 'function');
7+
assert.is(utils.capitalize('hello'), 'Hello');
8+
assert.is(utils.capitalize('foo bar'), 'Foo bar');
9+
});
10+
11+
test('dashify', () => {
12+
assert.type(utils.dashify, 'function');
13+
assert.is(utils.dashify('fooBar'), 'foo-bar');
14+
assert.is(utils.dashify('FooBar'), 'foo-bar');
15+
assert.is(utils.dashify('foobar'), 'foobar');
16+
});
17+
18+
test.run();
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"allowJs": true,
5+
"target": "esnext",
6+
"noImplicitAny": true,
7+
"moduleResolution": "node",
8+
"forceConsistentCasingInFileNames": true,
9+
"alwaysStrict": true,
10+
"module": "esnext",
11+
"checkJs": true,
12+
"noEmit": true
13+
},
14+
"include": [
15+
"src",
16+
"tests"
17+
],
18+
"exclude": [
19+
"node_modules"
20+
]
21+
}

0 commit comments

Comments
 (0)