@oliversalzburg/js-utils 0.0.24-dev.0 → 0.0.24-dev.2

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/core.d.ts CHANGED
@@ -30,7 +30,7 @@ export type AnyFunctionReturning<TReturned = any> = (...args: Array<any>) => TRe
30
30
  * @typeParam TReturned - The type of the item returned by the function.
31
31
  * @group Types
32
32
  */
33
- export type AnyAsyncFunctionReturning<TReturned = any> = (...args: Array<any>) => TReturned;
33
+ export type AnyAsyncFunctionReturning<TReturned = any> = (...args: Array<any>) => Promise<TReturned>;
34
34
  /**
35
35
  * Describes a class "mixin", which is a function that returns a dynamically
36
36
  * constructed class, based on the passed parameters.
package/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export * from "./graphics/index.js";
8
8
  export * from "./log/index.js";
9
9
  export * from "./math/index.js";
10
10
  export * from "./nil.js";
11
+ export * from "./performance.js";
11
12
  export * from "./random.js";
12
13
  export * from "./string-formatter.js";
13
14
  export * from "./string.js";
package/index.js CHANGED
@@ -8,6 +8,7 @@ export * from "./graphics/index.js";
8
8
  export * from "./log/index.js";
9
9
  export * from "./math/index.js";
10
10
  export * from "./nil.js";
11
+ export * from "./performance.js";
11
12
  export * from "./random.js";
12
13
  export * from "./string-formatter.js";
13
14
  export * from "./string.js";
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../source/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC"}
package/math/core.d.ts CHANGED
@@ -64,3 +64,35 @@ export declare const isInteger: (value: number) => boolean;
64
64
  * @group Math
65
65
  */
66
66
  export declare const clamp: (input: number, floor: number, ceil: number) => number;
