mm_os 4.0.1 → 4.0.2

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mm_os",
3
3
  "description": "MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。",
4
- "version": "4.0.1",
4
+ "version": "4.0.2",
5
5
  "main": "index.js",
6
6
  "keywords": [
7
7
  "AI",
@@ -76,6 +76,7 @@
76
76
  "adapter/",
77
77
  "com/",
78
78
  "common/",
79
- "core/"
79
+ "core/",
80
+ "ulits/"
80
81
  ]
81
82
  }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * ID生成器
3
+ * 基于雪花算法实现,生成唯一ID
4
+ */
5
+ class IdGen {
6
+ /**
7
+ * 构造函数
8
+ * @param {object} config 配置参数
9
+ * @param {number} config.worker_id 工作机器ID (0-31)
10
+ * @param {number} config.dc_id 数据中心ID (0-31)
11
+ * @param {number} config.sequence 序列号
12
+ */
13
+ constructor(config = {}) {
14
+ this.worker_id = config.worker_id || 0;
15
+ this.dc_id = config.dc_id || 0;
16
+ this.sequence = config.sequence || 0;
17
+
18
+ this.worker_id_bits = 5;
19
+ this.dc_id_bits = 5;
20
+ this.max_worker_id = -1 ^ (-1 << this.worker_id_bits);
21
+ this.max_dc_id = -1 ^ (-1 << this.dc_id_bits);
22
+ this.sequence_bits = 12;
23
+
24
+ this.worker_id_shift = this.sequence_bits;
25
+ this.dc_id_shift = this.sequence_bits + this.worker_id_bits;
26
+ this.timestamp_left_shift = this.sequence_bits + this.worker_id_bits + this.dc_id_bits;
27
+ this.sequence_mask = -1 ^ (-1 << this.sequence_bits);
28
+
29
+ this.last_timestamp = -1;
30
+
31
+ this.twepoch = 1288834974657;
32
+
33
+ if (this.worker_id > this.max_worker_id || this.worker_id < 0) {
34
+ throw new TypeError(`工作机器ID必须在0-${this.max_worker_id}之间`);
35
+ }
36
+ if (this.dc_id > this.max_dc_id || this.dc_id < 0) {
37
+ throw new TypeError(`数据中心ID必须在0-${this.max_dc_id}之间`);
38
+ }
39
+ }
40
+ }
41
+
42
+ /**
43
+ * 获取当前时间戳
44
+ * @returns {number} 当前时间戳
45
+ */
46
+ IdGen.prototype._timeGen = function () {
47
+ return Date.now();
48
+ };
49
+
50
+ /**
51
+ * 等待下一毫秒
52
+ * @param {number} last_timestamp 上次时间戳
53
+ * @returns {number} 新的时间戳
54
+ */
55
+ IdGen.prototype._tilNextMillis = function (last_timestamp) {
56
+ var timestamp = this._timeGen();
57
+ while (timestamp <= last_timestamp) {
58
+ timestamp = this._timeGen();
59
+ }
60
+ return timestamp;
61
+ };
62
+
63
+ /**
64
+ * 生成下一个ID
65
+ * @returns {string} 唯一ID
66
+ */
67
+ IdGen.prototype.nextId = function () {
68
+ var timestamp = this._timeGen();
69
+
70
+ if (timestamp < this.last_timestamp) {
71
+ throw new Error('时钟回拨,拒绝生成ID');
72
+ }
73
+
74
+ if (this.last_timestamp === timestamp) {
75
+ this.sequence = (this.sequence + 1) & this.sequence_mask;
76
+ if (this.sequence === 0) {
77
+ timestamp = this._tilNextMillis(this.last_timestamp);
78
+ }
79
+ } else {
80
+ this.sequence = 0;
81
+ }
82
+
83
+ this.last_timestamp = timestamp;
84
+
85
+ var id = (BigInt(timestamp - this.twepoch) << BigInt(this.timestamp_left_shift))
86
+ | (BigInt(this.dc_id) << BigInt(this.dc_id_shift))
87
+ | (BigInt(this.worker_id) << BigInt(this.worker_id_shift))
88
+ | BigInt(this.sequence);
89
+
90
+ return id.toString();
91
+ };
92
+
93
+ /**
94
+ * 生成带前缀的ID
95
+ * @param {string} prefix 前缀
96
+ * @returns {string} 带前缀的唯一ID
97
+ */
98
+ IdGen.prototype.genId = function (prefix) {
99
+ if (typeof prefix !== 'string') {
100
+ throw new TypeError('前缀必须是字符串');
101
+ }
102
+ return prefix + '_' + this.nextId();
103
+ };
104
+
105
+ /**
106
+ * 解析ID信息
107
+ * @param {string} id ID字符串
108
+ * @returns {object} ID信息对象
109
+ */
110
+ IdGen.prototype.parseId = function (id) {
111
+ if (typeof id !== 'string') {
112
+ throw new TypeError('ID必须是字符串');
113
+ }
114
+ var num = BigInt(id.replace(/^[a-zA-Z_]+_/, ''));
115
+ var timestamp = Number((num >> 22n) + BigInt(this.twepoch));
116
+ var dc_id = Number((num >> 17n) & 31n);
117
+ var worker_id = Number((num >> 12n) & 31n);
118
+ var sequence = Number(num & 4095n);
119
+
120
+ return {
121
+ timestamp: timestamp,
122
+ datetime: new Date(timestamp).toISOString(),
123
+ dc_id: dc_id,
124
+ worker_id: worker_id,
125
+ sequence: sequence
126
+ };
127
+ };
128
+
129
+ var idGen = new IdGen();
130
+
131
+ module.exports = {
132
+ IdGen,
133
+ idGen
134
+ };
package/ulits/word.js ADDED
@@ -0,0 +1,206 @@
1
+ /**
2
+ * 单词工具类
3
+ * @class Word
4
+ */
5
+ class Word {
6
+ /**
7
+ * 构造函数
8
+ */
9
+ constructor() {
10
+
11
+ }
12
+ }
13
+
14
+ /**
15
+ * 分割单词
16
+ * @param {string} name 名称
17
+ * @returns {Array} 单词数组
18
+ */
19
+ Word.prototype.splitWords = function (name) {
20
+ if (!name || name.length === 0) {
21
+ return [];
22
+ }
23
+
24
+ var words = [];
25
+ var current_word = '';
26
+ var last_char_type = null;
27
+
28
+ for (var i = 0; i < name.length; i++) {
29
+ var char = name[i];
30
+ var char_info = this._getCharInfo(char);
31
+
32
+ // 处理下划线和数字
33
+ if (char_info.is_under || char_info.is_digit) {
34
+ last_char_type = this._handleSepa(words, current_word, char, char_info);
35
+ current_word = '';
36
+ continue;
37
+ }
38
+
39
+ // 处理第一个字符
40
+ if (current_word.length === 0) {
41
+ current_word = char;
42
+ last_char_type = char_info.type;
43
+ continue;
44
+ }
45
+
46
+ // 处理字符转换
47
+ var result = this._handleCharTran(words, current_word, char, last_char_type, char_info.type);
48
+ words = result.words;
49
+ current_word = result.current_word;
50
+ last_char_type = char_info.type;
51
+ }
52
+
53
+ // 添加最后一个单词
54
+ if (current_word.length > 0) {
55
+ words.push(current_word);
56
+ }
57
+
58
+ return words;
59
+ };
60
+
61
+ /**
62
+ * 获取字符信息
63
+ * @param {string} char 字符
64
+ * @returns {object} 字符信息
65
+ * @private
66
+ */
67
+ Word.prototype._getCharInfo = function (char) {
68
+ var is_upper = char >= 'A' && char <= 'Z';
69
+ var is_lower = char >= 'a' && char <= 'z';
70
+ var is_digit = char >= '0' && char <= '9';
71
+ var type = is_upper ? 'upper' : (is_lower ? 'lower' : (is_digit ? 'digit' : 'other'));
72
+ return { is_upper, is_lower, is_digit, is_under: char === '_', type };
73
+ };
74
+
75
+ /**
76
+ * 处理分隔符
77
+ * @param {Array} words 单词数组
78
+ * @param {string} current_word 当前单词
79
+ * @param {string} char 字符
80
+ * @param {object} char_info 字符信息
81
+ * @returns {string} 新的字符类型
82
+ * @private
83
+ */
84
+ Word.prototype._handleSepa = function (words, current_word, char, char_info) {
85
+ if (current_word.length > 0) {
86
+ words.push(current_word);
87
+ }
88
+ if (char_info.is_digit) {
89
+ words.push(char);
90
+ return 'digit';
91
+ }
92
+ return 'underscore';
93
+ };
94
+
95
+ /**
96
+ * 处理字符转换
97
+ * @param {Array} words 单词数组
98
+ * @param {string} current_word 当前单词
99
+ * @param {string} char 字符
100
+ * @param {string} last_type 上一个字符类型
101
+ * @param {string} current_type 当前字符类型
102
+ * @returns {object} 处理结果
103
+ * @private
104
+ */
105
+ Word.prototype._handleCharTran = function (
106
+ words, current_word, char, last_type, current_type
107
+ ) {
108
+ var is_last_letter = last_type === 'upper' || last_type === 'lower';
109
+ var is_current_letter = current_type === 'upper' || current_type === 'lower';
110
+
111
+ if (!is_last_letter || !is_current_letter) {
112
+ return { words, current_word: current_word + char };
113
+ }
114
+
115
+ if (last_type === 'upper' && current_type === 'lower') {
116
+ // 大写转小写
117
+ if (current_word.length > 1) {
118
+ var last_char = current_word[current_word.length - 1];
119
+ words.push(current_word.substring(0, current_word.length - 1));
120
+ return { words, current_word: last_char + char };
121
+ }
122
+ return { words, current_word: current_word + char };
123
+ }
124
+
125
+ if (last_type === 'lower' && current_type === 'upper') {
126
+ // 小写转大写
127
+ words.push(current_word);
128
+ return { words, current_word: char };
129
+ }
130
+
131
+ // 相同类型
132
+ return { words, current_word: current_word + char };
133
+ };
134
+
135
+ /**
136
+ * 将名称改为帕斯卡命名法
137
+ * @param {string} name 名称
138
+ * @returns {string} 帕斯卡命名法的名称
139
+ */
140
+ Word.prototype.toPascalCase = function (name) {
141
+ var words = this.splitWords(name);
142
+ for (let i = 0; i < words.length; i++) {
143
+ words[i] = words[i].toLowerCase();
144
+ words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
145
+ }
146
+ return words.join('');
147
+ };
148
+
149
+ /**
150
+ * 将名称改为驼峰命名法
151
+ * @param {string} name 名称
152
+ * @returns {string} 驼峰命名法的名称
153
+ */
154
+ Word.prototype.toCamelCase = function (name) {
155
+ var words = this.splitWords(name);
156
+ for (let i = 0; i < words.length; i++) {
157
+ words[i] = words[i].toLowerCase();
158
+ if (i > 0) {
159
+ words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
160
+ }
161
+ }
162
+ return words.join('');
163
+ };
164
+
165
+ /**
166
+ * 将名称改为小写蛇形(下划线)命名法
167
+ * @param {string} name 名称
168
+ * @returns {string} 小写蛇形(下划线)命名法的名称
169
+ */
170
+ Word.prototype.toSnakeCase = function (name) {
171
+ var words = this.splitWords(name);
172
+ for (let i = 0; i < words.length; i++) {
173
+ words[i] = words[i].toLowerCase();
174
+ }
175
+ return words.join('_');
176
+ };
177
+
178
+ /**
179
+ * 将名称改为大写蛇形(下划线)命名法
180
+ * @param {string} name 名称
181
+ * @returns {string} 大写蛇形(下划线)命名法的名称
182
+ */
183
+ Word.prototype.toUpperSnakeCase = function (name) {
184
+ var words = this.splitWords(name);
185
+ for (let i = 0; i < words.length; i++) {
186
+ words[i] = words[i].toUpperCase();
187
+ }
188
+ return words.join('_');
189
+ };
190
+
191
+ /**
192
+ * 将名称改为小写短横线(短横线)命名法
193
+ * @param {string} name 名称
194
+ * @returns {string} 小写短横线(短横线)命名法的名称
195
+ */
196
+ Word.prototype.toKebabCase = function (name) {
197
+ var words = this.splitWords(name);
198
+ for (let i = 0; i < words.length; i++) {
199
+ words[i] = words[i].toLowerCase();
200
+ }
201
+ return words.join('-');
202
+ };
203
+
204
+ module.exports = {
205
+ Word
206
+ };