@zwa73/utils 1.0.281 → 1.0.283

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.
@@ -0,0 +1,35 @@
1
+ import type { DataStore, JObject } from '@zwa73/js-utils';
2
+ /**JSON文件存储实现 */
3
+ export declare class JsonStore<T extends JObject> implements DataStore<T> {
4
+ /**存储的数据 */
5
+ private _data;
6
+ /**上次保存时的数据快照,用于变更检测 */
7
+ private _lastSaved;
8
+ /**文件存储路径 */
9
+ private _filepath;
10
+ /**是否检查数据变更 */
11
+ private _checkChange;
12
+ /**
13
+ * 构造函数
14
+ * @param opt 配置选项
15
+ */
16
+ constructor(opt: {
17
+ /**文件路径 */
18
+ filepath: string;
19
+ /**是否检查数据变更,默认true */
20
+ checkChange?: boolean;
21
+ });
22
+ /**保存数据到文件
23
+ * @param data 要保存的数据
24
+ * @returns 是否真实产生写入保存
25
+ */
26
+ save(data: T): Promise<boolean>;
27
+ /**获取数据
28
+ * @returns 存储的数据
29
+ */
30
+ fetch(): Promise<T>;
31
+ /**获取唯一标识
32
+ * @returns 基于路径的唯一标识符
33
+ */
34
+ getUid(): string;
35
+ }
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.JsonStore = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const UtilFileTools_1 = require("../UtilFileTools");
9
+ const UtilFunctions_1 = require("../UtilFunctions");
10
+ const UtilLogger_1 = require("../UtilLogger");
11
+ /**JSON文件存储实现 */
12
+ class JsonStore {
13
+ /**存储的数据 */
14
+ _data;
15
+ /**上次保存时的数据快照,用于变更检测 */
16
+ _lastSaved = '';
17
+ /**文件存储路径 */
18
+ _filepath;
19
+ /**是否检查数据变更 */
20
+ _checkChange;
21
+ /**
22
+ * 构造函数
23
+ * @param opt 配置选项
24
+ */
25
+ constructor(opt) {
26
+ const { filepath, checkChange = true } = opt;
27
+ this._filepath = filepath;
28
+ this._checkChange = checkChange;
29
+ UtilFileTools_1.UtilFT.pathExists(this._filepath).then(exists => {
30
+ if (!exists)
31
+ UtilLogger_1.SLogger.warn(`JsonStore 配置文件不存在, 将会尝试用空对象创建 path:${this._filepath}`);
32
+ }).catch(err => {
33
+ UtilLogger_1.SLogger.error(`JsonStore 检查配置文件失败 path:${this._filepath}`, err);
34
+ });
35
+ }
36
+ /**保存数据到文件
37
+ * @param data 要保存的数据
38
+ * @returns 是否真实产生写入保存
39
+ */
40
+ async save(data) {
41
+ this._data = data;
42
+ const newSaved = this._checkChange ? JSON.stringify(this._data) : '';
43
+ if (this._checkChange == false || this._lastSaved != newSaved) {
44
+ await fs_1.default.promises.writeFile(this._filepath, UtilFunctions_1.UtilFunc.stringifyJToken(this._data));
45
+ this._lastSaved = newSaved;
46
+ return true;
47
+ }
48
+ return false;
49
+ }
50
+ /**获取数据
51
+ * @returns 存储的数据
52
+ */
53
+ async fetch() {
54
+ if (this._data == undefined) {
55
+ this._data = await UtilFileTools_1.UtilFT.loadJSONFile(this._filepath, { default: {} });
56
+ if (this._checkChange)
57
+ this._lastSaved = JSON.stringify(this._data);
58
+ }
59
+ return this._data;
60
+ }
61
+ /**获取唯一标识
62
+ * @returns 基于路径的唯一标识符
63
+ */
64
+ getUid() {
65
+ return `JsonStore_${this._filepath}`;
66
+ }
67
+ }
68
+ exports.JsonStore = JsonStore;
@@ -39,9 +39,10 @@ type ScanResult = {
39
39
  codeobj: undefined | Record<string, any>;
40
40
  };
