melperjs 2.0.1 → 3.0.0

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
@@ -21,7 +21,7 @@ const nodeHelper = require("melperjs/node");
21
21
  const axios = require("axios");
22
22
 
23
23
  (async () => {
24
- console.log(helper.Exception("something went wrong"));
24
+ console.log(helper.Exception("something went wrong", {status: 500}, "axios error"));
25
25
  console.log(helper.time());
26
26
  await helper.sleepMs(1000);
27
27
  console.log(helper.time());
@@ -43,8 +43,12 @@ const axios = require("axios");
43
43
  }
44
44
  }
45
45
  }));
46
- console.log("1 empty", helper.isEmpty(1));
47
- console.log("[] empty", helper.isEmpty([]));
46
+ console.log("str empty", helper.checkEmpty(""));
47
+ console.log("1 empty", helper.checkEmpty(1));
48
+ console.log("[] empty", helper.checkEmpty([]));
49
+ console.log(helper.upperCaseFirst("first letter upper"));
50
+ console.log(helper.lowerCaseFirst("First Letter Lower"));
51
+ console.log(helper.titleString("THIS mUsT be Title"));
48
52
  console.log(helper.limitString("LONG TEXT", 7));
49
53
  console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
50
54
  console.log(helper.randomString(32, true, true));
@@ -75,33 +79,41 @@ const axios = require("axios");
75
79
  })();
76
80
 
