@michaelroling/ts-library 1.0.2 → 1.0.3
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/dist/index.js +4 -1
- package/dist/json/index.js +9 -0
- package/dist/lib/index.js +17 -0
- package/dist/prototype/array.js +44 -7
- package/dist/prototype/number.js +14 -0
- package/dist/prototype/string.js +48 -0
- package/dist/types/array.d.ts +34 -7
- package/dist/types/index.d.ts +4 -2
- package/dist/types/json.d.ts +8 -0
- package/dist/types/lib.d.ts +4 -0
- package/dist/types/number.d.ts +12 -0
- package/dist/types/string.d.ts +37 -0
- package/package.json +53 -51
package/dist/index.js
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const toJsonString = (val) => JSON.stringify(val, null, 2);
|
|
2
|
+
export function toJSON(val) {
|
|
3
|
+
try {
|
|
4
|
+
return val ? { json: JSON.parse(val), isValidJson: true } : { json: null, isValidJson: false, error: "Value is null" };
|
|
5
|
+
}
|
|
6
|
+
catch (e) {
|
|
7
|
+
return { json: null, isValidJson: false, error: e };
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const isTruthy = (value) => isDefined(value) && ["1", 1, true, "true"].includes(value);
|
|
2
|
+
export const isFalsy = (value) => ["0", 0, false, "false", undefined, null].includes(value);
|
|
3
|
+
export const isDefined = (value) => value !== undefined && value !== null;
|
|
4
|
+
export const deepCopy = (value, errorHandler) => {
|
|
5
|
+
try {
|
|
6
|
+
return !isDefined(value) ? undefined : JSON.parse(JSON.stringify(value));
|
|
7
|
+
}
|
|
8
|
+
catch (err) {
|
|
9
|
+
if (errorHandler) {
|
|
10
|
+
errorHandler(err);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
// eslint-disable-next-line no-console
|
|
14
|
+
console.error(err);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export const isString = (value) => typeof value === "string";
|
package/dist/prototype/array.js
CHANGED
|
@@ -1,10 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return this
|
|
1
|
+
import "../types/array.d.ts";
|
|
2
|
+
const mrFirst = function () {
|
|
3
|
+
return this[0];
|
|
4
4
|
};
|
|
5
|
-
|
|
6
|
-
return this
|
|
5
|
+
const mrLast = function () {
|
|
6
|
+
return this[this.length - 1];
|
|
7
7
|
};
|
|
8
|
-
|
|
9
|
-
return this
|
|
8
|
+
const mrNth = function (n) {
|
|
9
|
+
return this[n];
|
|
10
10
|
};
|
|
11
|
+
const mrRemoveDuplicates = function () {
|
|
12
|
+
return this.filter((item, index) => this.indexOf(item) === index);
|
|
13
|
+
};
|
|
14
|
+
const mrTrimItems = function () {
|
|
15
|
+
return this.map((item) => (typeof item === "string" ? item.trim() : item));
|
|
16
|
+
};
|
|
17
|
+
const mrSum = function () {
|
|
18
|
+
return this.reduce((acc, val) => acc + val, 0);
|
|
19
|
+
};
|
|
20
|
+
const mrAverage = function () {
|
|
21
|
+
if (this.length === 0) {
|
|
22
|
+
return 0;
|
|
23
|
+
}
|
|
24
|
+
return this.mrSum() / this.length;
|
|
25
|
+
};
|
|
26
|
+
const mrMin = function () {
|
|
27
|
+
return this.reduce((acc, val) => (val < acc ? val : acc), this[0]);
|
|
28
|
+
};
|
|
29
|
+
const mrMax = function () {
|
|
30
|
+
return this.reduce((acc, val) => (val > acc ? val : acc), this[0]);
|
|
31
|
+
};
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
33
|
+
const arrayPrototypeMethods = [
|
|
34
|
+
["mrFirst", mrFirst],
|
|
35
|
+
["mrLast", mrLast],
|
|
36
|
+
["mrNth", mrNth],
|
|
37
|
+
["mrRemoveDuplicates", mrRemoveDuplicates],
|
|
38
|
+
["mrTrimItems", mrTrimItems],
|
|
39
|
+
["mrSum", mrSum],
|
|
40
|
+
["mrAverage", mrAverage],
|
|
41
|
+
["mrMin", mrMin],
|
|
42
|
+
["mrMax", mrMax],
|
|
43
|
+
];
|
|
44
|
+
for (const [name, fn] of arrayPrototypeMethods) {
|
|
45
|
+
// @ts-expect-error: Prototype-Erweiterung
|
|
46
|
+
Array.prototype[name] = fn;
|
|
47
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "../types/number.d.ts";
|
|
2
|
+
const mrPad = function (length = 2) {
|
|
3
|
+
if (this < 0) {
|
|
4
|
+
return `-${(this * -1).toString().padStart(length - 1, "0")}`;
|
|
5
|
+
}
|
|
6
|
+
return this.toString().padStart(length, "0");
|
|
7
|
+
};
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
9
|
+
const numberPrototypeMethods = [["mrPad", mrPad]];
|
|
10
|
+
for (const [name, fn] of numberPrototypeMethods) {
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
12
|
+
// @ts-expect-error
|
|
13
|
+
Number.prototype[name] = fn;
|
|
14
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import "../types/string.d.ts";
|
|
2
|
+
const mrReplaceAll = function (searched, replacement) {
|
|
3
|
+
const escapedSearchValue = searched.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4
|
+
return this.replace(new RegExp(escapedSearchValue, "g"), replacement);
|
|
5
|
+
};
|
|
6
|
+
const mrIsEmptyString = function () {
|
|
7
|
+
return this.trim() === "";
|
|
8
|
+
};
|
|
9
|
+
const mrIsBooleanString = function () {
|
|
10
|
+
return this === "true" || this === "false";
|
|
11
|
+
};
|
|
12
|
+
const mrDecomposeText = function (first, second) {
|
|
13
|
+
const startIndex = this.indexOf(first);
|
|
14
|
+
const endIndex = this.indexOf(second, startIndex);
|
|
15
|
+
const substring = this.substring(startIndex, endIndex + second.length);
|
|
16
|
+
const substringExcludeSearch = substring.mrStringReplacer([
|
|
17
|
+
{ val: first, newValue: "" },
|
|
18
|
+
{ val: second, newValue: "" },
|
|
19
|
+
]);
|
|
20
|
+
const textExcludeSubstring = this.replace(substring, "").trim();
|
|
21
|
+
return {
|
|
22
|
+
startIndex,
|
|
23
|
+
endIndex,
|
|
24
|
+
substring,
|
|
25
|
+
textExcludeSubstring,
|
|
26
|
+
substringExcludeSearch,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const mrStringReplacer = function (valueToReplace) {
|
|
30
|
+
return valueToReplace.reduce((acc, { val, newValue }) => acc.replace(val, newValue ?? ""), this);
|
|
31
|
+
};
|
|
32
|
+
const mrFirstLetterToUpperCase = function () {
|
|
33
|
+
return this.slice(0, 1).toUpperCase() + this.slice(1);
|
|
34
|
+
};
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
36
|
+
const stringPrototypeMethods = [
|
|
37
|
+
["mrReplaceAll", mrReplaceAll],
|
|
38
|
+
["mrIsEmptyString", mrIsEmptyString],
|
|
39
|
+
["mrIsBooleanString", mrIsBooleanString],
|
|
40
|
+
["mrDecomposeText", mrDecomposeText],
|
|
41
|
+
["mrStringReplacer", mrStringReplacer],
|
|
42
|
+
["mrFirstLetterToUpperCase", mrFirstLetterToUpperCase],
|
|
43
|
+
];
|
|
44
|
+
for (const [name, fn] of stringPrototypeMethods) {
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
46
|
+
// @ts-expect-error
|
|
47
|
+
String.prototype[name] = fn;
|
|
48
|
+
}
|
package/dist/types/array.d.ts
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
declare global {
|
|
2
|
+
interface Array {
|
|
3
|
+
/** Gets the first element of the array. */
|
|
4
|
+
mrFirst: MrFirst;
|
|
5
|
+
/** Gets the last element of the array. */
|
|
6
|
+
mrLast: MrLast;
|
|
7
|
+
/** Gets the nth element of the array. */
|
|
8
|
+
mrNth: MrNth;
|
|
9
|
+
/** Removes duplicate primitive values from the array. */
|
|
10
|
+
mrRemoveDuplicates: MrRemoveDuplicates;
|
|
11
|
+
/** Trims whitespace from string elements in the array. */
|
|
12
|
+
mrTrimItems: MrTrimItems;
|
|
13
|
+
/** Sums up the numeric elements in the array. */
|
|
14
|
+
mrSum: MrSum;
|
|
15
|
+
/** Calculates the average of the numeric elements in the array. */
|
|
16
|
+
mrAverage: MrAverage;
|
|
17
|
+
/** Finds the minimum numeric value in the array. */
|
|
18
|
+
mrMin: MrMin;
|
|
19
|
+
/** Finds the maximum numeric value in the array. */
|
|
20
|
+
mrMax: MrMax;
|
|
21
|
+
}
|
|
8
22
|
}
|
|
23
|
+
|
|
24
|
+
export type MrFirst = <T>(this: T[]) => T | undefined;
|
|
25
|
+
export type MrLast = <T>(this: T[]) => T | undefined;
|
|
26
|
+
export type MrNth = <T>(this: T[], n: number) => T | undefined;
|
|
27
|
+
export type MrRemoveDuplicates = (this: Primitive[]) => Primitive[];
|
|
28
|
+
export type MrTrimItems = (this: Primitive[]) => Primitive[];
|
|
29
|
+
export type MrSum = (this: number[]) => number;
|
|
30
|
+
export type MrAverage = (this: number[]) => number;
|
|
31
|
+
export type MrMin = (this: number[]) => number | undefined;
|
|
32
|
+
export type MrMax = (this: number[]) => number | undefined;
|
|
33
|
+
|
|
34
|
+
export type Primitive = string | number | boolean;
|
|
35
|
+
export {};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function isTruthy (value: any): boolean;
|
|
2
|
+
export declare function isFalsy (value: any): boolean;
|
|
3
|
+
export declare function isDefined <T>(value: T | undefined | null): value is T
|
|
4
|
+
export declare function deepCopy <T>(obj: T, errorHandler?:(e:any)=>void): T | undefined
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Number {
|
|
3
|
+
/** Pads a number with leading zeros to reach a specified length.
|
|
4
|
+
* @param length The desired total length of the resulting string. Default is 2.
|
|
5
|
+
*/
|
|
6
|
+
mrPad: MrPad;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type MrPad = (ThisType: number, length?: number) => string;
|
|
11
|
+
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface String {
|
|
3
|
+
/** Replaces all occurrences of a substring with a new string. */
|
|
4
|
+
mrReplaceAll: MrReplaceAll;
|
|
5
|
+
/** Checks if the string is empty or contains only whitespace characters. */
|
|
6
|
+
mrIsEmptyString: MrIsEmptyString;
|
|
7
|
+
/** Checks if the string represents a boolean value ('true' or 'false'). */
|
|
8
|
+
mrIsBooleanString: MrIsBooleanString;
|
|
9
|
+
/** Decomposes the string into parts based on two search strings. */
|
|
10
|
+
mrDecomposeText: MrDecomposeText;
|
|
11
|
+
/** Replaces multiple specified substrings with their corresponding new values. */
|
|
12
|
+
mrStringReplacer: MrStringReplacer;
|
|
13
|
+
/** Converts the first letter of the string to uppercase. */
|
|
14
|
+
mrFirstLetterToUpperCase: MrFirstLetterToUpperCase;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type MrReplaceAll = (this: string, searched: string, replacement: string) => string;
|
|
19
|
+
export type MrIsEmptyString = (this: string) => boolean;
|
|
20
|
+
export type MrIsBooleanString = (this: string) => boolean;
|
|
21
|
+
export type MrDecomposeText = (this: string, first: string, second: string) => DecomposeTextReturnType;
|
|
22
|
+
export type MrStringReplacer = (this: string, valueToReplace: StringReplacerObj[]) => string;
|
|
23
|
+
export type MrFirstLetterToUpperCase = (this: string) => string;
|
|
24
|
+
|
|
25
|
+
export interface DecomposeTextReturnType {
|
|
26
|
+
startIndex: number;
|
|
27
|
+
endIndex: number;
|
|
28
|
+
substring: string;
|
|
29
|
+
textExcludeSubstring: string;
|
|
30
|
+
substringExcludeSearch: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface StringReplacerObj {
|
|
34
|
+
val: string;
|
|
35
|
+
newValue?: string;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,51 +1,53 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@michaelroling/ts-library",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/types/index.d.ts",
|
|
7
|
-
"module": "dist/index.js",
|
|
8
|
-
"publishConfig": {
|
|
9
|
-
"access": "public"
|
|
10
|
-
},
|
|
11
|
-
"dependencies": {
|
|
12
|
-
"chai": "^6.2.2",
|
|
13
|
-
"esbuild": "^0.27.2",
|
|
14
|
-
"prettier": "3.8.0",
|
|
15
|
-
"rollup": "^4.55.1",
|
|
16
|
-
"typescript": "^5.9.3",
|
|
17
|
-
"vitest": "^4.0.17"
|
|
18
|
-
},
|
|
19
|
-
"devDependencies": {
|
|
20
|
-
"@eslint/js": "^9.39.2",
|
|
21
|
-
"@vitest/coverage-v8": "4.0.17",
|
|
22
|
-
"cpx2": "^8.0.0",
|
|
23
|
-
"eslint": "^9.39.2",
|
|
24
|
-
"globals": "^17.0.0",
|
|
25
|
-
"jiti": "^2.6.1",
|
|
26
|
-
"jsdom": "^27.4.0",
|
|
27
|
-
"typescript-eslint": "^8.53.0"
|
|
28
|
-
},
|
|
29
|
-
"scripts": {
|
|
30
|
-
"test": "vitest",
|
|
31
|
-
"coverage": "vitest run --coverage",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@michaelroling/ts-library",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/types/index.d.ts",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"chai": "^6.2.2",
|
|
13
|
+
"esbuild": "^0.27.2",
|
|
14
|
+
"prettier": "3.8.0",
|
|
15
|
+
"rollup": "^4.55.1",
|
|
16
|
+
"typescript": "^5.9.3",
|
|
17
|
+
"vitest": "^4.0.17"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@eslint/js": "^9.39.2",
|
|
21
|
+
"@vitest/coverage-v8": "4.0.17",
|
|
22
|
+
"cpx2": "^8.0.0",
|
|
23
|
+
"eslint": "^9.39.2",
|
|
24
|
+
"globals": "^17.0.0",
|
|
25
|
+
"jiti": "^2.6.1",
|
|
26
|
+
"jsdom": "^27.4.0",
|
|
27
|
+
"typescript-eslint": "^8.53.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "vitest",
|
|
31
|
+
"test:coverage": "vitest run --coverage --coverage.threshold=100",
|
|
32
|
+
"coverage": "vitest run --coverage",
|
|
33
|
+
"lint": "eslint --ext .ts src --fix",
|
|
34
|
+
"build": "tsc -p tsconfig.json && npx cpx2 \"src/types/**/*.d.ts\" dist/types",
|
|
35
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
36
|
+
"prepublishOnly": "yarn build"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/MiRo1310/ts-library.git"
|
|
46
|
+
},
|
|
47
|
+
"author": "Michael Roling",
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/MiRo1310/ts-library/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/MiRo1310/ts-library#readme"
|
|
53
|
+
}
|