Skip to content

Commit

Permalink
Improve tree shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
omrilotan committed Oct 24, 2024
1 parent f8a6162 commit c39c32a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 38 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 3.1.2 2023-09-12

## Improvement

Optimise tree shaking: Omit "falvoured" variables if not used.

# 3.1.1 2023-01-26

## Enhancement
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "paraphrase",
"version": "3.1.1",
"version": "3.1.2",
"description": "🧩 Create flavoured string template interpolation",
"keywords": [
"string",
Expand Down Expand Up @@ -55,7 +55,7 @@
},
"devDependencies": {
"@types/jest": "^29.4.0",
"esbuild": "^0.19.2",
"esbuild": "^0.24.0",
"jest": "^29.4.0",
"prettier": "^3.0.3",
"ts-jest": "^29.0.5"
Expand Down
46 changes: 24 additions & 22 deletions src/flavours/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
export const flavours: Record<string, RegExp> = {
/**
* Template: 'Hello, ${name}'
*/
dollar: /\${([^{}]*)}/gm,
/**
* Template: 'Hello, {{name}}'
*/
double: /{{([^{}]*)}}/gm,
/**
* Template: 'Hello, {name}'
*/
single: /{([^{}]*)}/gm,
/**
* Template: 'Hello, #{name}'
*/
hash: /#{([^{}]*)}/gm,
/**
* Template: 'Hello, %{name}'
*/
percent: /%{([^{}]*)}/gm,
};
/**
* Template: 'Hello, ${name}'
*/
export const dollarPattern = /\${([^{}]*)}/gm;

/**
* Template: 'Hello, {{name}}'
*/
export const doublePattern = /{{([^{}]*)}}/gm;

/**
* Template: 'Hello, {name}'
*/
export const singlePattern = /{([^{}]*)}/gm;

/**
* Template: 'Hello, #{name}'
*/
export const hashPattern = /#{([^{}]*)}/gm;

/**
* Template: 'Hello, %{name}'
*/
export const percentPattern = /%{([^{}]*)}/gm;
32 changes: 19 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { notate } from "./notate/index";
import { isObject } from "./isObject/index";
import { flavours } from "./flavours/index";
import {
dollarPattern,
doublePattern,
percentPattern,
hashPattern,
singlePattern,
} from "./flavours/index";

/**
* Valid types of results for the interpolated string
Expand Down Expand Up @@ -104,8 +110,8 @@ export function paraphrase(
return VALID_RESULT_TYPES.includes(typeof replacement as any)
? replacement
: options.clean
? ""
: haystack;
? ""
: haystack;
}

const result = (patterns as RegExp[]).reduce(
Expand All @@ -126,15 +132,15 @@ export function paraphrase(
return phraser as Phraser;
}

export const dollar = paraphrase(flavours.dollar);
export const double = paraphrase(flavours.double);
export const single = paraphrase(flavours.single);
export const percent = paraphrase(flavours.percent);
export const hash = paraphrase(flavours.hash);
export const dollar = paraphrase(dollarPattern);
export const double = paraphrase(doublePattern);
export const single = paraphrase(singlePattern);
export const percent = paraphrase(percentPattern);
export const hash = paraphrase(hashPattern);
export const loose = paraphrase(
flavours.dollar,
flavours.double,
flavours.percent,
flavours.hash,
flavours.single,
dollarPattern,
doublePattern,
percentPattern,
hashPattern,
singlePattern,
);
21 changes: 21 additions & 0 deletions src/isObject/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-env mocha */

import { isObject } from ".";

describe("isObject", (): void => {
test.each([
[{}, true],
[{ a: 1 }, true],
[[], false],
[null, false],
[undefined, false],
[1, false],
["", false],
[true, false],
[new Map(), false],
[new Set(), false],
[() => {}, false],
])("isObject(%p) === %p", (input, expected): void =>
expect(isObject(input)).toBe(expected),
);
});
21 changes: 20 additions & 1 deletion types/flavours/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
export declare const flavours: Record<string, RegExp>;
/**
* Template: 'Hello, ${name}'
*/
export declare const dollarPattern: RegExp;
/**
* Template: 'Hello, {{name}}'
*/
export declare const doublePattern: RegExp;
/**
* Template: 'Hello, {name}'
*/
export declare const singlePattern: RegExp;
/**
* Template: 'Hello, #{name}'
*/
export declare const hashPattern: RegExp;
/**
* Template: 'Hello, %{name}'
*/
export declare const percentPattern: RegExp;

0 comments on commit c39c32a

Please sign in to comment.