Skip to content

Commit

Permalink
Improve decap logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Vendicated committed May 24, 2023
1 parent 96d4fd8 commit 0358710
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 40 deletions.
50 changes: 26 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
{
"private": true,
"scripts": {
"build": "node build.mjs"
},
"dependencies": {
"@swc/helpers": "^0.4.14"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@swc/core": "^1.3.35",
"esbuild": "^0.16.14",
"rollup": "^3.9.1",
"rollup-plugin-esbuild": "^5.0.0",
"vendetta-types": "latest"
},
"pnpm": {
"peerDependencyRules": {
"ignoreMissing": [
"react",
"react-native"
]
}
}
"private": true,
"scripts": {
"build": "node build.mjs",
"test": "tsx tests/index.ts"
},
"dependencies": {
"@swc/helpers": "^0.4.14"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@swc/core": "^1.3.35",
"@types/node": "^20.2.3",
"esbuild": "^0.16.14",
"rollup": "^3.9.1",
"rollup-plugin-esbuild": "^5.0.0",
"vendetta-types": "latest"
},
"pnpm": {
"peerDependencyRules": {
"ignoreMissing": [
"react",
"react-native"
]
}
}
}
34 changes: 34 additions & 0 deletions plugins/Decap/src/decap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const re = /^[A-Z][a-z']*$/;

export function decap(text: string) {
if (!text || text.length < 2) return text;

const out = [] as string[];

const words = text.split(" ");
for (let i = 0; i < words.length; i++) {
const word = words[i];

const findNextMaybe = (sequence: string) => {
const buf = [word];

for (let j = i + 1; j < words.length; j++) {
buf.push(words[j]);
if (words[j].includes(sequence)) {
out.push(...buf);
i = j;
return;
}
}

out.push(word);
};

if (word.startsWith("https://")) out.push(word);
else if (word.startsWith("```")) findNextMaybe("```");
else if (word.startsWith("`")) findNextMaybe("`");
else out.push(word.replace(re, m => m.toLowerCase()));
}

return out.join(" ");
}
10 changes: 4 additions & 6 deletions plugins/Decap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { findByProps } from "@vendetta/metro";
import { before } from "@vendetta/patcher";
import { decap } from "./decap";

const ups = [];

function bef(obj: any, name: string, cb: (args: any[]) => any) {
ups.push(before(name, obj, cb));
}

function decap(msg: { content?: string; }) {
if (!msg?.content) return;
msg.content = msg.content.replace(/\b[A-Z](?=[a-z]*\b)/g, m => m.toLowerCase());
}
const doDecap = (msg?: { content?: string; }) => msg?.content && (msg.content = decap(msg.content));

bef(findByProps("editMessage", "sendMessage"), "sendMessage", args => decap(args[1]));
bef(findByProps("editMessage", "sendMessage"), "sendMessage", args => doDecap(args[1]));

bef(findByProps("uploadLocalFiles"), "uploadLocalFiles", args => decap(args[0].parsedMessage));
bef(findByProps("uploadLocalFiles"), "uploadLocalFiles", args => doDecap(args[0].parsedMessage));

export const onUnload = () => ups.forEach(up => up());
15 changes: 15 additions & 0 deletions tests/decap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { decap } from "../plugins/Decap/src/decap";
import assert from "assert/strict";

const cases = {
"F": "F",
"Banana": "banana",
"Some sentence WITH funny CasIng": "some sentence WITH funny CasIng",
"Hi https://google.com/Foo Bye": "hi https://google.com/Foo bye",
"```js\nconst a = 'Hi'``` Bye": "```js\nconst a = 'Hi'``` bye"
};

for (const [input, expected] of Object.entries(cases)) {
const actual = decap(input);
assert.equal(actual, expected, `decap("${input}") should be "${expected}" but was "${actual}"`);
}
7 changes: 7 additions & 0 deletions tests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { readdirSync } from "fs";

for (const test of readdirSync(__dirname)) {
if (test !== "index.ts") {
require(`./${test}`);
}
}
20 changes: 10 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"outDir": "dist",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"jsx": "react-native",
"allowJs": true
},
"include": ["plugins", "node_modules/vendetta-types"]
"compilerOptions": {
"outDir": "dist",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"jsx": "react-native",
"allowJs": true
},
"include": ["plugins", "node_modules/vendetta-types", "tests"]
}

0 comments on commit 0358710

Please sign in to comment.