@vnejs/helpers.tokenize-text 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -0
- package/dist/index.js +27 -0
- package/package.json +13 -6
- package/src/index.ts +40 -0
- package/tsconfig.json +17 -0
- package/index.js +0 -25
- package/tests.js +0 -7
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const TOKEN_MARK_START_CHARS = ["{"];
|
|
2
|
+
const TOKEN_MARK_END_CHARS = { "{": "}" };
|
|
3
|
+
const reduceTokenize = (acc, char) => {
|
|
4
|
+
if (acc.isInsideMark) {
|
|
5
|
+
const mark = acc.currentMark ?? "";
|
|
6
|
+
if (char !== acc.markEndChar) {
|
|
7
|
+
acc.currentMark = mark + char;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
if (!mark.startsWith("/"))
|
|
11
|
+
acc.marks.push(mark);
|
|
12
|
+
else
|
|
13
|
+
acc.marks = acc.marks.filter((m) => m !== mark.slice(1));
|
|
14
|
+
acc.isInsideMark = false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
else if (TOKEN_MARK_START_CHARS.includes(char)) {
|
|
18
|
+
acc.currentMark = "";
|
|
19
|
+
acc.isInsideMark = true;
|
|
20
|
+
acc.markEndChar = TOKEN_MARK_END_CHARS[char] || char;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
acc.array.push({ token: char, marks: acc.marks.slice() });
|
|
24
|
+
}
|
|
25
|
+
return acc;
|
|
26
|
+
};
|
|
27
|
+
export const tokenizeText = (str) => [...str].reduce(reduceTokenize, { array: [], marks: [] }).array;
|
package/package.json
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/helpers.tokenize-text",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"src",
|
|
10
|
+
"tsconfig.json"
|
|
11
|
+
],
|
|
6
12
|
"scripts": {
|
|
7
|
-
"test": "
|
|
13
|
+
"test": "npm run build && node tests.js",
|
|
14
|
+
"build": "npx @vnejs/monorepo package",
|
|
8
15
|
"publish:major:plugin": "npm run publish:major",
|
|
9
16
|
"publish:minor:plugin": "npm run publish:minor",
|
|
10
17
|
"publish:patch:plugin": "npm run publish:patch",
|
|
11
|
-
"publish:major": "
|
|
12
|
-
"publish:minor": "
|
|
13
|
-
"publish:patch": "
|
|
18
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
19
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
20
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
14
21
|
},
|
|
15
22
|
"author": "",
|
|
16
23
|
"license": "ISC"
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const TOKEN_MARK_START_CHARS = ["{"] as const;
|
|
2
|
+
const TOKEN_MARK_END_CHARS: Record<string, string> = { "{": "}" };
|
|
3
|
+
|
|
4
|
+
export type TokenizedChar = {
|
|
5
|
+
token: string;
|
|
6
|
+
marks: string[];
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type TokenizeTextAcc = {
|
|
10
|
+
array: TokenizedChar[];
|
|
11
|
+
marks: string[];
|
|
12
|
+
isInsideMark?: boolean;
|
|
13
|
+
markEndChar?: string;
|
|
14
|
+
currentMark?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const reduceTokenize = (acc: TokenizeTextAcc, char: string): TokenizeTextAcc => {
|
|
18
|
+
if (acc.isInsideMark) {
|
|
19
|
+
const mark = acc.currentMark ?? "";
|
|
20
|
+
|
|
21
|
+
if (char !== acc.markEndChar) {
|
|
22
|
+
acc.currentMark = mark + char;
|
|
23
|
+
} else {
|
|
24
|
+
if (!mark.startsWith("/")) acc.marks.push(mark);
|
|
25
|
+
else acc.marks = acc.marks.filter((m) => m !== mark.slice(1));
|
|
26
|
+
|
|
27
|
+
acc.isInsideMark = false;
|
|
28
|
+
}
|
|
29
|
+
} else if ((TOKEN_MARK_START_CHARS as readonly string[]).includes(char)) {
|
|
30
|
+
acc.currentMark = "";
|
|
31
|
+
acc.isInsideMark = true;
|
|
32
|
+
acc.markEndChar = TOKEN_MARK_END_CHARS[char] || char;
|
|
33
|
+
} else {
|
|
34
|
+
acc.array.push({ token: char, marks: acc.marks.slice() });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return acc;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const tokenizeText = (str: string): TokenizedChar[] => [...str].reduce(reduceTokenize, { array: [], marks: [] }).array;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"noEmitOnError": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"rootDir": "src",
|
|
13
|
+
"outDir": "dist"
|
|
14
|
+
},
|
|
15
|
+
"include": ["src/**/*.ts"],
|
|
16
|
+
"exclude": ["dist", "node_modules"]
|
|
17
|
+
}
|
package/index.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const TOKEN_MARK_START_CHARS = ["{"];
|
|
2
|
-
const TOKEN_MARK_END_CHARS = { "{": "}" };
|
|
3
|
-
|
|
4
|
-
const reduceTokenize = (acc, char) => {
|
|
5
|
-
if (acc.isInsideMark) {
|
|
6
|
-
if (char !== acc.markEndChar) {
|
|
7
|
-
acc.currentMark += char;
|
|
8
|
-
} else {
|
|
9
|
-
if (!acc.currentMark.startsWith("/")) acc.marks.push(acc.currentMark);
|
|
10
|
-
else acc.marks = acc.marks.filter((m) => m !== acc.currentMark.slice(1));
|
|
11
|
-
|
|
12
|
-
acc.isInsideMark = false;
|
|
13
|
-
}
|
|
14
|
-
} else if (TOKEN_MARK_START_CHARS.includes(char)) {
|
|
15
|
-
acc.currentMark = "";
|
|
16
|
-
acc.isInsideMark = true;
|
|
17
|
-
acc.markEndChar = TOKEN_MARK_END_CHARS[char] || char;
|
|
18
|
-
} else {
|
|
19
|
-
acc.array.push({ token: char, marks: acc.marks.slice() });
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return acc;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const tokenizeText = (str) => [...str].reduce(reduceTokenize, { array: [], marks: [] }).array;
|
package/tests.js
DELETED