@luxass/utils 1.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/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/chunk-JVIVQFL7.mjs +38 -0
- package/dist/chunk-OKC7RR3A.mjs +15 -0
- package/dist/guards.cjs +20 -0
- package/dist/guards.d.cts +58 -0
- package/dist/guards.d.ts +58 -0
- package/dist/guards.mjs +1 -0
- package/dist/index.cjs +62 -0
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +2 -0
- package/dist/string.cjs +44 -0
- package/dist/string.d.cts +79 -0
- package/dist/string.d.ts +79 -0
- package/dist/string.mjs +1 -0
- package/dist/types.cjs +2 -0
- package/dist/types.d.cts +66 -0
- package/dist/types.d.ts +66 -0
- package/dist/types.mjs +1 -0
- package/package.json +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-PRESENT Lucas Nørgård
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# utils
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
|
|
6
|
+
## 📦 Installation
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @luxass/utils
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 📚 Usage
|
|
13
|
+
|
|
14
|
+
You can view the documentation [here](https://www.jsdocs.io/package/@luxass/utils).
|
|
15
|
+
|
|
16
|
+
## 📄 License
|
|
17
|
+
|
|
18
|
+
Published under [MIT License](./LICENSE).
|
|
19
|
+
|
|
20
|
+
<!-- Badges -->
|
|
21
|
+
|
|
22
|
+
[npm-version-src]: https://img.shields.io/npm/v/@luxass/utils?style=flat&colorA=18181B&colorB=4169E1
|
|
23
|
+
[npm-version-href]: https://npmjs.com/package/@luxass/utils
|
|
24
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@luxass/utils?style=flat&colorA=18181B&colorB=4169E1
|
|
25
|
+
[npm-downloads-href]: https://npmjs.com/package/@luxass/utils
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// src/string.ts
|
|
2
|
+
function capitalize(str) {
|
|
3
|
+
if (typeof str !== "string")
|
|
4
|
+
throw new TypeError("Expected a string");
|
|
5
|
+
if (str.trim().length === 0)
|
|
6
|
+
return "";
|
|
7
|
+
return str[0].toUpperCase() + str.slice(1).toLowerCase();
|
|
8
|
+
}
|
|
9
|
+
function toCamelCase(str) {
|
|
10
|
+
if (typeof str !== "string")
|
|
11
|
+
throw new TypeError("Expected a string");
|
|
12
|
+
if (str.trim().length === 0)
|
|
13
|
+
return "";
|
|
14
|
+
return str.toLowerCase().replace(/[-_](.)/g, (_, c) => c.toUpperCase());
|
|
15
|
+
}
|
|
16
|
+
function toKebabCase(str) {
|
|
17
|
+
if (typeof str !== "string")
|
|
18
|
+
throw new TypeError("Expected a string");
|
|
19
|
+
if (str.trim().length === 0)
|
|
20
|
+
return "";
|
|
21
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
|
|
22
|
+
}
|
|
23
|
+
function toPascalCase(str) {
|
|
24
|
+
if (typeof str !== "string")
|
|
25
|
+
throw new TypeError("Expected a string");
|
|
26
|
+
if (str.trim().length === 0)
|
|
27
|
+
return "";
|
|
28
|
+
return str.toLowerCase().replace(/[-_](.)/g, (_, c) => c.toUpperCase()).replace(/^[a-z]/, (c) => c.toUpperCase());
|
|
29
|
+
}
|
|
30
|
+
function toSnakeCase(str) {
|
|
31
|
+
if (typeof str !== "string")
|
|
32
|
+
throw new TypeError("Expected a string");
|
|
33
|
+
if (str.trim().length === 0)
|
|
34
|
+
return "";
|
|
35
|
+
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { capitalize, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/guards.ts
|
|
2
|
+
function isNotNullish(v) {
|
|
3
|
+
return v != null;
|
|
4
|
+
}
|
|
5
|
+
function isNotNull(v) {
|
|
6
|
+
return v !== null;
|
|
7
|
+
}
|
|
8
|
+
function isNotUndefined(v) {
|
|
9
|
+
return v !== void 0;
|
|
10
|
+
}
|
|
11
|
+
function isTruthy(v) {
|
|
12
|
+
return Boolean(v);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { isNotNull, isNotNullish, isNotUndefined, isTruthy };
|
package/dist/guards.cjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/guards.ts
|
|
4
|
+
function isNotNullish(v) {
|
|
5
|
+
return v != null;
|
|
6
|
+
}
|
|
7
|
+
function isNotNull(v) {
|
|
8
|
+
return v !== null;
|
|
9
|
+
}
|
|
10
|
+
function isNotUndefined(v) {
|
|
11
|
+
return v !== void 0;
|
|
12
|
+
}
|
|
13
|
+
function isTruthy(v) {
|
|
14
|
+
return Boolean(v);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.isNotNull = isNotNull;
|
|
18
|
+
exports.isNotNullish = isNotNullish;
|
|
19
|
+
exports.isNotUndefined = isNotUndefined;
|
|
20
|
+
exports.isTruthy = isTruthy;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a value is not null or undefined.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the input value.
|
|
5
|
+
* @param {T | null | undefined} v - The value to check for not being null or undefined.
|
|
6
|
+
* @returns {v is NonNullable<T>} - True if the value is not null or undefined, false otherwise.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isNotNullish)
|
|
11
|
+
* // [true, false, 0, 1, "", "hello"]
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare function isNotNullish<T>(v: T | null | undefined): v is NonNullable<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Checks if a value is not null.
|
|
17
|
+
*
|
|
18
|
+
* @template T - The type of the input value.
|
|
19
|
+
* @param {T | null} v - The value to check for not being null.
|
|
20
|
+
* @returns {v is Exclude<T, null>} True if the value is not null, false otherwise.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isNotNull)
|
|
25
|
+
* // [true, false, 0, 1, "", "hello", undefined]
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function isNotNull<T>(v: T | null): v is Exclude<T, null>;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a value is defined.
|
|
31
|
+
*
|
|
32
|
+
* @template T - The type of the input value.
|
|
33
|
+
* @param {T} v - The value to check for being defined.
|
|
34
|
+
* @returns {v is Exclude<T, undefined>} True if the value is defined, false otherwise.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isDefined)
|
|
39
|
+
* // [true, false, 0, 1, "", "hello", null]
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare function isNotUndefined<T>(v: T): v is Exclude<T, undefined>;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if a value is truthy, excluding null and undefined.
|
|
45
|
+
*
|
|
46
|
+
* @template T - The type of the input value.
|
|
47
|
+
* @param {T} v - The value to check for truthiness.
|
|
48
|
+
* @returns {v is NonNullable<T>} True if the value is truthy, false otherwise.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isTruthy)
|
|
53
|
+
* // [true, 1, "hello"]
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare function isTruthy<T>(v: T): v is NonNullable<T>;
|
|
57
|
+
|
|
58
|
+
export { isNotNull, isNotNullish, isNotUndefined, isTruthy };
|
package/dist/guards.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a value is not null or undefined.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the input value.
|
|
5
|
+
* @param {T | null | undefined} v - The value to check for not being null or undefined.
|
|
6
|
+
* @returns {v is NonNullable<T>} - True if the value is not null or undefined, false otherwise.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isNotNullish)
|
|
11
|
+
* // [true, false, 0, 1, "", "hello"]
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare function isNotNullish<T>(v: T | null | undefined): v is NonNullable<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Checks if a value is not null.
|
|
17
|
+
*
|
|
18
|
+
* @template T - The type of the input value.
|
|
19
|
+
* @param {T | null} v - The value to check for not being null.
|
|
20
|
+
* @returns {v is Exclude<T, null>} True if the value is not null, false otherwise.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isNotNull)
|
|
25
|
+
* // [true, false, 0, 1, "", "hello", undefined]
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare function isNotNull<T>(v: T | null): v is Exclude<T, null>;
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a value is defined.
|
|
31
|
+
*
|
|
32
|
+
* @template T - The type of the input value.
|
|
33
|
+
* @param {T} v - The value to check for being defined.
|
|
34
|
+
* @returns {v is Exclude<T, undefined>} True if the value is defined, false otherwise.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isDefined)
|
|
39
|
+
* // [true, false, 0, 1, "", "hello", null]
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare function isNotUndefined<T>(v: T): v is Exclude<T, undefined>;
|
|
43
|
+
/**
|
|
44
|
+
* Checks if a value is truthy, excluding null and undefined.
|
|
45
|
+
*
|
|
46
|
+
* @template T - The type of the input value.
|
|
47
|
+
* @param {T} v - The value to check for truthiness.
|
|
48
|
+
* @returns {v is NonNullable<T>} True if the value is truthy, false otherwise.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isTruthy)
|
|
53
|
+
* // [true, 1, "hello"]
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare function isTruthy<T>(v: T): v is NonNullable<T>;
|
|
57
|
+
|
|
58
|
+
export { isNotNull, isNotNullish, isNotUndefined, isTruthy };
|
package/dist/guards.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isNotNull, isNotNullish, isNotUndefined, isTruthy } from './chunk-OKC7RR3A.mjs';
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/guards.ts
|
|
4
|
+
function isNotNullish(v) {
|
|
5
|
+
return v != null;
|
|
6
|
+
}
|
|
7
|
+
function isNotNull(v) {
|
|
8
|
+
return v !== null;
|
|
9
|
+
}
|
|
10
|
+
function isNotUndefined(v) {
|
|
11
|
+
return v !== void 0;
|
|
12
|
+
}
|
|
13
|
+
function isTruthy(v) {
|
|
14
|
+
return Boolean(v);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/string.ts
|
|
18
|
+
function capitalize(str) {
|
|
19
|
+
if (typeof str !== "string")
|
|
20
|
+
throw new TypeError("Expected a string");
|
|
21
|
+
if (str.trim().length === 0)
|
|
22
|
+
return "";
|
|
23
|
+
return str[0].toUpperCase() + str.slice(1).toLowerCase();
|
|
24
|
+
}
|
|
25
|
+
function toCamelCase(str) {
|
|
26
|
+
if (typeof str !== "string")
|
|
27
|
+
throw new TypeError("Expected a string");
|
|
28
|
+
if (str.trim().length === 0)
|
|
29
|
+
return "";
|
|
30
|
+
return str.toLowerCase().replace(/[-_](.)/g, (_, c) => c.toUpperCase());
|
|
31
|
+
}
|
|
32
|
+
function toKebabCase(str) {
|
|
33
|
+
if (typeof str !== "string")
|
|
34
|
+
throw new TypeError("Expected a string");
|
|
35
|
+
if (str.trim().length === 0)
|
|
36
|
+
return "";
|
|
37
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
|
|
38
|
+
}
|
|
39
|
+
function toPascalCase(str) {
|
|
40
|
+
if (typeof str !== "string")
|
|
41
|
+
throw new TypeError("Expected a string");
|
|
42
|
+
if (str.trim().length === 0)
|
|
43
|
+
return "";
|
|
44
|
+
return str.toLowerCase().replace(/[-_](.)/g, (_, c) => c.toUpperCase()).replace(/^[a-z]/, (c) => c.toUpperCase());
|
|
45
|
+
}
|
|
46
|
+
function toSnakeCase(str) {
|
|
47
|
+
if (typeof str !== "string")
|
|
48
|
+
throw new TypeError("Expected a string");
|
|
49
|
+
if (str.trim().length === 0)
|
|
50
|
+
return "";
|
|
51
|
+
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.capitalize = capitalize;
|
|
55
|
+
exports.isNotNull = isNotNull;
|
|
56
|
+
exports.isNotNullish = isNotNullish;
|
|
57
|
+
exports.isNotUndefined = isNotUndefined;
|
|
58
|
+
exports.isTruthy = isTruthy;
|
|
59
|
+
exports.toCamelCase = toCamelCase;
|
|
60
|
+
exports.toKebabCase = toKebabCase;
|
|
61
|
+
exports.toPascalCase = toPascalCase;
|
|
62
|
+
exports.toSnakeCase = toSnakeCase;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish } from './types.cjs';
|
|
2
|
+
export { isNotNull, isNotNullish, isNotUndefined, isTruthy } from './guards.cjs';
|
|
3
|
+
export { capitalize, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from './string.cjs';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish } from './types.js';
|
|
2
|
+
export { isNotNull, isNotNullish, isNotUndefined, isTruthy } from './guards.js';
|
|
3
|
+
export { capitalize, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from './string.js';
|
package/dist/index.mjs
ADDED
package/dist/string.cjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/string.ts
|
|
4
|
+
function capitalize(str) {
|
|
5
|
+
if (typeof str !== "string")
|
|
6
|
+
throw new TypeError("Expected a string");
|
|
7
|
+
if (str.trim().length === 0)
|
|
8
|
+
return "";
|
|
9
|
+
return str[0].toUpperCase() + str.slice(1).toLowerCase();
|
|
10
|
+
}
|
|
11
|
+
function toCamelCase(str) {
|
|
12
|
+
if (typeof str !== "string")
|
|
13
|
+
throw new TypeError("Expected a string");
|
|
14
|
+
if (str.trim().length === 0)
|
|
15
|
+
return "";
|
|
16
|
+
return str.toLowerCase().replace(/[-_](.)/g, (_, c) => c.toUpperCase());
|
|
17
|
+
}
|
|
18
|
+
function toKebabCase(str) {
|
|
19
|
+
if (typeof str !== "string")
|
|
20
|
+
throw new TypeError("Expected a string");
|
|
21
|
+
if (str.trim().length === 0)
|
|
22
|
+
return "";
|
|
23
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
|
|
24
|
+
}
|
|
25
|
+
function toPascalCase(str) {
|
|
26
|
+
if (typeof str !== "string")
|
|
27
|
+
throw new TypeError("Expected a string");
|
|
28
|
+
if (str.trim().length === 0)
|
|
29
|
+
return "";
|
|
30
|
+
return str.toLowerCase().replace(/[-_](.)/g, (_, c) => c.toUpperCase()).replace(/^[a-z]/, (c) => c.toUpperCase());
|
|
31
|
+
}
|
|
32
|
+
function toSnakeCase(str) {
|
|
33
|
+
if (typeof str !== "string")
|
|
34
|
+
throw new TypeError("Expected a string");
|
|
35
|
+
if (str.trim().length === 0)
|
|
36
|
+
return "";
|
|
37
|
+
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.capitalize = capitalize;
|
|
41
|
+
exports.toCamelCase = toCamelCase;
|
|
42
|
+
exports.toKebabCase = toKebabCase;
|
|
43
|
+
exports.toPascalCase = toPascalCase;
|
|
44
|
+
exports.toSnakeCase = toSnakeCase;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First letter uppercase, other lowercase
|
|
3
|
+
* @param {string} str - The string to capitalize
|
|
4
|
+
* @returns {string} The capitalized string
|
|
5
|
+
* @throws {TypeError} If `str` is not string
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* capitalize("hello")
|
|
10
|
+
* // "Hello"
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
declare function capitalize(str: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Converts a string to camel case.
|
|
16
|
+
* @param {string} str - The input string to be converted.
|
|
17
|
+
* @returns {string} The camel cased string.
|
|
18
|
+
* @throws {TypeError} If the input is not a string.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* toCamelCase("some_text_here")
|
|
23
|
+
* // "someTextHere"
|
|
24
|
+
*
|
|
25
|
+
* toCamelCase("another-Example")
|
|
26
|
+
* // "anotherExample"
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
declare function toCamelCase(str: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Converts a string to kebab case.
|
|
32
|
+
* @param {string} str - The input string to be converted.
|
|
33
|
+
* @returns {string} The kebab cased string.
|
|
34
|
+
* @throws {TypeError} If the input is not a string.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* toKebabCase("someTextHere")
|
|
39
|
+
* // "some-text-here"
|
|
40
|
+
*
|
|
41
|
+
* toKebabCase("anotherExample")
|
|
42
|
+
* // "another-example"
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function toKebabCase(str: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Converts a string to pascal case.
|
|
48
|
+
* @param {string} str - The input string to be converted.
|
|
49
|
+
* @returns {string} The pascal cased string.
|
|
50
|
+
* @throws {TypeError} If the input is not a string.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* toPascalCase("some_text_here")
|
|
55
|
+
* // "SomeTextHere"
|
|
56
|
+
*
|
|
57
|
+
* toPascalCase("another-Example")
|
|
58
|
+
* // "AnotherExample"
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function toPascalCase(str: string): string;
|
|
62
|
+
/**
|
|
63
|
+
* Converts a string to snake case.
|
|
64
|
+
* @param {string} str - The input string to be converted.
|
|
65
|
+
* @returns {string} The snake cased string.
|
|
66
|
+
* @throws {TypeError} If the input is not a string.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* toSnakeCase("someTextHere")
|
|
71
|
+
* // "some_text_here"
|
|
72
|
+
*
|
|
73
|
+
* toSnakeCase("anotherExample")
|
|
74
|
+
* // "another_example"
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare function toSnakeCase(str: string): string;
|
|
78
|
+
|
|
79
|
+
export { capitalize, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
package/dist/string.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First letter uppercase, other lowercase
|
|
3
|
+
* @param {string} str - The string to capitalize
|
|
4
|
+
* @returns {string} The capitalized string
|
|
5
|
+
* @throws {TypeError} If `str` is not string
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* capitalize("hello")
|
|
10
|
+
* // "Hello"
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
declare function capitalize(str: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Converts a string to camel case.
|
|
16
|
+
* @param {string} str - The input string to be converted.
|
|
17
|
+
* @returns {string} The camel cased string.
|
|
18
|
+
* @throws {TypeError} If the input is not a string.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* toCamelCase("some_text_here")
|
|
23
|
+
* // "someTextHere"
|
|
24
|
+
*
|
|
25
|
+
* toCamelCase("another-Example")
|
|
26
|
+
* // "anotherExample"
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
declare function toCamelCase(str: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Converts a string to kebab case.
|
|
32
|
+
* @param {string} str - The input string to be converted.
|
|
33
|
+
* @returns {string} The kebab cased string.
|
|
34
|
+
* @throws {TypeError} If the input is not a string.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* toKebabCase("someTextHere")
|
|
39
|
+
* // "some-text-here"
|
|
40
|
+
*
|
|
41
|
+
* toKebabCase("anotherExample")
|
|
42
|
+
* // "another-example"
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function toKebabCase(str: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Converts a string to pascal case.
|
|
48
|
+
* @param {string} str - The input string to be converted.
|
|
49
|
+
* @returns {string} The pascal cased string.
|
|
50
|
+
* @throws {TypeError} If the input is not a string.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* toPascalCase("some_text_here")
|
|
55
|
+
* // "SomeTextHere"
|
|
56
|
+
*
|
|
57
|
+
* toPascalCase("another-Example")
|
|
58
|
+
* // "AnotherExample"
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function toPascalCase(str: string): string;
|
|
62
|
+
/**
|
|
63
|
+
* Converts a string to snake case.
|
|
64
|
+
* @param {string} str - The input string to be converted.
|
|
65
|
+
* @returns {string} The snake cased string.
|
|
66
|
+
* @throws {TypeError} If the input is not a string.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* toSnakeCase("someTextHere")
|
|
71
|
+
* // "some_text_here"
|
|
72
|
+
*
|
|
73
|
+
* toSnakeCase("anotherExample")
|
|
74
|
+
* // "another_example"
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare function toSnakeCase(str: string): string;
|
|
78
|
+
|
|
79
|
+
export { capitalize, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
package/dist/string.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { capitalize, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from './chunk-JVIVQFL7.mjs';
|
package/dist/types.cjs
ADDED
package/dist/types.d.cts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whatever type, or Promise of that type
|
|
3
|
+
* @param T - Type
|
|
4
|
+
* @returns T or Promise<T>
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* type A = Awaitable<string>
|
|
9
|
+
* // string | Promise<string>
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
type Awaitable<T> = T | PromiseLike<T>;
|
|
13
|
+
/**
|
|
14
|
+
* Whatever type, or null
|
|
15
|
+
* @param T - Type
|
|
16
|
+
* @returns T or null
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* type A = Nullable<string>
|
|
21
|
+
* // string | null
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
type Nullable<T> = T | null;
|
|
25
|
+
/**
|
|
26
|
+
* Whatever type, null or undefined
|
|
27
|
+
* @param T - Type
|
|
28
|
+
* @returns T, undefined or null
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* type A = Nullish<string>
|
|
33
|
+
* // string | null | undefined
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
type Nullish<T> = T | null | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Array, or not yet
|
|
39
|
+
*/
|
|
40
|
+
type Arrayable<T> = T | Array<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Infers the element type of an array
|
|
43
|
+
* @param T - Array type
|
|
44
|
+
* @returns The inferred element type
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* type A = ElementOf<string[]>
|
|
49
|
+
* // string
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
type ElementOf<T> = T extends (infer E)[] ? E : never;
|
|
53
|
+
/**
|
|
54
|
+
* Infers the arguments type of a function
|
|
55
|
+
* @param T - Function type
|
|
56
|
+
* @returns The inferred arguments type
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* type A = InferArguments<(a: string, b: number) => void>
|
|
61
|
+
* // [string, number]
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
type InferArguments<T> = T extends ((...args: infer A) => any) ? A : never;
|
|
65
|
+
|
|
66
|
+
export type { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whatever type, or Promise of that type
|
|
3
|
+
* @param T - Type
|
|
4
|
+
* @returns T or Promise<T>
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* type A = Awaitable<string>
|
|
9
|
+
* // string | Promise<string>
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
type Awaitable<T> = T | PromiseLike<T>;
|
|
13
|
+
/**
|
|
14
|
+
* Whatever type, or null
|
|
15
|
+
* @param T - Type
|
|
16
|
+
* @returns T or null
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* type A = Nullable<string>
|
|
21
|
+
* // string | null
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
type Nullable<T> = T | null;
|
|
25
|
+
/**
|
|
26
|
+
* Whatever type, null or undefined
|
|
27
|
+
* @param T - Type
|
|
28
|
+
* @returns T, undefined or null
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* type A = Nullish<string>
|
|
33
|
+
* // string | null | undefined
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
type Nullish<T> = T | null | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Array, or not yet
|
|
39
|
+
*/
|
|
40
|
+
type Arrayable<T> = T | Array<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Infers the element type of an array
|
|
43
|
+
* @param T - Array type
|
|
44
|
+
* @returns The inferred element type
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* type A = ElementOf<string[]>
|
|
49
|
+
* // string
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
type ElementOf<T> = T extends (infer E)[] ? E : never;
|
|
53
|
+
/**
|
|
54
|
+
* Infers the arguments type of a function
|
|
55
|
+
* @param T - Function type
|
|
56
|
+
* @returns The inferred arguments type
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* type A = InferArguments<(a: string, b: number) => void>
|
|
61
|
+
* // [string, number]
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
type InferArguments<T> = T extends ((...args: infer A) => any) ? A : never;
|
|
65
|
+
|
|
66
|
+
export type { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish };
|
package/dist/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@luxass/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A collection of utilities for JavaScript/TypeScript",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Lucas Nørgård",
|
|
8
|
+
"email": "lucasnrgaard@gmail.com",
|
|
9
|
+
"url": "https://luxass.dev"
|
|
10
|
+
},
|
|
11
|
+
"packageManager": "pnpm@8.12.0",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"homepage": "https://github.com/luxass/utils",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/luxass/utils"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/luxass/utils/issues"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"utils"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.mjs"
|
|
30
|
+
},
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./dist/index.d.cts",
|
|
33
|
+
"default": "./dist/index.cjs"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"./guards": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/guards.d.ts",
|
|
39
|
+
"default": "./dist/guards.mjs"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/guards.d.cts",
|
|
43
|
+
"default": "./dist/guards.cjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"./types": {
|
|
47
|
+
"import": {
|
|
48
|
+
"types": "./dist/types.d.ts",
|
|
49
|
+
"default": "./dist/types.mjs"
|
|
50
|
+
},
|
|
51
|
+
"require": {
|
|
52
|
+
"types": "./dist/types.d.cts",
|
|
53
|
+
"default": "./dist/types.cjs"
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"./package.json": "./package.json"
|
|
57
|
+
},
|
|
58
|
+
"main": "./dist/index.cjs",
|
|
59
|
+
"module": "./dist/index.mjs",
|
|
60
|
+
"types": "./dist/index.d.ts",
|
|
61
|
+
"files": [
|
|
62
|
+
"dist"
|
|
63
|
+
],
|
|
64
|
+
"engines": {
|
|
65
|
+
"node": ">=18"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "tsup",
|
|
69
|
+
"dev": "tsup --watch",
|
|
70
|
+
"test": "vitest --run",
|
|
71
|
+
"test:watch": "vitest",
|
|
72
|
+
"lint": "eslint .",
|
|
73
|
+
"typecheck": "tsc --noEmit"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@luxass/eslint-config": "^4.0.0-beta.16",
|
|
77
|
+
"@types/node": "^20.10.4",
|
|
78
|
+
"eslint": "^8.55.0",
|
|
79
|
+
"eslint-plugin-format": "^0.1.0",
|
|
80
|
+
"tsup": "^8.0.1",
|
|
81
|
+
"typescript": "^5.3.3",
|
|
82
|
+
"vitest": "^1.0.4"
|
|
83
|
+
}
|
|
84
|
+
}
|