@less-is-more/less-js 1.2.20 → 1.2.24

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@less-is-more/less-js",
3
- "version": "1.2.20",
3
+ "version": "1.2.24",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/cache.js CHANGED
@@ -2,32 +2,78 @@ const Redis = require("./redis");
2
2
  const Param = require("./param");
3
3
 
4
4
  module.exports = class Cache {
5
- /**
6
- * 缓存并获取结果
7
- * @param {*} key 缓存关键词,拼接参数
8
- * @param {number} timeSecond 缓存秒数
9
- * @param {object} thisObject this指向的对象
10
- * @param {function} targetMethod 没有缓存时,执行目标。暂时支持返回文本、对象、数组
11
- * @param {...any} args 没有缓存时,参数
12
- * @returns
13
- */
14
- static async get(key, timeSecond, thisObject, targetMethod, ...args) {
15
- let fullKey = key;
16
- args.forEach((nArg) => (fullKey += ":" + nArg));
17
- let savedData = await Redis.exec("get", fullKey);
18
- if (savedData == null) {
19
- let result = await targetMethod.apply(thisObject, args);
20
- if (!Param.isBlank(result)) {
21
- if (typeof result === "string") {
22
- await Redis.exec("setex", fullKey, timeSecond, result);
23
- return result;
24
- } else {
25
- throw new Error("只支持返回值是文本的方法");
26
- }
27
- }
28
- } else {
29
- console.log("Found cache", fullKey);
30
- return savedData;
31
- }
32
- }
5
+ /**
6
+ * 缓存并获取结果(调用方)
7
+ * @param {*} key 缓存关键词,拼接参数
8
+ * @param {number} timeSecond 缓存秒数
9
+ * @param {object} thisObject this指向的对象
10
+ * @param {function} targetMethod 没有缓存时,执行目标。暂时支持返回文本、对象、数组
11
+ * @param {...any} args 没有缓存时,参数
12
+ * @returns
13
+ */
14
+ static async get(key, timeSecond, thisObject, targetMethod, ...args) {
15
+ let fullKey = key;
16
+ args.forEach((nArg) => (fullKey += ":" + nArg));
17
+ let savedData = await Redis.exec("get", fullKey);
18
+ if (savedData == null) {
19
+ let result = await targetMethod.apply(thisObject, args);
20
+ if (!Param.isBlank(result)) {
21
+ if (typeof result === "string") {
22
+ await Redis.exec("setex", fullKey, timeSecond + "", result);
23
+ return result;
24
+ } else {
25
+ throw new Error("只支持返回值是文本的方法");
26
+ }
27
+ }
28
+ } else {
29
+ console.log("Found cache", fullKey);
30
+ return savedData;
31
+ }
32
+ }
33
+
34
+ /**
35
+ *
36
+ * @param {string} keyPrefix 缓存前缀
37
+ * @param {number} timeSecond 缓存时间
38
+ * @param {*} args 直接传arguments,用于拼接缓存key
39
+ * @param {function} fn 没有缓存的实现
40
+ * @returns 优先返回对象
41
+ */
42
+ static async auto(keyPrefix, timeSecond, args, fn) {
43
+ let fullKey = keyPrefix;
44
+ for (let i = 0; i < args.length; i++) {
45
+ fullKey += ":" + args[i].toString();
46
+ }
47
+ let savedData = await Redis.exec("get", fullKey);
48
+ if (savedData == null) {
49
+ let result = await fn();
50
+ if (!Param.isBlank(result)) {
51
+ // 处理JSON
52
+ if (result instanceof Object) {
53
+ await Redis.exec(
54
+ "setex",
55
+ fullKey,
56
+ timeSecond + "",
57
+ JSON.stringify(result)
58
+ );
59
+ } else {
60
+ await Redis.exec(
61
+ "setex",
62
+ fullKey,
63
+ timeSecond + "",
64
+ result.toString()
65
+ );
66
+ }
67
+ return result;
68
+ }
69
+ } else {
70
+ console.log("Found cache", fullKey);
71
+ // 优先转成json
72
+ if (savedData.startsWith("{") || savedData.startsWith("[")) {
73
+ return JSON.parse(savedData);
74
+ } else {
75
+ return savedData;
76
+ }
77
+ }
78
+ }
33
79
  };
