mm_dict 1.0.1 → 1.0.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/README.md CHANGED
@@ -18,12 +18,12 @@ npm install mm_dict
18
18
  const { Dict, dictAdmin } = require('mm_dict');
19
19
 
20
20
  // 直接使用存储类
21
- const dict = new Dict('./data/dict.json');
21
+ const dict = new Dict({ file: './data/dict.json' });
22
22
  dict.set('val', 'value').set('生命值', 'hp');
23
23
  dict.save();
24
24
 
25
25
  // 通过管理器按作用域获取实例(相同作用域返回同一实例)
26
- const scoped = dictAdmin('app', './data/app_dict.json');
26
+ const scoped = dictAdmin('app', { file: './data/app_dict.json' });
27
27
  scoped.set('HP', 'hp');
28
28
  ```
29
29
 
@@ -0,0 +1,52 @@
1
+ /**
2
+ * mm_dict 字典存储示例
3
+ * 演示 dictAdmin() 作用域工厂,以及 Dict 类的字符串键值
4
+ * 增删查与 JSON 文件持久化(save / load)。
5
+ * 运行:node example/index.js
6
+ */
7
+ const fs = require('fs');
8
+ const os = require('os');
9
+ const path = require('path');
10
+ const { Dict, dictAdmin } = require('../index.js');
11
+
12
+ /** 示例临时目录 */
13
+ const TMP_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'mm-dict-example-'));
14
+
15
+ /**
16
+ * 主流程
17
+ */
18
+ function main() {
19
+ // 方式1:作用域工厂,获取 os 作用域的字典存储实例
20
+ const store = dictAdmin('os');
21
+ store.set('country', 'CN');
22
+ console.log('工厂实例读取 country:', store.get('country'));
23
+
24
+ // 方式2:类实例 + 文件持久化
25
+ const file = path.join(TMP_DIR, 'dict.json');
26
+ const dict = new Dict({ file });
27
+ dict.set('zh', '中文').set('en', 'English');
28
+ console.log('字典列表:', dict.list());
29
+ console.log('has(zh):', dict.has('zh'));
30
+ console.log('保存到文件:', dict.save());
31
+
32
+ // 从文件重新加载
33
+ const loaded = new Dict({ file });
34
+ loaded.load();
35
+ console.log('加载后读取 en:', loaded.get('en'));
36
+
37
+ // 删除字典项
38
+ console.log('删除 en:', loaded.del('en'));
39
+ console.log('删除后列表:', loaded.list());
40
+
41
+ // 值必须是字符串
42
+ try {
43
+ dict.set('num', 123);
44
+ } catch (err) {
45
+ console.log('非字符串值被拒绝:', err.message);
46
+ }
47
+
48
+ // 清理临时目录
49
+ fs.rmSync(TMP_DIR, { recursive: true, force: true });
50
+ }
51
+
52
+ main();
package/lib/dict.js CHANGED
@@ -1,4 +1,4 @@
1
- const KvStore = require('mm_corejs').KvStore;
1
+ const KvStore = require('mm_kv_store').KvStore;
2
2
 
3
3
  /**
4
4
  * 字典存储类,值只能是字符串
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "mm_dict",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "字典存储模块:键和值都只能是字符串,用于键值映射",
5
5
  "main": "index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://gitee.com/qiuwenwu91/mm_dict.git"
9
+ },
6
10
  "keywords": [
7
11
  "mm_mod",
8
12
  "mm_infra",
@@ -19,7 +23,8 @@
19
23
  "test": "jest --coverage"
20
24
  },
21
25
  "dependencies": {
22
- "mm_corejs": "^1.1.4"
26
+ "mm_corejs": "^1.1.4",
27
+ "mm_kv_store": "^1.0.0"
23
28
  },
24
29
  "devDependencies": {
25
30
  "jest": "^30.5.2"
package/test/dict.test.js CHANGED
@@ -37,14 +37,14 @@ describe('Dict', () => {
37
37
  describe('构造', () => {
38
38
  it('默认不传文件路径时 file 为空字符串', () => {
39
39
  const dict = new Dict();
40
- expect(dict._file).toBe('');
40
+ expect(dict.config.file).toBe('');
41
41
  expect(dict.list()).toEqual({});
42
42
  });
43
43
 
44
44
  it('传入文件路径时保存路径', () => {
45
45
  const file = path.join(tmp_dir, 'dict.json');
46
- const dict = new Dict(file);
47
- expect(dict._file).toBe(file);
46
+ const dict = new Dict({ file });
47
+ expect(dict.config.file).toBe(file);
48
48
  });
49
49
  });
50
50
 
@@ -146,7 +146,7 @@ describe('Dict', () => {
146
146
 
147
147
  it('save 写入文件返回 true', () => {
148
148
  const file = path.join(tmp_dir, 'dict.json');
149
- const dict = new Dict(file);
149
+ const dict = new Dict({ file });
150
150
  dict.set('val', 'value');
151
151
  expect(dict.save()).toBe(true);
152
152
  expect(JSON.parse(fs.readFileSync(file, 'utf8'))).toEqual({ val: 'value' });
@@ -158,14 +158,14 @@ describe('Dict', () => {
158
158
  });
159
159
 
160
160
  it('文件不存在时 load 返回 false', () => {
161
- const dict = new Dict(path.join(tmp_dir, 'nope.json'));
161
+ const dict = new Dict({ file: path.join(tmp_dir, 'nope.json') });
162
162
  expect(dict.load()).toBe(false);
163
163
  });
164
164
 
165
165
  it('文件存在时 load 加载数据返回 true', () => {
166
166
  const file = path.join(tmp_dir, 'dict.json');
167
167
  fs.writeFileSync(file, JSON.stringify({ val: 'value', key: 'k' }));
168
- const dict = new Dict(file);
168
+ const dict = new Dict({ file });
169
169
  expect(dict.load()).toBe(true);
170
170
  expect(dict.get('val')).toBe('value');
171
171
  expect(dict.get('key')).toBe('k');
@@ -174,7 +174,7 @@ describe('Dict', () => {
174
174
  it('文件含非字符串值时 load 抛 TypeError', () => {
175
175
  const file = path.join(tmp_dir, 'dict.json');
176
176
  fs.writeFileSync(file, JSON.stringify({ val: 123 }));
177
- const dict = new Dict(file);
177
+ const dict = new Dict({ file });
178
178
  expect(() => dict.load()).toThrow('dict 的值必须是字符串');
179
179
  });
180
180
  });