67
+ /**
68
+ * Rounds a given number to the next highest value.
69
+ * @param input - The number to round.
70
+ * @param fractionDigits - The amount of digits to retain in the fraction.
71
+ * @returns The rounded number.
72
+ * @group Math
73
+ */
74
+ export declare const ceilTo: (input: number, fractionDigits?: number) => number;
75
+ /**
76
+ * Rounds a given number to the next lowest value.
77
+ * @param input - The number to round.
78
+ * @param fractionDigits - The amount of digits to retain in the fraction.
79
+ * @returns The rounded number.
80
+ * @group Math
81
+ */
82
+ export declare const floorTo: (input: number, fractionDigits?: number) => number;
83
+ /**
84
+ * Rounds a given number to the next closest value.
85
+ * @param input - The number to round.
86
+ * @param fractionDigits - The amount of digits to retain in the fraction.
87
+ * @returns The rounded number.
88
+ * @group Math
89
+ */
90
+ export declare const roundTo: (input: number, fractionDigits?: number) => number;
91
+ /**
92
+ * Cuts off digits of a number.
93
+ * @param input - The number to round.
94
+ * @param fractionDigits - The amount of digits to retain in the fraction.
95
+ * @returns The rounded number.
96
+ * @group Math
97
+ */
98
+ export declare const truncTo: (input: number, fractionDigits?: number) => number;
package/math/core.js CHANGED
@@ -91,4 +91,48 @@ export const isInteger = (value) => {
91
91
  export const clamp = (input, floor, ceil) => {
92
92
  return Math.max(floor, Math.min(input, ceil));
93
93
  };
94
+ /**
95
+ * Rounds a given number to the next highest value.
96
+ * @param input - The number to round.
97
+ * @param fractionDigits - The amount of digits to retain in the fraction.
98
+ * @returns The rounded number.
99
+ * @group Math
100
+ */
101
+ export const ceilTo = (input, fractionDigits = 0) => {
102
+ const scale = Math.pow(10, fractionDigits);
103
+ return Math.floor(input * scale) / scale;
104
+ };
105
+ /**
106
+ * Rounds a given number to the next lowest value.
107
+ * @param input - The number to round.
108
+ * @param fractionDigits - The amount of digits to retain in the fraction.
109
+ * @returns The rounded number.
110
+ * @group Math
111
+ */
112
+ export const floorTo = (input, fractionDigits = 0) => {
113
+ const scale = Math.pow(10, fractionDigits);
114
+ return Math.floor(input * scale) / scale;
115
+ };
116
+ /**
117
+ * Rounds a given number to the next closest value.
118
+ * @param input - The number to round.
119
+ * @param fractionDigits - The amount of digits to retain in the fraction.
120
+ * @returns The rounded number.
121
+ * @group Math
122
+ */
123
+ export const roundTo = (input, fractionDigits = 0) => {
124
+ const scale = Math.pow(10, fractionDigits);
125
+ return Math.round(input * scale) / scale;
126
+ };
127
+ /**
128
+ * Cuts off digits of a number.
129
+ * @param input - The number to round.
130
+ * @param fractionDigits - The amount of digits to retain in the fraction.
131
+ * @returns The rounded number.
132
+ * @group Math
133
+ */
134
+ export const truncTo = (input, fractionDigits = 0) => {
135
+ const scale = Math.pow(10, fractionDigits);
136
+ return Math.trunc(input * scale) / scale;
137
+ };
94
138
  //# sourceMappingURL=core.js.map
package/math/core.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"core.js","sourceRoot":"","sources":["../../source/math/core.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,cAAc,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,C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,CAAC;IAC3C,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC;AAC3C,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@oliversalzburg/js-utils",
4
- "version": "0.0.24-dev.0",
4
+ "version": "0.0.24-dev.2",
5
5
  "license": "MIT",
6
6
  "author": "Oliver Salzburg <oliver.salzburg@gmail.com>",
7
7
  "repository": {
@@ -35,7 +35,7 @@
35
35
  "@types/eslint": "8.44.7",
36
36
  "@types/mocha": "10.0.6",
37
37
  "@types/node": "20.10.0",
38
- "@types/web": "0.0.120",
38
+ "@types/web": "0.0.121",
39
39
  "@typescript-eslint/eslint-plugin": "6.12.0",
40
40
  "@typescript-eslint/parser": "6.12.0",
41
41
  "c8": "8.0.1",
@@ -0,0 +1,16 @@
1
+ import { AnyAsyncFunctionReturning, AnyFunctionReturning } from "./core.js";
2
+ /**
3
+ * Executes the given function and measures how long the execution takes.
4
+ * @param context - The function of which the execution speed should be measured.
5
+ * @returns A tuple with the result of the function, and the execution time in milliseconds.
6
+ * @group Performance
7
+ */
8
+ export declare const measure: <TReturn>(context: AnyFunctionReturning<TReturn>) => [TReturn, number];
9
+ /**
10
+ * Executes the given async function and measures how long the execution takes.
11
+ * @param context - The async function of which the execution speed should be measured.
12
+ * @returns A tuple with the result of the function, and the execution time in milliseconds.
13
+ * @group Performance
14
+ * @group Async
15
+ */
16
+ export declare const measureAsync: <TReturn>(context: AnyAsyncFunctionReturning<TReturn>) => Promise<[TReturn, number]>;
package/performance.js ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Executes the given function and measures how long the execution takes.
3
+ * @param context - The function of which the execution speed should be measured.
4
+ * @returns A tuple with the result of the function, and the execution time in milliseconds.
5
+ * @group Performance
6
+ */
7
+ export const measure = (context) => {
8
+ const entry = performance.now();
9
+ return [context(), performance.now() - entry];
10
+ };
11
+ /**
12
+ * Executes the given async function and measures how long the execution takes.
13
+ * @param context - The async function of which the execution speed should be measured.
14
+ * @returns A tuple with the result of the function, and the execution time in milliseconds.
15
+ * @group Performance
16
+ * @group Async
17
+ */
18
+ export const measureAsync = async (context) => {
19
+ const entry = performance.now();
20
+ return [await context(), performance.now() - entry];
21
+ };
22
+ //# sourceMappingURL=performance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"performance.js","sourceRoot":"","sources":["../source/performance.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAU,OAAsC,EAAqB,EAAE;IAC5F,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IAChC,OAAO,CAAC,OAAO,EAAE,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC;AAChD,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAC/B,OAA2C,EACf,EAAE;IAC9B,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IAChC,OAAO,CAAC,MAAM,OAAO,EAAE,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC;AACtD,CAAC,CAAC"}