41
41
  export declare class ObsidianDepot {
42
- /**将数组转为markdown列表文本 */
43
- static toList(v?: string[]): string | undefined;
44
- private constructor();
42
+ /**将数组转为markdown列表 */
43
+ static toList(itemList?: string[]): string | undefined;
44
+ /**将数组转为markdown文件预览 */
45
+ static toPreview(filepath: string): string;
45
46
  /**搜索info.md文件
46
47
  * @param opt - 搜索选项
47
48
  */
@@ -12,11 +12,22 @@ const UtilFileTools_1 = require("../UtilFileTools");
12
12
  const UtilFunctions_1 = require("../UtilFunctions");
13
13
  const Markdown_1 = require("./Markdown");
14
14
  class ObsidianDepot {
15
- /**将数组转为markdown列表文本 */
16
- static toList(v) {
17
- return v?.map(vv => ` - ${vv.trim()}`).join('\n');
15
+ //static create(opt:ObsidianDepotOpt){return new ObsidianDepot(opt);}
16
+ //private constructor(private opt:ObsidianDepotOpt){}
17
+ //async scanMarkdown(opt:Omit<ScanOpt,'rootPath'>){
18
+ // return ObsidianDepot.scanMarkdown({...opt,rootPath:this.opt.depotPath});
19
+ //}
20
+ //async updateMarkdown(opt:Omit<UpdateOpt,'rootPath'>){
21
+ // return ObsidianDepot.updateMarkdown({...opt,rootPath:this.opt.depotPath});
22
+ //}
23
+ /**将数组转为markdown列表 */
24
+ static toList(itemList) {
25
+ return itemList?.map(item => ` - ${item.trim()}`).join('\n');
26
+ }
27
+ /**将数组转为markdown文件预览 */
28
+ static toPreview(filepath) {
29
+ return `![[${filepath}]]`;
18
30
  }
19
- constructor() { }
20
31
  /**搜索info.md文件
21
32
  * @param opt - 搜索选项
22
33
  */
@@ -1,5 +1,6 @@
1
1
  export * from './Hbs';
2
2
  export * from './Markdown';
3
3
  export * from './ObsidianDepot';
4
- export type { EventData, DListMiddleNode, DListHeadNode, DListTailNode, DListInvalidNode, DListMaybeNode, DListNode, BridgeInterface } from "@zwa73/js-utils";
5
- export { Stream, Spool, SmartCache, SequenceQueue, PromiseQueue, Piper, EventSystem, DLinkedList, DelayQueue, Bridge } from "@zwa73/js-utils";
4
+ export * from './DataStore';
5
+ export type { EventData, DListMiddleNode, DListHeadNode, DListTailNode, DListInvalidNode, DListMaybeNode, DListNode, DataStore, BridgeInterface } from "@zwa73/js-utils";
6
+ export { Stream, Spool, SmartCache, SequenceQueue, PromiseQueue, Piper, EventSystem, DLinkedList, DelayQueue, ObjectStore, Bridge } from "@zwa73/js-utils";
@@ -14,10 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Bridge = exports.DelayQueue = exports.DLinkedList = exports.EventSystem = exports.Piper = exports.PromiseQueue = exports.SequenceQueue = exports.SmartCache = exports.Spool = exports.Stream = void 0;
17
+ exports.Bridge = exports.ObjectStore = exports.DelayQueue = exports.DLinkedList = exports.EventSystem = exports.Piper = exports.PromiseQueue = exports.SequenceQueue = exports.SmartCache = exports.Spool = exports.Stream = void 0;
18
18
  __exportStar(require("./Hbs"), exports);
19
19
  __exportStar(require("./Markdown"), exports);
20
20
  __exportStar(require("./ObsidianDepot"), exports);
21
+ __exportStar(require("./DataStore"), exports);
21
22
  var js_utils_1 = require("@zwa73/js-utils");
22
23
  Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return js_utils_1.Stream; } });
23
24
  Object.defineProperty(exports, "Spool", { enumerable: true, get: function () { return js_utils_1.Spool; } });
@@ -28,5 +29,6 @@ Object.defineProperty(exports, "Piper", { enumerable: true, get: function () { r
28
29
  Object.defineProperty(exports, "EventSystem", { enumerable: true, get: function () { return js_utils_1.EventSystem; } });
29
30
  Object.defineProperty(exports, "DLinkedList", { enumerable: true, get: function () { return js_utils_1.DLinkedList; } });
30
31
  Object.defineProperty(exports, "DelayQueue", { enumerable: true, get: function () { return js_utils_1.DelayQueue; } });
32
+ Object.defineProperty(exports, "ObjectStore", { enumerable: true, get: function () { return js_utils_1.ObjectStore; } });
31
33
  Object.defineProperty(exports, "Bridge", { enumerable: true, get: function () { return js_utils_1.Bridge; } });
32
34
  //#endregion
package/dist/UtilHttp.js CHANGED
@@ -493,6 +493,14 @@ class UtilHttp {
493
493
  }
494
494
  }
495
495
  exports.UtilHttp = UtilHttp;
496
+ if (false)
497
+ void (async () => {
498
+ const tool2 = UtilHttp
499
+ .url('http://postman-echo.com/post')
500
+ .postJson()
501
+ .option({ timeout: 20000, port: 80 });
502
+ console.log(await tool2.once({ json: { j: 123 } }));
503
+ })();
496
504
  if (false)
497
505
  void ((async () => {
498
506
  const tool = UtilHttp
@@ -1 +1,2 @@
1
1
  export type { JToken, JValue, JArray, JObject, IJData, AnyFunc, Keyable, DeepReadonly, DeepWritable, PartialOption, ProperSubset, ProperSubsetCheck, IncludeCheck, UnionCheck, Literal, LiteralCheck, AllExtends, AssignObject, Writeable, Requireify, Inverted, FixedLengthTuple, Increment, RangeTuple, AnyString, UnionToIntersection, ExclusiveRecord, ExclusiveJObject, PromiseStatus, StatusVerifyFn, Await, FuncPropNames, ExtendThen, RequiredOnly, WithPrefix, Outcome, Either, Matchable, MatchableFlag, ExtractOutcome, ExtractMatchable, NeedInit, PRecord, MPromise, SPromise, SMPromise, NotPromise, AnyRecord, MReturnType, CmtTuple, Flasy, SrtSegment, ILogger, LogLevel, Asyncize, SharpSchema, ExpandSharpSchema, Preset, PresetOption, PresetFinal } from "@zwa73/js-utils";
2
+ export type { StringifyOpt, PromiseRetries, PromiseRetryResult } from "@zwa73/js-utils";
@@ -1,3 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.281",
3
+ "version": "1.0.283",
4
4
  "description": "my utils",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {