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
6 changes: 6 additions & 0 deletions packages/tpl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
index.js
index.d.ts
index.js.map
lib/*.js
lib/*.d.ts
lib/*.js.map
1 change: 0 additions & 1 deletion packages/tpl/index.js

This file was deleted.

3 changes: 3 additions & 0 deletions packages/tpl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import tpl = require('./lib/tpl');

export = tpl;
13 changes: 5 additions & 8 deletions packages/tpl/lib/tpl.js → packages/tpl/lib/tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ const interpolate = /(?<!{){([^{]+?)}/g;
* Will ignore double or triple braces like {{get}} or {{{helpername}}}
* Can handle escaped braces e.g. \\{\\{{helpername}\\}\\}
* But there's a simple bare minimum escaping needed to make {{{helpername}}} work e.g. {\\{{helpername}}}
*
*
* @param {String} string - string with optional {data properties}
* @param {Object} [data] - optional data to interpolate
* @returns {string} the interpolated string
*/
module.exports = (string, data) => {
function tpl(string: string, data?: object | null): string {
if (!data) {
return string;
}
Expand All @@ -25,8 +20,10 @@ module.exports = (string, data) => {
if (!(trimmed in data)) {
throw new ReferenceError(`${trimmed} is not defined`);
}
return data[trimmed];
return String((data as Record<string, unknown>)[trimmed]);
});
// Replace our swapped out left braces and any escaped right braces
return processedString.replace(/\\U\+007B/g, '{').replace(/\\}/g, '}');
};
}

export = tpl;
16 changes: 12 additions & 4 deletions packages/tpl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@
},
"files": [
"index.js",
"lib"
"index.d.ts",
"lib/*.js",
"lib/*.d.ts"
],
"main": "index.js",
"types": "index.d.ts",
"publishConfig": {
"access": "public"
},
"scripts": {
"dev": "echo \"Implement me!\"",
"test": "NODE_ENV=testing vitest run --coverage",
"dev": "tsc --watch --preserveWatchOutput --sourceMap",
"build": "tsc",
"prepare": "tsc",
"pretest": "pnpm run build",
"test": "pnpm run test:types && NODE_ENV=testing vitest run --coverage",
"test:types": "tsc -p tsconfig.test.json --noEmit",
"lint": "oxlint -c ../../.oxlintrc.json .",
"posttest": "pnpm run lint"
},
"dependencies": {},
"devDependencies": {
"sinon": "catalog:"
"@types/node": "24.13.1",
"typescript": "catalog:"
}
}
33 changes: 23 additions & 10 deletions packages/tpl/test/tpl.test.js → packages/tpl/test/tpl.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert/strict');
const tpl = require('../');
import assert from 'assert/strict';
import tpl from '../';

describe('tpl', function () {
it('Can handle a plain string', function () {
Expand All @@ -13,7 +13,20 @@ describe('tpl', function () {
const string = 'Go visit {url}';
const data = { url: 'https://example.com' };

let result = tpl(string, data);
const result = tpl(string, data);

assert.equal(result, 'Go visit https://example.com');
});

it('Can handle typed data objects', function () {
interface TemplateData {
url: string;
}

const string = 'Go visit {url}';
const data: TemplateData = { url: 'https://example.com' };

const result = tpl(string, data);

assert.equal(result, 'Go visit https://example.com');
});
Expand All @@ -24,7 +37,7 @@ describe('tpl', function () {
totalMs: '500',
};

let result = tpl(string, data);
const result = tpl(string, data);
assert.equal(result, '{{#get}} helper took 500ms to complete');
});

Expand All @@ -36,7 +49,7 @@ describe('tpl', function () {
totalMs: '500',
};

let result = tpl(string, data);
const result = tpl(string, data);
assert.equal(result, '{{#get}} helper took 500ms to complete');
});

Expand All @@ -46,7 +59,7 @@ describe('tpl', function () {
helperName: 'get',
totalMs: '500',
};
let result = tpl(string, data);
const result = tpl(string, data);
assert.equal(result, 'The {{{helperName}}} helper is not available.');
});

Expand All @@ -56,7 +69,7 @@ describe('tpl', function () {
helperName: 'get',
totalMs: '500',
};
let result = tpl(string, data);
const result = tpl(string, data);
assert.equal(result, 'The {{get}} helper is not available.');
});

Expand All @@ -66,7 +79,7 @@ describe('tpl', function () {
helperName: 'get',
totalMs: '500',
};
let result = tpl(string, data);
const result = tpl(string, data);
assert.equal(result, 'The {{get}} helper is not available.');
});

Expand All @@ -76,7 +89,7 @@ describe('tpl', function () {
helperName: 'get',
totalMs: '500',
};
let result = tpl(string, data);
const result = tpl(string, data);
assert.equal(result, 'The {{get}} helper is not available.');
});

Expand All @@ -86,7 +99,7 @@ describe('tpl', function () {
totalMs: '500',
};

let resultFn = () => {
const resultFn = () => {
tpl(string, data);
};
assert.throws(resultFn, /helperName is not defined/);
Expand Down
9 changes: 9 additions & 0 deletions packages/tpl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"sourceMap": false,
"types": []
},
"include": ["index.ts", "lib/**/*.ts"]
}
7 changes: 7 additions & 0 deletions packages/tpl/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": ["node", "vitest/globals"]
},
"include": ["index.ts", "lib/**/*.ts", "test/**/*.ts"]
}
14 changes: 14 additions & 0 deletions packages/tpl/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig, mergeConfig } from 'vitest/config';
import rootConfig from '../../vitest.config';

export default mergeConfig(
rootConfig,
defineConfig({
test: {
coverage: {
include: ['lib/**/*.js'],
exclude: ['**/*.ts', '**/*.d.ts'],
},
},
}),
);
Loading
Loading