@vnejs/helpers.tokenize-exec-line 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 +1 -0
- package/dist/index.js +39 -0
- package/package.json +13 -6
- package/{index.js → src/index.ts} +13 -7
- package/tsconfig.json +17 -0
- package/tests.js +0 -18
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const tokenizeExecLine: (text: string) => (string | number | boolean)[];
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const NUMBER_REGEXP = /^-?\d+(\.\d+)?$/;
|
|
2
|
+
const TOKEN_INSIDE_START_CHARS = ["{", "(", "[", '"', "'", "`"];
|
|
3
|
+
const TOKEN_INSIDE_END_CHARS = { "{": "}", "(": ")", "[": "]" };
|
|
4
|
+
const fromStrToCorrectType = (token) => {
|
|
5
|
+
if (token === "true")
|
|
6
|
+
return true;
|
|
7
|
+
if (token === "false")
|
|
8
|
+
return false;
|
|
9
|
+
if (NUMBER_REGEXP.test(token))
|
|
10
|
+
return token.includes(".") ? parseFloat(token) : parseInt(token, 10);
|
|
11
|
+
return token;
|
|
12
|
+
};
|
|
13
|
+
const toLength = (q) => q.length;
|
|
14
|
+
const reduceTokenize = (acc, char) => {
|
|
15
|
+
if (acc.isInsideToken) {
|
|
16
|
+
if (char !== acc.tokenEndChar)
|
|
17
|
+
acc.array[acc.array.length - 1] += char;
|
|
18
|
+
else {
|
|
19
|
+
acc.array.push("");
|
|
20
|
+
acc.isInsideToken = false;
|
|
21
|
+
}
|
|
22
|
+
return acc;
|
|
23
|
+
}
|
|
24
|
+
if (TOKEN_INSIDE_START_CHARS.includes(char)) {
|
|
25
|
+
acc.array.push("");
|
|
26
|
+
acc.isInsideToken = true;
|
|
27
|
+
acc.tokenEndChar = TOKEN_INSIDE_END_CHARS[char] || char;
|
|
28
|
+
return acc;
|
|
29
|
+
}
|
|
30
|
+
if (char === " ")
|
|
31
|
+
acc.array.push("");
|
|
32
|
+
else
|
|
33
|
+
acc.array[acc.array.length - 1] += char;
|
|
34
|
+
return acc;
|
|
35
|
+
};
|
|
36
|
+
export const tokenizeExecLine = (text) => [...text]
|
|
37
|
+
.reduce(reduceTokenize, { array: [""] })
|
|
38
|
+
.array.filter(toLength)
|
|
39
|
+
.map(fromStrToCorrectType);
|
package/package.json
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/helpers.tokenize-exec-line",
|
|
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"
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
const NUMBER_REGEXP = /^-?\d+(\.\d+)?$/;
|
|
2
2
|
|
|
3
|
-
const TOKEN_INSIDE_START_CHARS = ["{", "(", "[", '"', "'", "`"];
|
|
4
|
-
const TOKEN_INSIDE_END_CHARS = { "{": "}", "(": ")", "[": "]" };
|
|
3
|
+
const TOKEN_INSIDE_START_CHARS = ["{", "(", "[", '"', "'", "`"] as const;
|
|
4
|
+
const TOKEN_INSIDE_END_CHARS: Record<string, string> = { "{": "}", "(": ")", "[": "]" };
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
type TokenizeExecLineAcc = {
|
|
7
|
+
array: string[];
|
|
8
|
+
isInsideToken?: boolean;
|
|
9
|
+
tokenEndChar?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const fromStrToCorrectType = (token: string): string | number | boolean => {
|
|
7
13
|
if (token === "true") return true;
|
|
8
14
|
if (token === "false") return false;
|
|
9
15
|
if (NUMBER_REGEXP.test(token)) return token.includes(".") ? parseFloat(token) : parseInt(token, 10);
|
|
10
16
|
return token;
|
|
11
17
|
};
|
|
12
18
|
|
|
13
|
-
const toLength = (q) => q.length;
|
|
19
|
+
const toLength = (q: string) => q.length;
|
|
14
20
|
|
|
15
|
-
const reduceTokenize = (acc, char) => {
|
|
21
|
+
const reduceTokenize = (acc: TokenizeExecLineAcc, char: string): TokenizeExecLineAcc => {
|
|
16
22
|
if (acc.isInsideToken) {
|
|
17
23
|
if (char !== acc.tokenEndChar) acc.array[acc.array.length - 1] += char;
|
|
18
24
|
else {
|
|
@@ -23,7 +29,7 @@ const reduceTokenize = (acc, char) => {
|
|
|
23
29
|
return acc;
|
|
24
30
|
}
|
|
25
31
|
|
|
26
|
-
if (TOKEN_INSIDE_START_CHARS.includes(char)) {
|
|
32
|
+
if ((TOKEN_INSIDE_START_CHARS as readonly string[]).includes(char)) {
|
|
27
33
|
acc.array.push("");
|
|
28
34
|
acc.isInsideToken = true;
|
|
29
35
|
acc.tokenEndChar = TOKEN_INSIDE_END_CHARS[char] || char;
|
|
@@ -36,7 +42,7 @@ const reduceTokenize = (acc, char) => {
|
|
|
36
42
|
return acc;
|
|
37
43
|
};
|
|
38
44
|
|
|
39
|
-
export const tokenizeExecLine = (text) =>
|
|
45
|
+
export const tokenizeExecLine = (text: string): (string | number | boolean)[] =>
|
|
40
46
|
[...text]
|
|
41
47
|
.reduce(reduceTokenize, { array: [""] })
|
|
42
48
|
.array.filter(toLength)
|
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/tests.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { tokenizeExecLine } from "./index.js";
|
|
2
|
-
|
|
3
|
-
const tests = [
|
|
4
|
-
"{Confusion} 500:500:500",
|
|
5
|
-
"{Confusion} 500 500 500",
|
|
6
|
-
"co change {co/hand_on_shoulder} 300",
|
|
7
|
-
'"Открыть глаза" 50 50',
|
|
8
|
-
`'Открыть глаза' "Открыть глаза" 50 50`,
|
|
9
|
-
'fl "Прикрой её."',
|
|
10
|
-
`fl 'Прикрой её.'`,
|
|
11
|
-
"{true} {123.45} abc",
|
|
12
|
-
"a{b}c",
|
|
13
|
-
" true false 42 ",
|
|
14
|
-
];
|
|
15
|
-
|
|
16
|
-
tests.forEach((test) => {
|
|
17
|
-
console.log(`${JSON.stringify(test)} ->`, tokenizeExecLine(test));
|
|
18
|
-
});
|