77
81
  /*
78
- { message: 'something went wrong', response: { status: 400 } }
79
- 1700522762
80
- 1700522763
81
- 1700522764
82
+ {
83
+ name: 'AxiosError',
84
+ message: 'something went wrong',
85
+ response: { status: 500 }
86
+ }
87
+ 1700529489
88
+ 1700529490
89
+ 1700529491
82
90
  Promise timed out after 1000ms
83
91
  { x: 1, y: 2, c: { d: true } }
92
+ str empty true
84
93
  1 empty false
85
94
  [] empty true
95
+ First letter upper
96
+ first Letter Lower
97
+ This Must Be Title
86
98
  LONG...
87
99
  SAFE TEXT
88
- i9ULmdbboTDL7NPgJRiY1i6O1x0D71wG
89
- 91d59653
90
- 50fe22be-a4e9-46cd-b73b-da5669ca43b4
100
+ 15WY89Q4yAMCzPmNsU0ANGX2eiOz7Gfy
101
+ 9eb93429
102
+ 7e1fb299-251c-4a75-bb37-f7cc3a730121
91
103
  strongProbability
92
- kpG7GBnGf8kK2JGwSNe8EaIdkrKl5OEr
93
- 4f592b6e
94
- e7947cc3-d975-446c-82ec-fc77d84a67bb
104
+ STIhrofUYVAQ4anplyJW2T7GEwSJkuk7
105
+ 6db791a1
106
+ 0cbef42d-a277-4f66-8bcc-e4de337c2b56
95
107
  strongProbability
96
108
  8d777f385d3dfec8815d20f7496026dc
97
- $2b$10$m3gbDZMz6P/NKtOBtcJPTe5Dt9TtGaFVE5M.F07QR4bLHoKuezfpm
109
+ $2b$10$IsuTscKKHbcf6sBp7BrCOOcg6A8v32G9UxzdYN3Y6xyMaUynweYX2
98
110
  passwordHash true
99
111
  {
100
- '1P_JAR': '2023-11-20-23',
101
- AEC: 'Ackid1QUUwFH8ZlfrHR1RFoG3nbxsOvmYckU_mnIw4Z6cE4ue9_mbng2mmU',
102
- NID: '511=m55MFOJ7XlxlkanJtm0q7eK5PkaePFQiaxEeMcAR_ojuPBWZ0bqw6tbDq4VGuT2CrTNgGWw_lLECx100Qlm1bRBNYEx_0wWtVNCh582q9-zhvQFSDbAI3vDtl-vMlMbpv_6JYJ7YBhGf1S6zbqv6Q84hlbLBOIg8Dt633-b5b94'
112
+ '1P_JAR': '2023-11-21-01',
113
+ AEC: 'Ackid1TlDwA2YJw3rzP5t3x5vBdxZt-4AzkhdwLahUVpj3vhdVHvPlw0VWM',
114
+ NID: '511=tD21gyuziCvCgZSQZd5h_xDFOF6df8AhkFy0iXq9MwHG9K8J3FEkT7L0CACgjJhGDVQFoZG_Pwi2Wo8Kf7NnmvcNGVmk-lDhY768PI9sVSUmSHIYwpfsuVvG4NwNwk3iPNKmbqaC_H-YVGZhEJVn2c6YYUVxE0oEDtfuyPhGOXw'
103
115
  }
104
- 1P_JAR=2023-11-20-23;AEC=Ackid1QUUwFH8ZlfrHR1RFoG3nbxsOvmYckU_mnIw4Z6cE4ue9_mbng2mmU;NID=511=m55MFOJ7XlxlkanJtm0q7eK5PkaePFQiaxEeMcAR_ojuPBWZ0bqw6tbDq4VGuT2CrTNgGWw_lLECx100Qlm1bRBNYEx_0wWtVNCh582q9-zhvQFSDbAI3vDtl-vMlMbpv_6JYJ7YBhGf1S6zbqv6Q84hlbLBOIg8Dt633-b5b94
116
+ 1P_JAR=2023-11-21-01;AEC=Ackid1TlDwA2YJw3rzP5t3x5vBdxZt-4AzkhdwLahUVpj3vhdVHvPlw0VWM;NID=511=tD21gyuziCvCgZSQZd5h_xDFOF6df8AhkFy0iXq9MwHG9K8J3FEkT7L0CACgjJhGDVQFoZG_Pwi2Wo8Kf7NnmvcNGVmk-lDhY768PI9sVSUmSHIYwpfsuVvG4NwNwk3iPNKmbqaC_H-YVGZhEJVn2c6YYUVxE0oEDtfuyPhGOXw
105
117
  http://id:pw-{SESSION}@127.0.0.1:8080
106
118
  {
107
119
  protocol: 'http',
@@ -109,7 +121,7 @@ http://id:pw-{SESSION}@127.0.0.1:8080
109
121
  port: 8080,
110
122
  auth: { username: 'id', password: 'pw-{SESSION}' }
111
123
  }
112
- http://id:pw-06cbcd75@127.0.0.1:8080
124
+ http://id:pw-527ef984@127.0.0.1:8080
113
125
  127.0.0.1
114
126
  HTTP CODE: 401 FOREIGN false
115
127
  HTTP CODE: 407 FOREIGN (Failed Proxy Auth) true
package/lib/index.js CHANGED
@@ -4,13 +4,15 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Exception = Exception;
7
+ exports.checkEmpty = checkEmpty;
7
8
  exports.cookieDict = cookieDict;
8
9
  exports.cookieHeader = cookieHeader;
9
10
  exports.findKeyNode = findKeyNode;
10
11
  exports.foreignHttpError = foreignHttpError;
11
12
  exports.hashBcrypt = hashBcrypt;
12
- exports.isEmpty = isEmpty;
13
13
  exports.limitString = limitString;
14
+ exports.lowerCaseFirst = lowerCaseFirst;
15
+ exports.pascalCase = pascalCase;
14
16
  exports.promiseTimeout = promiseTimeout;
15
17
  exports.randomHex = randomHex;
16
18
  exports.randomString = randomString;
@@ -20,6 +22,8 @@ exports.safeString = safeString;
20
22
  exports.sleep = sleep;
21
23
  exports.sleepMs = sleepMs;
22
24
  exports.time = time;
25
+ exports.titleString = titleString;
26
+ exports.upperCaseFirst = upperCaseFirst;
23
27
  exports.verifyBcrypt = verifyBcrypt;
24
28
  var _ = _interopRequireWildcard(require("lodash"));
25
29
  var _xss = _interopRequireDefault(require("xss"));
@@ -28,11 +32,12 @@ var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
28
32
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
33
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
30
34
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
31
- function Exception(message, response = {}) {
35
+ function Exception(message, response = {}, name = null) {
32
36
  response.status = response.status || 400;
33
37
  return {
34
- message: message,
35
- response: response
38
+ name: pascalCase(name),
39
+ message,
40
+ response
36
41
  };
37
42
  }
38
43
  function time() {
@@ -71,13 +76,28 @@ function findKeyNode(key, node, pair = null) {
71
76
  }
72
77
  return null;
73
78
  }
74
- function isEmpty(value) {
79
+ function checkEmpty(value) {
75
80
  if (typeof value === "number") {
76
81
  return value === 0;
77
82
  } else {
78
83
  return _.isEmpty(value);
79
84
  }
80
85
  }
86
+ function pascalCase(str) {
87
+ return upperCaseFirst(_.camelCase(str));
88
+ }
89
+ function upperCaseFirst(str) {
90
+ str = str || "";
91
+ return _.upperCase(str[0]) + str.slice(1);
92
+ }
93
+ function lowerCaseFirst(str) {
94
+ str = str || "";
95
+ return _.lowerCase(str[0]) + str.slice(1);
96
+ }
97
+ function titleString(str) {
98
+ str = str || "";
99
+ return str.split(' ').map(word => _.capitalize(word)).join(' ');
100
+ }
81
101
  function limitString(str, limit = 35) {
82
102
  str = str || "";
83
103
  if (str.length <= limit) {
@@ -86,8 +106,9 @@ function limitString(str, limit = 35) {
86
106
  return str.substring(0, limit - 3) + "...";
87
107
  }
88
108
  }
89
- function safeString(source) {
90
- return (0, _xss.default)(source, {
109
+ function safeString(str) {
110
+ str = str || "";
111
+ return (0, _xss.default)(str, {
91
112
  whiteList: {},
92
113
  stripIgnoreTag: true,
93
114
  stripIgnoreTagBody: ["script"]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "melperjs",
3
- "version": "2.0.1",
3
+ "version": "3.0.0",
4
4
  "description": "Nodejs module to use predefined common functions and utilities",
5
5
  "keywords": [
6
6
  "helper",
package/src/index.js CHANGED
@@ -1,13 +1,14 @@
1
- import * as _ from "lodash";
1
+ import * as _ from "lodash";
2
2
  import xss from "xss";
3
3
  import bcrypt from "bcrypt";
4
4
  import setCookieParser from "set-cookie-parser";
5
5
 
6
- export function Exception(message, response = {}) {
6
+ export function Exception(message, response = {}, name = null) {
7
7
  response.status = response.status || 400;
8
8
  return {
9
- message: message,
10
- response: response
9
+ name: pascalCase(name),
10
+ message,
11
+ response,
11
12
  }
12
13
  }
13
14
 
@@ -55,7 +56,7 @@ export function findKeyNode(key, node, pair = null) {
55
56
  return null;
56
57
  }
57
58
 
58
- export function isEmpty(value) {
59
+ export function checkEmpty(value) {
59
60
  if (typeof value === "number") {
60
61
  return value === 0;
61
62
  } else {
@@ -63,6 +64,28 @@ export function isEmpty(value) {
63
64
  }
64
65
  }
65
66
 
67
+ export function pascalCase(str){
68
+ return upperCaseFirst(_.camelCase(str));
69
+ }
70
+
71
+ export function upperCaseFirst(str) {
72
+ str = str || "";
73
+ return _.upperCase(str[0]) + str.slice(1);
74
+ }
75
+
76
+ export function lowerCaseFirst(str) {
77
+ str = str || "";
78
+ return _.lowerCase(str[0]) + str.slice(1);
79
+ }
80
+
81
+ export function titleString(str) {
82
+ str = str || "";
83
+ return str
84
+ .split(' ')
85
+ .map(word => _.capitalize(word))
86
+ .join(' ');
87
+ }
88
+
66
89
  export function limitString(str, limit = 35) {
67
90
  str = str || "";
68
91
  if (str.length <= limit) {
@@ -72,8 +95,9 @@ export function limitString(str, limit = 35) {
72
95
  }
73
96
  }
74
97
 
75
- export function safeString(source) {
76
- return xss(source, {
98
+ export function safeString(str) {
99
+ str = str || "";
100
+ return xss(str, {
77
101
  whiteList: {},
78
102
  stripIgnoreTag: true,
79
103
  stripIgnoreTagBody: ["script"]
package/test/script.js CHANGED
@@ -4,7 +4,7 @@ import axios from "axios"
4
4
 
5
5
 
6
6
  (async () => {
7
- console.log(helper.Exception("something went wrong"));
7
+ console.log(helper.Exception("something went wrong", {status: 500}, "axios error"));
8
8
  console.log(helper.time());
9
9
  await helper.sleepMs(1000);
10
10
  console.log(helper.time());
@@ -26,8 +26,12 @@ import axios from "axios"
26
26
  }
27
27
  }
28
28
  }));
29
- console.log("1 empty", helper.isEmpty(1));
30
- console.log("[] empty", helper.isEmpty([]));
29
+ console.log("str empty", helper.checkEmpty(""));
30
+ console.log("1 empty", helper.checkEmpty(1));
31
+ console.log("[] empty", helper.checkEmpty([]));
32
+ console.log(helper.upperCaseFirst("first letter upper"));
33
+ console.log(helper.lowerCaseFirst("First Letter Lower"));
34
+ console.log(helper.titleString("THIS mUsT be Title"));
31
35
  console.log(helper.limitString("LONG TEXT", 7));
32
36
  console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
33
37
  console.log(helper.randomString(32, true, true));