Skip to content

Commit 8c754e3

Browse files
alexandr-garbuzovqwerty541
authored andcommitted
refactor: move encode HTML function into separate module (anuraghazra#4591)
Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
1 parent aef861e commit 8c754e3

9 files changed

Lines changed: 30 additions & 24 deletions

File tree

backend/src/cards/gist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @ts-check
22

33
import {
4-
encodeHTML,
54
measureText,
65
flexLayout,
76
iconWithLabel,
@@ -10,6 +9,7 @@ import {
109
import Card from "../common/Card.js";
1110
import { getCardColors } from "../common/color.js";
1211
import { kFormatter, wrapTextMultiline } from "../common/fmt.js";
12+
import { encodeHTML } from "../common/html.js";
1313
import { icons } from "../common/icons.js";
1414
import languageColors from "../common/languageColors.json" with { type: "json" };
1515
import { parseEmojis } from "../common/ops.js";

backend/src/cards/repo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import { Card } from "../common/Card.js";
44
import { getCardColors } from "../common/color.js";
55
import { kFormatter, wrapTextMultiline } from "../common/fmt.js";
6+
import { encodeHTML } from "../common/html.js";
67
import { I18n } from "../common/I18n.js";
78
import { icons } from "../common/icons.js";
89
import { clampValue, parseEmojis } from "../common/ops.js";
910
import { buildSearchFilter } from "../common/utils.js";
1011
import {
11-
encodeHTML,
1212
flexLayout,
1313
measureText,
1414
iconWithLabel,

backend/src/common/Card.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { encodeHTML, flexLayout } from "./render.js";
1+
// @ts-check
2+
3+
import { encodeHTML } from "./html.js";
4+
import { flexLayout } from "./render.js";
25

36
class Card {
47
/**

backend/src/common/fmt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22

33
import wrap from "word-wrap";
4-
import { encodeHTML } from "./render.js";
4+
import { encodeHTML } from "./html.js";
55

66
/**
77
* Retrieves num with suffix k(thousands) precise to given decimal places.

backend/src/common/html.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @ts-check
2+
3+
/**
4+
* Encode string as HTML.
5+
*
6+
* @see https://stackoverflow.com/a/48073476/10629172
7+
*
8+
* @param {string} str String to encode.
9+
* @returns {string} Encoded string.
10+
*/
11+
const encodeHTML = (str) => {
12+
return str
13+
.replace(/[\u00A0-\u9999<>&](?!#)/gim, (i) => {
14+
return "&#" + i.charCodeAt(0) + ";";
15+
})
16+
.replace(/\u0008/gim, "");
17+
};
18+
19+
export { encodeHTML };

backend/src/common/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export { retryer } from "./retryer.js";
99
export {
1010
ERROR_CARD_LENGTH,
1111
renderError,
12-
encodeHTML,
1312
flexLayout,
1413
measureText,
1514
} from "./render.js";

backend/src/common/render.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
44
import { getCardColors } from "./color.js";
5+
import { encodeHTML } from "./html.js";
56

67
const OWNER_AFFILIATIONS = ["OWNER", "COLLABORATOR", "ORGANIZATION_MEMBER"];
78

@@ -90,22 +91,6 @@ const buildSearchFilter = (repos = [], owners = []) => {
9091
// Script parameters.
9192
const ERROR_CARD_LENGTH = 576.5;
9293

93-
/**
94-
* Encode string as HTML.
95-
*
96-
* @see https://stackoverflow.com/a/48073476/10629172
97-
*
98-
* @param {string} str String to encode.
99-
* @returns {string} Encoded string.
100-
*/
101-
const encodeHTML = (str) => {
102-
return str
103-
.replace(/[\u00A0-\u9999<>&](?!#)/gim, (i) => {
104-
return "&#" + i.charCodeAt(0) + ";";
105-
})
106-
.replace(/\u0008/gim, "");
107-
};
108-
10994
const UPSTREAM_API_ERRORS = [
11095
TRY_AGAIN_LATER,
11196
SECONDARY_ERROR_MESSAGES.MAX_RETRY,
@@ -252,7 +237,6 @@ export {
252237
renderError,
253238
createLanguageNode,
254239
iconWithLabel,
255-
encodeHTML,
256240
buildSearchFilter,
257241
flexLayout,
258242
OWNER_AFFILIATIONS,

backend/src/translations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22

3-
import { encodeHTML } from "./common/render.js";
3+
import { encodeHTML } from "./common/html.js";
44

55
/**
66
* Retrieves stat card labels in the available locales.

backend/tests/render.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import { describe, expect, it } from "@jest/globals";
44
import { queryByTestId } from "@testing-library/dom";
55
import "@testing-library/jest-dom/jest-globals";
6-
import { encodeHTML, renderError } from "../src/common/render.js";
6+
import { renderError } from "../src/common/render.js";
7+
import { encodeHTML } from "../src/common/html.js";
78

89
describe("Test render.js", () => {
910
it("should test encodeHTML", () => {

0 commit comments

Comments
 (0)