Skip to content

Commit

Permalink
Merge pull request #16 from SnowFireWolf/dev
Browse files Browse the repository at this point in the history
rename function clean tags to clean codes
  • Loading branch information
SnowFireWolf authored Aug 1, 2023
2 parents c361aa8 + bed92a7 commit 386868e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 27 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Minecraft Server MOTD Parser
![Version](https://img.shields.io/github/languages/top/SnowFireWolf/minecraft-motd-parser?style=for-the-badge)
[![npm version](https://img.shields.io/npm/v/@sfirew/mc-motd-parser?label=version&style=for-the-badge)](https://www.npmjs.com/package/@sfirew/mc-motd-parser?style=for-the-badge)
[![npm version](https://img.shields.io/npm/v/@sfirew/minecraft-motd-parser?label=version&style=for-the-badge)](https://www.npmjs.com/package/@sfirew/minecraft-motd-parser?style=for-the-badge)
[![License](https://img.shields.io/npm/l/minecraft-server-util?style=for-the-badge)](https://github.com/SnowFireWolf/minecraft-motd-parser/blob/master/LICENSE)
![npm weekly downloads](https://img.shields.io/npm/dw/@sfirew/mc-motd-parser?style=for-the-badge)
![npm weekly downloads](https://img.shields.io/npm/dw/@sfirew/minecraft-motd-parser?style=for-the-badge)



## Introduction
This package support **CommonJS**, **ES Module**, and **tree shaking**

Can convert minecraft server MOTD data to text, json, html, and clean tags.
Can convert minecraft server MOTD data to text, json, html, and clean codes.

Support **1.16** custom **hex color**, and auto check motd data type.

Expand Down Expand Up @@ -83,14 +83,14 @@ You can try server status view in my created service
Some examples here, you can use **TypeScript** or **JavaScript**.


### `cleanTags(string)`
clean text color tags.
### `cleanCodes(string)`
clean motd text color codes.
```typescript
import motdParser from '@sfirew/minecraft-motd-parser'
import { cleanTags } from '@sfirew/minecraft-motd-parser'
import { cleanCodes } from '@sfirew/minecraft-motd-parser'

const motdText = "§aHypixel Network §7§c1.8/1.9/1.10/1.11/1.12 §e§lNEW PTL GAME:§b§l THE BRIDGE";
const result = cleanTags(motdText);
const result = cleanCodes(motdText);
console.log(result);

/* result, callback Text:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sfirew/minecraft-motd-parser",
"description": "Minecraft Server MOTD Parser, can convert to html, json, text.",
"version": "1.1.2",
"version": "1.1.2-1",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "types/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import {
htmlStringFormatting,
cleanTags,
cleanCodes,
cleanHtmlTags,
} from "./utils";
import {
Expand Down Expand Up @@ -48,8 +48,8 @@ const motdParser = {

// utils
htmlStringFormatting,
// clean all motd tags
cleanTags,
// clean all motd codes
cleanCodes,
// clean all html tags
cleanHtmlTags,
};
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ export function cleanHtmlTags(text: string): string {


/**
* clean motd tags
* clean motd codes
*
* Clean all tags from motd source string.
* Clean all codes from motd source string.
*
* @param {string} text - motd string include tag § will remove
*/
export function cleanTags(text: string) {
export function cleanCodes(text: string) {
const REGEX = /(?:§)([0-9a-fA-FklmnorFKLMNOR])/g;
let textResult = "";

Expand Down
11 changes: 6 additions & 5 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,26 @@ describe("Minecraft MOTD Parser", () => {
describe("autoToHTML", () => {
describe("- string to HTML", () => {
it("should convert MOTD string to HTML", () => {
const expectedOutput = `<span style="color:#FFFFFF;font-weight: bold;">『</span><span style="color:#FFFF55;font-weight: bold;">FC夢幻峽谷</span><span style="color:#FFFFFF;font-weight: bold;">』 </span><span style="color:#AAAAAA;font-weight: bold;">FantasyCanyon <br/> </span><span style="color:#55FFFF;font-weight: bold;">&lt;&lt;</span><span style="color:#5555FF;font-weight: bold;">◎</span><span style="color:#55FFFF;font-weight: bold;">------------</span><span style="color:#FFFF55;font-weight: bold;">加入冒險!</span><span style="color:#55FFFF;font-weight: bold;">------------</span><span style="color:#5555FF;font-weight: bold;">◎</span><span style="color:#55FFFF;font-weight: bold;">&gt;&gt;</span>`;
const result = motdParser.autoToHtml(testMOTDString);
const expectedOutput = `<span style="color:#FFFFFF;font-weight: bold;">『</span><span style="color:#FFFF55;font-weight: bold;font-weight: bold;">FC夢幻峽谷</span><span style="color:#FFFFFF;font-weight: bold;">』 </span><span style="color:#AAAAAA;font-weight: bold;font-weight: bold;">FantasyCanyon <br/> </span><span style="color:#55FFFF;font-weight: bold;font-weight: bold;font-weight: bold;">&lt;&lt;</span><span style="color:#5555FF;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;">◎</span><span style="color:#55FFFF;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;">------------</span><span style="color:#FFFF55;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;">加入冒險!</span><span style="color:#55FFFF;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;">------------</span><span style="color:#5555FF;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;">◎</span><span style="color:#55FFFF;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;font-weight: bold;">&gt;&gt;</span>`;
const result = motdParser.autoToHTML(testMOTDString);
console.log('result', result)
expect(result).toEqual(expectedOutput);
});
});

describe("- object (JSON) to HTML", () => {
it("should convert MOTD json to HTML", () => {
const expectedOutput = `<span style=\"color:#808080;\"> </span><span style=\"font-weight: bold;\"><span style=\"color:#1991EA;\">D</span><span style=\"color:#1C93EB;\">r</span><span style=\"color:#2096EC;\">e</span><span style=\"color:#2499ED;\">a</span><span style=\"color:#289CEE;\">m</span><span style=\"color:#2C9EEF;\">C</span><span style=\"color:#30A1F0;\">r</span><span style=\"color:#33A4F1;\">a</span><span style=\"color:#37A7F2;\">f</span><span style=\"color:#3BA9F3;\">t</span><span style=\"color:#3FACF4;\">e</span><span style=\"color:#43AFF5;\">r</span><span style=\"color:#47B2F6;\"> </span><span style=\"color:#4BB4F7;\">N</span><span style=\"color:#4EB7F8;\">e</span><span style=\"color:#52BAF9;\">t</span><span style=\"color:#56BDFA;\">w</span><span style=\"color:#5ABFFB;\">o</span><span style=\"color:#5EC2FC;\">r</span><span style=\"color:#62C5FD;\">k</span></span> <span style=\"color:#1f92ed;\">- </span><span style=\"font-weight: bold;\"><span style=\"color:#66C8FF;\">築</span><span style=\"color:#52BAF9;\">夢</span><span style=\"color:#3FACF4;\">物</span><span style=\"color:#2C9EEF;\">語</span></span><br/> <span style=\"color:#FFFFFF;\"> 在這裡 -- 實現你的理想! </span>`;
const result = motdParser.autoToHtml(testMotdJSON);
const result = motdParser.autoToHTML(testMotdJSON);
expect(result).toEqual(expectedOutput);
});
});
});

describe("cleanTags", () => {
describe("cleanCodes", () => {
it("should clean MOTD color tags", () => {
const expectedOutput = "『FC夢幻峽谷』 FantasyCanyon \n <<◎------------加入冒險!------------◎>>";
expect(motdParser.cleanTags(testMOTDString)).toEqual(expectedOutput);
expect(motdParser.cleanCodes(testMOTDString)).toEqual(expectedOutput);
});
});

Expand Down
6 changes: 3 additions & 3 deletions test_self/htmlTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let start = 0;
// clean tags
console.log("清除 MOTD Tags HTML");
start = new Date().getTime();
let cleanedString = motdParser.cleanTags(text);
let cleanedString = motdParser.cleanCodes(text);

console.log(cleanedString);
console.log(`執行時間: ${new Date().getTime() - start} ms`);
Expand All @@ -17,7 +17,7 @@ console.log(`執行時間: ${new Date().getTime() - start} ms`);
console.log("自動轉成 HTML");
start = new Date().getTime();

const motdHtml = motdParser.autoToHtml(text);
const motdHtml = motdParser.autoToHTML(text);
console.log("-------------------------");
console.log(motdHtml);
console.log("-------------------------");
Expand Down Expand Up @@ -59,7 +59,7 @@ const mcfalloutSourceMotd = {
text: "",
};

const mcfalloutMotdHtml = motdParser.autoToHtml(mcfalloutSourceMotd);
const mcfalloutMotdHtml = motdParser.autoToHTML(mcfalloutSourceMotd);
console.log("-------------------------");
console.log(mcfalloutMotdHtml);
console.log("-------------------------");
10 changes: 5 additions & 5 deletions test_self/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let start = 0;
console.log("-------------------------------------------------------");
console.log("清除 MOTD Tags HTML");
start = new Date().getTime();
let cleanedString = motdParser.cleanTags(text);
let cleanedString = motdParser.cleanCodes(text);

console.log(cleanedString);
console.log(`執行時間: ${new Date().getTime() - start} ms`);
Expand All @@ -83,8 +83,8 @@ console.log(`執行時間: ${new Date().getTime() - start} ms`);
console.log("-------------------------------------------------------");
console.log("JSON 轉成 HTML");
start = new Date().getTime();
let jsonToHtml = motdParser.JSONToHtml(JSON.parse(json));
let jsonToHtml2 = motdParser.JSONToHtml(mcfalloutJson);
let jsonToHtml = motdParser.JSONToHTML(JSON.parse(json));
let jsonToHtml2 = motdParser.JSONToHTML(mcfalloutJson);

console.log(jsonToHtml);
console.log("-------------------------");
Expand Down Expand Up @@ -152,8 +152,8 @@ let jsonExample = {

let textExample =
"§5§m §6>§7§l§6§l>§6§l[§5§l§oPurple §8§l§oPrison§6§l]§6§l<§6<§5§m §R §7 §5§k§l;;;§r §d§lNEW BLACK-MARKET §5§l» §6§L/BLACKMARKET §5§k§l;;;";
let autoToHtml = motdParser.autoToHtml(jsonExample);
let TextAutoToHtml = motdParser.autoToHtml(textExample);
let autoToHtml = motdParser.autoToHTML(jsonExample);
let TextAutoToHtml = motdParser.autoToHTML(textExample);

console.log("-------------------------------------------------------");
console.log(autoToHtml);
Expand Down

0 comments on commit 386868e

Please sign in to comment.