@ceale/util 1.3.0 → 1.5.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
@@ -1,9 +1,3 @@
1
1
  # @ceale/util
2
2
 
3
- To install dependencies:
4
-
5
- ```bash
6
- npm install
7
- # or
8
- bun install
9
- ```
3
+ 小工具集
package/dist/cjs/index.js CHANGED
@@ -29,39 +29,155 @@ var __export = (target, all) => {
29
29
  // src/index.ts
30
30
  var exports_src = {};
31
31
  __export(exports_src, {
32
- UrlUtil: () => UrlUtil,
33
- StringUtil: () => StringUtil
32
+ URI: () => URI
34
33
  });
35
34
  module.exports = __toCommonJS(exports_src);
36
35
 
37
36
  // src/string.ts
38
- var StringUtil = (str) => ({
39
- string: str,
40
- toString: () => str,
41
- valueOf: () => str,
42
- removeStart: (searchString) => str.startsWith(searchString) ? str.slice(searchString.length) : str,
43
- removeEnd: (searchString) => str.endsWith(searchString) ? str.slice(0, -searchString.length) : str,
44
- removeStartAll(searchString) {
45
- while (str.startsWith(searchString)) {
46
- str = str.slice(searchString.length);
37
+ Object.defineProperty(String.prototype, "removePrefix", {
38
+ value: function(prefix) {
39
+ return this.startsWith(prefix) ? this.slice(prefix.length) : this;
40
+ },
41
+ writable: true,
42
+ configurable: true,
43
+ enumerable: false
44
+ });
45
+ Object.defineProperty(String.prototype, "removeSuffix", {
46
+ value: function(suffix) {
47
+ return this.endsWith(suffix) ? this.slice(0, -suffix.length) : this;
48
+ },
49
+ writable: true,
50
+ configurable: true,
51
+ enumerable: false
52
+ });
53
+ Object.defineProperty(String.prototype, "removeAllPrefixes", {
54
+ value: function(prefix) {
55
+ if (prefix.length === 0)
56
+ return this;
57
+ let str = this;
58
+ while (str.startsWith(prefix)) {
59
+ str = str.slice(prefix.length);
47
60
  }
48
61
  return str;
49
62
  },
50
- removeEndAll(searchString) {
51
- while (str.endsWith(searchString)) {
52
- str = str.slice(0, -searchString.length);
63
+ writable: true,
64
+ configurable: true,
65
+ enumerable: false
66
+ });
67
+ Object.defineProperty(String.prototype, "removeAllSuffixes", {
68
+ value: function(suffix) {
69
+ if (suffix.length === 0)
70
+ return this;
71
+ let str = this;
72
+ while (str.endsWith(suffix)) {
73
+ str = str.slice(0, -suffix.length);
53
74
  }
54
75
  return str;
55
- }
76
+ },
77
+ writable: true,
78
+ configurable: true,
79
+ enumerable: false
56
80
  });
57
- // src/url.ts
58
- var UrlUtil = (url) => ({
59
- join(...path) {
60
- return path.filter((p) => p !== "").map((p, index) => {
81
+ Object.defineProperty(String.prototype, "toCamelCase", {
82
+ value: function() {
83
+ return this.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
84
+ },
85
+ writable: true,
86
+ configurable: true,
87
+ enumerable: false
88
+ });
89
+ Object.defineProperty(String.prototype, "toKebabCase", {
90
+ value: function() {
91
+ return this.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
92
+ },
93
+ writable: true,
94
+ configurable: true,
95
+ enumerable: false
96
+ });
97
+ // src/uri.ts
98
+ var URI;
99
+ ((URI) => {
100
+ const leftSlash = /^\/(?=[^\/])/g;
101
+ const rightSlash = /(?<=[^\/])\/$/g;
102
+ const innerSlash = /(?<=[^\/])\/(?=[^\/])/g;
103
+ URI.join = (...path) => {
104
+ return path.filter((path2) => path2.length > 0).map((path2, index, pathArray) => {
61
105
  if (index === 0) {
62
- return StringUtil(p).removeEnd("/");
106
+ return path2.replace(rightSlash, "").split(innerSlash);
107
+ } else if (index === pathArray.length - 1) {
108
+ return path2.replace(leftSlash, "").split(innerSlash);
109
+ } else {
110
+ return path2.replace(leftSlash, "").replace(rightSlash, "").split(innerSlash);
111
+ }
112
+ }).flat().reduce((stack, path2) => {
113
+ if (path2 === "." || path2 === "/." || path2 === "./") {
114
+ return stack;
115
+ } else if (path2 === ".." || path2 === "/.." || path2 === "../") {
116
+ stack.pop();
117
+ return stack;
118
+ } else {
119
+ stack.push(path2);
120
+ return stack;
121
+ }
122
+ }, []).reduce((path2, segment) => {
123
+ if (path2.length === 0) {
124
+ return path2 + segment;
125
+ } else if (path2.endsWith("/") || segment.startsWith("/")) {
126
+ return path2 + segment;
127
+ } else {
128
+ return path2 + "/" + segment;
63
129
  }
64
- return "/" + StringUtil(p).removeStart("/");
65
- }).join("");
66
- }
130
+ }, "");
131
+ };
132
+ })(URI ||= {});
133
+ // src/class.ts
134
+ Object.defineProperty(Function.prototype, "isDirectSubclass", {
135
+ value: function(potentialChild) {
136
+ if (typeof potentialChild !== "function" || typeof this !== "function") {
137
+ return false;
138
+ }
139
+ return Object.getPrototypeOf(potentialChild) === this;
140
+ },
141
+ writable: true,
142
+ configurable: true,
143
+ enumerable: false
144
+ });
145
+ Object.defineProperty(Function.prototype, "isSubclass", {
146
+ value: function(potentialDescendant) {
147
+ if (typeof potentialDescendant !== "function" || typeof this !== "function") {
148
+ return false;
149
+ }
150
+ let current = potentialDescendant;
151
+ while (typeof current === "function") {
152
+ if (current === this) {
153
+ return true;
154
+ }
155
+ current = Object.getPrototypeOf(current);
156
+ if (current === Function.prototype)
157
+ break;
158
+ }
159
+ return false;
160
+ },
161
+ writable: true,
162
+ configurable: true,
163
+ enumerable: false
164
+ });
165
+ Object.defineProperty(Function.prototype, "isDirectInstance", {
166
+ value: function(potentialInstance) {
167
+ if (typeof this !== "function" || potentialInstance === null || typeof potentialInstance !== "object") {
168
+ return false;
169
+ }
170
+ return Object.getPrototypeOf(potentialInstance).constructor === this;
171
+ },
172
+ writable: true,
173
+ configurable: true,
174
+ enumerable: false
175
+ });
176
+ Object.defineProperty(Function.prototype, "isInstance", {
177
+ value: function(potentialInstance) {
178
+ return typeof this === "function" && potentialInstance instanceof this;
179
+ },
180
+ writable: true,
181
+ configurable: true,
182
+ enumerable: false
67
183
  });
package/dist/esm/index.js CHANGED
@@ -1,35 +1,151 @@
1
1
  // src/string.ts
2
- var StringUtil = (str) => ({
3
- string: str,
4
- toString: () => str,
5
- valueOf: () => str,
6
- removeStart: (searchString) => str.startsWith(searchString) ? str.slice(searchString.length) : str,
7
- removeEnd: (searchString) => str.endsWith(searchString) ? str.slice(0, -searchString.length) : str,
8
- removeStartAll(searchString) {
9
- while (str.startsWith(searchString)) {
10
- str = str.slice(searchString.length);
2
+ Object.defineProperty(String.prototype, "removePrefix", {
3
+ value: function(prefix) {
4
+ return this.startsWith(prefix) ? this.slice(prefix.length) : this;
5
+ },
6
+ writable: true,
7
+ configurable: true,
8
+ enumerable: false
9
+ });
10
+ Object.defineProperty(String.prototype, "removeSuffix", {
11
+ value: function(suffix) {
12
+ return this.endsWith(suffix) ? this.slice(0, -suffix.length) : this;
13
+ },
14
+ writable: true,
15
+ configurable: true,
16
+ enumerable: false
17
+ });
18
+ Object.defineProperty(String.prototype, "removeAllPrefixes", {
19
+ value: function(prefix) {
20
+ if (prefix.length === 0)
21
+ return this;
22
+ let str = this;
23
+ while (str.startsWith(prefix)) {
24
+ str = str.slice(prefix.length);
11
25
  }
12
26
  return str;
13
27
  },
14
- removeEndAll(searchString) {
15
- while (str.endsWith(searchString)) {
16
- str = str.slice(0, -searchString.length);
28
+ writable: true,
29
+ configurable: true,
30
+ enumerable: false
31
+ });
32
+ Object.defineProperty(String.prototype, "removeAllSuffixes", {
33
+ value: function(suffix) {
34
+ if (suffix.length === 0)
35
+ return this;
36
+ let str = this;
37
+ while (str.endsWith(suffix)) {
38
+ str = str.slice(0, -suffix.length);
17
39
  }
18
40
  return str;
19
- }
41
+ },
42
+ writable: true,
43
+ configurable: true,
44
+ enumerable: false
20
45
  });
21
- // src/url.ts
22
- var UrlUtil = (url) => ({
23
- join(...path) {
24
- return path.filter((p) => p !== "").map((p, index) => {
46
+ Object.defineProperty(String.prototype, "toCamelCase", {
47
+ value: function() {
48
+ return this.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
49
+ },
50
+ writable: true,
51
+ configurable: true,
52
+ enumerable: false
53
+ });
54
+ Object.defineProperty(String.prototype, "toKebabCase", {
55
+ value: function() {
56
+ return this.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
57
+ },
58
+ writable: true,
59
+ configurable: true,
60
+ enumerable: false
61
+ });
62
+ // src/uri.ts
63
+ var URI;
64
+ ((URI) => {
65
+ const leftSlash = /^\/(?=[^\/])/g;
66
+ const rightSlash = /(?<=[^\/])\/$/g;
67
+ const innerSlash = /(?<=[^\/])\/(?=[^\/])/g;
68
+ URI.join = (...path) => {
69
+ return path.filter((path2) => path2.length > 0).map((path2, index, pathArray) => {
25
70
  if (index === 0) {
26
- return StringUtil(p).removeEnd("/");
71
+ return path2.replace(rightSlash, "").split(innerSlash);
72
+ } else if (index === pathArray.length - 1) {
73
+ return path2.replace(leftSlash, "").split(innerSlash);
74
+ } else {
75
+ return path2.replace(leftSlash, "").replace(rightSlash, "").split(innerSlash);
76
+ }
77
+ }).flat().reduce((stack, path2) => {
78
+ if (path2 === "." || path2 === "/." || path2 === "./") {
79
+ return stack;
80
+ } else if (path2 === ".." || path2 === "/.." || path2 === "../") {
81
+ stack.pop();
82
+ return stack;
83
+ } else {
84
+ stack.push(path2);
85
+ return stack;
86
+ }
87
+ }, []).reduce((path2, segment) => {
88
+ if (path2.length === 0) {
89
+ return path2 + segment;
90
+ } else if (path2.endsWith("/") || segment.startsWith("/")) {
91
+ return path2 + segment;
92
+ } else {
93
+ return path2 + "/" + segment;
27
94
  }
28
- return "/" + StringUtil(p).removeStart("/");
29
- }).join("");
30
- }
95
+ }, "");
96
+ };
97
+ })(URI ||= {});
98
+ // src/class.ts
99
+ Object.defineProperty(Function.prototype, "isDirectSubclass", {
100
+ value: function(potentialChild) {
101
+ if (typeof potentialChild !== "function" || typeof this !== "function") {
102
+ return false;
103
+ }
104
+ return Object.getPrototypeOf(potentialChild) === this;
105
+ },
106
+ writable: true,
107
+ configurable: true,
108
+ enumerable: false
109
+ });
110
+ Object.defineProperty(Function.prototype, "isSubclass", {
111
+ value: function(potentialDescendant) {
112
+ if (typeof potentialDescendant !== "function" || typeof this !== "function") {
113
+ return false;
114
+ }
115
+ let current = potentialDescendant;
116
+ while (typeof current === "function") {
117
+ if (current === this) {
118
+ return true;
119
+ }
120
+ current = Object.getPrototypeOf(current);
121
+ if (current === Function.prototype)
122
+ break;
123
+ }
124
+ return false;
125
+ },
126
+ writable: true,
127
+ configurable: true,
128
+ enumerable: false
129
+ });
130
+ Object.defineProperty(Function.prototype, "isDirectInstance", {
131
+ value: function(potentialInstance) {
132
+ if (typeof this !== "function" || potentialInstance === null || typeof potentialInstance !== "object") {
133
+ return false;
134
+ }
135
+ return Object.getPrototypeOf(potentialInstance).constructor === this;
136
+ },
137
+ writable: true,
138
+ configurable: true,
139
+ enumerable: false
140
+ });
141
+ Object.defineProperty(Function.prototype, "isInstance", {
142
+ value: function(potentialInstance) {
143
+ return typeof this === "function" && potentialInstance instanceof this;
144
+ },
145
+ writable: true,
146
+ configurable: true,
147
+ enumerable: false
31
148
  });
32
149
  export {
33
- UrlUtil,
34
- StringUtil
150
+ URI
35
151
  };
@@ -0,0 +1,32 @@
1
+ type AnyClass = new (...args: any[]) => any;
2
+ declare global {
3
+ interface Function {
4
+ /**
5
+ * 检查 `potentialChild` 是否是当前类的直接子类。
6
+ * @param potentialChild 潜在的子类构造函数。
7
+ * @returns 如果是直接子类,则返回 `true`;否则返回 `false`。
8
+ */
9
+ isDirectSubclass(this: AnyClass, potentialChild: unknown): boolean;
10
+ /**
11
+ * 检查 `potentialDescendant` 是否是当前类的子孙类或当前类本身。
12
+ * @param potentialDescendant 潜在的子孙类或类本身。
13
+ * @returns 如果是子孙类或类本身,则返回 `true`;否则返回 `false`。
14
+ */
15
+ isSubclass(this: AnyClass, potentialDescendant: unknown): boolean;
16
+ /**
17
+ * 检查 `potentialInstance` 是否由当前类直接构造。
18
+ * 判断依据: `Object.getPrototypeOf(potentialInstance).constructor === this`。
19
+ * @param potentialInstance 潜在的实例对象。
20
+ * @returns 如果是由当前类直接创建的实例,则返回 `true`;否则返回 `false`。
21
+ */
22
+ isDirectInstance(this: AnyClass, potentialInstance: unknown): boolean;
23
+ /**
24
+ * 检查 `potentialInstance` 是否是当前类或其任何子类的实例。
25
+ * 内部使用 `instanceof` 操作符。
26
+ * @param potentialInstance 潜在的实例对象。
27
+ * @returns 如果是实例,则返回 `true`;否则返回 `false`。
28
+ */
29
+ isInstance(this: AnyClass, potentialInstance: unknown): boolean;
30
+ }
31
+ }
32
+ export {};
@@ -1,2 +1,3 @@
1
1
  export * from "./string";
2
- export * from "./url";
2
+ export * from "./uri";
3
+ export * from "./class";
@@ -1,9 +1,39 @@
1
- export declare const StringUtil: (str: string) => {
2
- string: string;
3
- toString: () => string;
4
- valueOf: () => string;
5
- removeStart: (searchString: string) => string;
6
- removeEnd: (searchString: string) => string;
7
- removeStartAll(searchString: string): string;
8
- removeEndAll(searchString: string): string;
9
- };
1
+ declare global {
2
+ interface String {
3
+ /**
4
+ * 移除字符串的前缀`prefix`,如果不以`prefix`开头,则返回原字符串
5
+ * @param prefix 指定字符串
6
+ * @returns 移除后的字符串
7
+ */
8
+ removePrefix(prefix: string): string;
9
+ /**
10
+ * 移除字符串的后缀`suffix`,如果不以`suffix`结尾,则返回原字符串
11
+ * @param suffix 待匹配的串
12
+ * @returns 移除后的字符串
13
+ */
14
+ removeSuffix(suffix: string): string;
15
+ /**
16
+ * 移除字符串连续出现的若干个前缀`prefix`,如果不以`prefix`开头,则返回原字符串
17
+ * @param prefix 待匹配的串
18
+ * @returns 移除后的字符串
19
+ */
20
+ removeAllPrefixes(prefix: string): string;
21
+ /**
22
+ * 移除字符串连续出现的若干个后缀`suffix`,如果不以`suffix`结尾,则返回原字符串
23
+ * @param suffix 待匹配的串
24
+ * @returns 移除后的字符串
25
+ */
26
+ removeAllSuffixes(suffix: string): string;
27
+ /**
28
+ * 将字符串转为驼峰命名
29
+ * @returns 驼峰命名的字符串
30
+ */
31
+ toCamelCase(): string;
32
+ /**
33
+ * 将字符串转为连字符命名
34
+ * @returns 连字符命名的字符串
35
+ */
36
+ toKebabCase(): string;
37
+ }
38
+ }
39
+ export {};
@@ -0,0 +1,3 @@
1
+ export declare namespace URI {
2
+ const join: (...path: string[]) => string;
3
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ceale/util",
3
3
  "author": "Ceale",
4
- "version": "1.3.0",
4
+ "version": "1.5.0",
5
5
  "module": "index.ts",
6
6
  "type": "module",
7
7
  "main": "dist/esm/index.js",
@@ -14,7 +14,7 @@
14
14
  "build": "bun run build:esm && bun run build:cjs && bun run build:ts",
15
15
  "build:esm": "bun build --outdir=dist/esm/ --format=esm --target=node src/index.ts",
16
16
  "build:cjs": "bun build --outdir=dist/cjs/ --format=cjs --target=node src/index.ts",
17
- "build:ts": "tsc --outDir dist/ts/"
17
+ "build:ts": "tsc --project tsconfig.build.json"
18
18
  },
19
19
  "exports": {
20
20
  ".": {
@@ -1,2 +0,0 @@
1
- export declare namespace UrlUtil {
2
- }