mm_dict 1.0.2 → 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
 
package/example/index.js CHANGED
@@ -23,14 +23,14 @@ function main() {
23
23
 
24
24
  // 方式2:类实例 + 文件持久化
25
25
  const file = path.join(TMP_DIR, 'dict.json');
26
- const dict = new Dict(file);
26
+ const dict = new Dict({ file });
27
27
  dict.set('zh', '中文').set('en', 'English');
28
28
  console.log('字典列表:', dict.list());
29
29
  console.log('has(zh):', dict.has('zh'));
30
30
  console.log('保存到文件:', dict.save());
31
31
 
32
32
  // 从文件重新加载
33
- const loaded = new Dict(file);
33
+ const loaded = new Dict({ file });
34
34
  loaded.load();
35
35
  console.log('加载后读取 en:', loaded.get('en'));
36
36
 
package/lib/dict.js CHANGED
@@ -17,14 +17,6 @@ class Dict extends KvStore {
17
17
  throw new TypeError('dict 的值必须是字符串');
18
18
  }
19
19
  }
20
-
21
- /**
22
- * 是否允许覆盖已存在的配置项
23
- * @returns {boolean} 不允许覆盖返回 false
24
- */
25
- _isOverwritable() {
26
- return false;
27
- }
28
20
  }
29
21
 
30
22
  module.exports = Dict;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_dict",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "字典存储模块:键和值都只能是字符串,用于键值映射",
5
5
  "main": "index.js",
6
6
  "repository": {
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
  });