package/src/redis.js CHANGED
@@ -33,7 +33,8 @@ module.exports = class Redis {
33
33
 
34
34
  static async exec(name, ...args) {
35
35
  let client = await Redis.getInstance();
36
- return await client[name].bind(client).apply(undefined, args);
36
+ args.unshift(name);
37
+ return await client.sendCommand(args);
37
38
  }
38
39
 
39
40
  static formatKey(name, params = {}) {
package/src/ret.js CHANGED
@@ -6,53 +6,64 @@ module.exports = class Ret {
6
6
  static CODE_SYSTEM = 1004;
7
7
 
8
8
  constructor(success, message, code) {
9
- this.#instance.success = success;
9
+ this.success = success;
10
10
  if (message) {
11
- this.#instance.message = message;
11
+ this.message = message;
12
12
  }
13
13
  if (code) {
14
- this.#instance.code = code;
14
+ this.code = code;
15
15
  }
16
16
  }
17
17
 
18
- #instance = {};
19
18
  ok() {
20
- this.#instance.success = true;
19
+ this.success = true;
21
20
  return this;
22
21
  }
22
+ isOk() {
23
+ return this.success;
24
+ }
23
25
  fail() {
24
- this.#instance.success = false;
26
+ this.success = false;
25
27
  return this;
26
28
  }
29
+ isFail() {
30
+ return !this.success;
31
+ }
27
32
  setMessage(message) {
28
- this.#instance.message = message;
33
+ this.message = message;
29
34
  return this;
30
35
  }
36
+ getMessage() {
37
+ return this.message;
38
+ }
31
39
  setCode(code) {
32
- this.#instance.code = code;
40
+ this.code = code;
33
41
  return this;
34
42
  }
35
43
  setData(data) {
36
- this.#instance.data = data;
44
+ this.data = data;
37
45
  return this;
38
46
  }
47
+ getData() {
48
+ return this.data;
49
+ }
39
50
  toString() {
40
- return JSON.stringify(this.#instance);
51
+ return JSON.stringify(this);
41
52
  }
42
53
  getJson() {
43
- return this.#instance;
54
+ return this;
44
55
  }
45
56
  toJson() {
46
- return this.#instance;
57
+ return this;
47
58
  }
48
59
 
49
60
  filter(props = []) {
50
61
  if (props.length > 0) {
51
- const data = this.#instance.data;
62
+ const data = this.data;
52
63
  if (data instanceof Array) {
53
- this.#instance.data = this._filterArray(data, props);
64
+ this.data = this._filterArray(data, props);
54
65
  } else {
55
- this.#instance.data = this._filterObject(data, props);
66
+ this.data = this._filterObject(data, props);
56
67
  }
57
68
  }
58
69
  return this;
@@ -3,45 +3,94 @@ const Cache = require("../src/cache.js");
3
3
  const Redis = require("../src/redis.js");
4
4
 
5
5
  describe("cache.js", () => {
6
- before(() => {
7
- console.log("init");
8
- Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
9
- });
6
+ before(() => {
7
+ console.log("init");
8
+ Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
9
+ });
10
10
 
11
- describe("get()", () => {
12
- it("return string", async () => {
13
- let testFunction = (a, b) => {
14
- console.log(a + b);
15
- return a + b + "";
16
- };
17
- let result = await Cache.get(
18
- "test",
19
- 20,
20
- testFunction,
21
- testFunction,
22
- 1,
23
- 2
24
- );
25
- console.log("result", result);
26
- assert(result === "3");
27
- });
28
- it("return object", async () => {
29
- let testFunction = (a, b) => {
30
- console.log(a + b);
31
- return { r: a + b };
32
- };
33
- try {
34
- let result = await Cache.get(
35
- "test2",
36
- 20,
37
- testFunction,
38
- testFunction,
39
- 1,
40
- 2
41
- );
42
- } catch (e) {
43
- assert(e.message === "只支持返回值是文本的方法");
44
- }
45
- });
46
- });
11
+ describe("get()", () => {
12
+ it("return string", async () => {
13
+ let testFunction = (a, b) => {
14
+ console.log(a + b);
15
+ return a + b + "";
16
+ };
17
+ let result = await Cache.get(
18
+ "test",
19
+ 20,
20
+ testFunction,
21
+ testFunction,
22
+ 1,
23
+ 2
24
+ );
25
+ console.log("result", result);
26
+ assert(result === "3");
27
+ });
28
+ it("return object", async () => {
29
+ let testFunction = (a, b) => {
30
+ console.log(a + b);
31
+ return { r: a + b };
32
+ };
33
+ try {
34
+ let result = await Cache.get(
35
+ "test2",
36
+ 20,
37
+ testFunction,
38
+ testFunction,
39
+ 1,
40
+ 2
41
+ );
42
+ } catch (e) {
43
+ assert(e.message === "只支持返回值是文本的方法");
44
+ }
45
+ });
46
+ });
47
+
48
+ describe("auto()", () => {
49
+ it("return string", async () => {
50
+ async function test(a, b) {
51
+ return await Cache.auto("test", 20, arguments, () => {
52
+ console.log(a + b);
53
+ return a + b + "";
54
+ });
55
+ }
56
+ const result = await test(1, 2);
57
+ console.log("result", result);
58
+ assert(result === "3");
59
+ });
60
+ it("return int", async () => {
61
+ async function test(a, b) {
62
+ let r = await Cache.auto("test2", 20, arguments, () => {
63
+ console.log(a + b);
64
+ return a + b;
65
+ });
66
+ return r;
67
+ }
68
+ const result = await test(1, 2);
69
+ console.log("result", result);
70
+ assert(result == 3);
71
+ });
72
+ it("return object", async () => {
73
+ async function test(a, b) {
74
+ return await Cache.auto("test3", 20, arguments, () => {
75
+ console.log(a, b);
76
+ return { a, b };
77
+ });
78
+ }
79
+ const result = await test(1, 2);
80
+ console.log("result", JSON.stringify(result));
81
+ const compare = { a: 1, b: 2 };
82
+ assert(result.a === compare.a);
83
+ });
84
+ it("return array", async () => {
85
+ async function test(a, b) {
86
+ return await Cache.auto("test4", 20, arguments, () => {
87
+ console.log(a, b);
88
+ return [{ a, b }];
89
+ });
90
+ }
91
+ const result = await test(1, 2);
92
+ console.log("result", JSON.stringify(result));
93
+ assert(result.length === 1);
94
+ });
95
+ });
47
96
  });
@@ -2,42 +2,53 @@ const assert = require("assert");
2
2
  const Redis = require("../src/redis.js");
3
3
 
4
4
  describe("redis.js", () => {
5
- before(() => {
6
- console.log("init");
7
- Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
8
- });
9
- describe("getInstance()", () => {
10
- it("connect", async () => {
11
- try {
12
- let result = await Redis.exec("get", "test");
13
- console.log(result);
14
- console.log("Connection has been established successfully.");
15
- assert.ok(true);
16
- } catch (error) {
17
- console.error("Unable to connect to the redis:", error);
18
- assert.ok(false);
19
- }
20
- });
21
- it("set", async () => {
22
- try {
23
- await Redis.exec("set", "test", "OK");
24
- let reply = await Redis.exec("get", "test");
25
- console.log(reply);
26
- assert.ok(true);
27
- } catch (error) {
28
- assert.ok(false);
29
- }
30
- });
31
- });
32
- describe("formatKey()", () => {
33
- it("no params", async () => {
34
- let result = await Redis.formatKey("order", {});
35
- assert(result == "order");
36
- });
37
- it("has params", async () => {
38
- let result = await Redis.formatKey("order", { code: "123" });
39
- console.log(result);
40
- assert(result == "order:code:123");
41
- });
42
- });
5
+ before(() => {
6
+ console.log("init");
7
+ Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
8
+ });
9
+ describe("getInstance()", () => {
10
+ it("connect", async () => {
11
+ try {
12
+ let result = await Redis.exec("get", "test");
13
+ console.log(result);
14
+ console.log("Connection has been established successfully.");
15
+ assert.ok(true);
16
+ } catch (error) {
17
+ console.error("Unable to connect to the redis:", error);
18
+ assert.ok(false);
19
+ }
20
+ });
21
+ it("set", async () => {
22
+ try {
23
+ await Redis.exec("set", "test", "OK");
24
+ let reply = await Redis.exec("get", "test");
25
+ console.log(reply);
26
+ assert.ok(true);
27
+ } catch (error) {
28
+ assert.ok(false);
29
+ }
30
+ });
31
+ it("set ex", async () => {
32
+ try {
33
+ await Redis.exec("setex", "test", "100", "OK");
34
+ let reply = await Redis.exec("get", "test");
35
+ console.log(reply);
36
+ assert.ok(true);
37
+ } catch (error) {
38
+ console.log(error);
39
+ assert.ok(false);
40
+ }
41
+ });
42
+ });
43
+ describe("formatKey()", () => {
44
+ it("no params", async () => {
45
+ let result = await Redis.formatKey("order", {});
46
+ assert(result == "order");
47
+ });
48
+ it("has params", async () => {
49
+ let result = await Redis.formatKey("order", { code: "123" });
50
+ console.log(result);
51
+ assert(result == "order:code:123");
52
+ });
53
+ });
43
54
  });
package/test/test-ret.js CHANGED
@@ -5,6 +5,7 @@ describe("ret.js", () => {
5
5
  describe("new()", () => {
6
6
  it("ok", () => {
7
7
  let ret = new Ret(true).setData({ name: "good" });
8
+ console.log(ret)
8
9
  assert(ret.getJson().data.name, "good");
9
10
  });
10
11
  it("mutli params", () => {