onchain-utility 0.0.1

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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # `@lexical/utils`
2
+
3
+ [![See API Documentation](https://lexical.dev/img/see-api-documentation.svg)](https://lexical.dev/docs/api/modules/lexical_utils)
4
+
5
+ This package contains misc utilities for Lexical.
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "onchain-utility",
3
+ "description": "This package contains misc utilities for onchain.",
4
+ "license": "MIT",
5
+ "version": "0.0.1",
6
+ "exports": {
7
+ ".": {
8
+ "import": {
9
+ "types": "./src/index.ts",
10
+ "default": "./dist/OnchainUtility.mjs"
11
+ },
12
+ "require": {
13
+ "types": "./src/index.ts",
14
+ "default": "./dist/OnchainUtility.js"
15
+ }
16
+ },
17
+ "./language": {
18
+ "import": {
19
+ "types": "./src/Language/index.ts",
20
+ "default": "./dist/Language.mjs"
21
+ },
22
+ "require": {
23
+ "types": "./src/Language/index.ts",
24
+ "default": "./dist/Language.js"
25
+ }
26
+ },
27
+ "./traversal": {
28
+ "import": {
29
+ "types": "./src/Traversal/index.ts",
30
+ "default": "./dist/Traversal.mjs"
31
+ },
32
+ "require": {
33
+ "types": "./src/Traversal/index.ts",
34
+ "default": "./dist/Traversal.js"
35
+ }
36
+ }
37
+ }
38
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ /**
10
+ * 用于国际化的文本格式化处理
11
+ */
12
+ export function t(strTemp: string, obj?: object) {
13
+ if (!obj) {
14
+ return strTemp;
15
+ }
16
+
17
+ for (const key in obj) {
18
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
19
+ const reg = new RegExp(`{(${key})+}`, 'g');
20
+ strTemp = strTemp.replace(reg, (obj as Record<string, string>)[key]);
21
+ }
22
+ }
23
+ return strTemp;
24
+ }
25
+
26
+ /**
27
+ * 国际化-根据pathCode从用户的languageMap查询对应文本
28
+ */
29
+ export const getI18nText = (
30
+ languageMap?: Record<string, string | undefined>,
31
+ pathCode?: string,
32
+ placeholder?: string,
33
+ ) => {
34
+ if (languageMap && pathCode) {
35
+ return languageMap[pathCode] || placeholder;
36
+ }
37
+
38
+ return placeholder;
39
+ };
40
+ /** 处理国际化模板的基础方法 */
41
+ export function baseTemplate(
42
+ LanguageMap: Record<string, string>,
43
+ ...params: TranslateI18nParams
44
+ ) {
45
+ const [key, extra] = params;
46
+ return t(
47
+ getI18nText(LanguageMap, key, extra.placeholder) || '',
48
+ extra.variate,
49
+ );
50
+ }
51
+ /**
52
+ * 初始化国际化函数 从LanguageMap(Record<string, string>)上获取到字符串模板,生成对应语言
53
+ * ```javascript
54
+ * initTranslateI18nFn((...params) => {
55
+ * return baseTemplate(LanguageMap, ...params)
56
+ * })
57
+ * ```
58
+ */
59
+ export function initTranslateI18nFn(
60
+ fn: (...params: TranslateI18nParams) => string,
61
+ ) {
62
+ window.translateI18n = fn;
63
+ }
64
+
65
+ export function translateI18n(...params: TranslateI18nParams) {
66
+ if (window.translateI18n) {
67
+ return window.translateI18n(...params);
68
+ } else {
69
+ return baseTemplate({}, ...params);
70
+ }
71
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ /** 按顺序深度优先 */
10
+ export function dfs<T = unknown>(data: T[], getNewStack: (node: T) => T[]) {
11
+ const stack = [...data];
12
+ const result = [];
13
+ while (stack.length > 0) {
14
+ const node = stack.shift()!;
15
+ result.push(node);
16
+ const children = getNewStack(node);
17
+ for (let i = children.length - 1; i >= 0; i--) {
18
+ const child = children[i];
19
+ stack.unshift(child);
20
+ }
21
+ }
22
+ return result;
23
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ export * from './Language';
10
+ export * from './Traversal';