Skip to content

Commit 2965e6d

Browse files
feat(frontend): use excerpt method from util package
1 parent 8caecf6 commit 2965e6d

File tree

3 files changed

+3
-42
lines changed

3 files changed

+3
-42
lines changed
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
export {
2+
excerpt,
23
formatDate as date,
34
formatDateToLocal as localDate,
45
getCanonicalUrl as url,
56
slugify,
67
} from "@indiekit/util";
78
export { isArray } from "./array.js";
89
export { languageName, languageNativeName } from "./locale.js";
9-
export { excerpt, includes, linkTo, markdown } from "./string.js";
10+
export { includes, linkTo, markdown } from "./string.js";
1011
export { friendlyUrl, imageUrl } from "./url.js";

packages/frontend/lib/filters/string.js

-30
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,5 @@
11
import markdownIt from "../markdown-it.js";
22

3-
/**
4-
* Excerpt a string
5-
* @param {string} string - String to excerpt
6-
* @param {number} value - Maximum number of words
7-
* @param {string} locale - Locale
8-
* @returns {string} Excerpted string
9-
*/
10-
export const excerpt = (string, value = 100, locale = "en") => {
11-
const segmenter = new Intl.Segmenter(locale, { granularity: "word" });
12-
const segments = segmenter.segment(string);
13-
14-
let excerpt = "";
15-
let n = 0;
16-
let words = 0;
17-
[...segments].map((segment) => {
18-
words = segment.isWordLike ? n++ : n;
19-
if (words < value) {
20-
excerpt += segment.segment;
21-
}
22-
23-
return excerpt;
24-
});
25-
26-
if (words > value) {
27-
excerpt += "…";
28-
}
29-
30-
return excerpt;
31-
};
32-
333
/**
344
* Check if string includes a value
355
* @param {string} string - String to check

packages/frontend/test/unit/filters/string.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
import { strict as assert } from "node:assert";
22
import { describe, it } from "node:test";
33

4-
import {
5-
excerpt,
6-
includes,
7-
linkTo,
8-
markdown,
9-
} from "../../../lib/filters/index.js";
4+
import { includes, linkTo, markdown } from "../../../lib/filters/index.js";
105

116
describe("frontend/lib/filters/string", () => {
12-
it("Excerpts a string", () => {
13-
assert.equal(excerpt("Well, well, well!", 1), "Well…");
14-
assert.equal(excerpt("Well, indeed."), "Well, indeed.");
15-
});
16-
177
it("Check if string includes a value", () => {
188
assert.equal(includes("foo,bar", "foo"), true);
199
assert.equal(includes("foo,bar", "qux"), false);

0 commit comments

Comments
 (0)