@hpcc-js/util 3.5.1 → 3.5.4
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 +43 -43
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +1 -1
- package/dist/index.umd.cjs.map +1 -1
- package/package.json +5 -4
- package/src/__package__.ts +3 -3
- package/src/array.ts +97 -97
- package/src/cache.ts +65 -65
- package/src/debounce.ts +88 -88
- package/src/dictionary.ts +69 -69
- package/src/dispatch.ts +125 -125
- package/src/esp.ts +32 -32
- package/src/graph.ts +353 -353
- package/src/graph2.ts +675 -675
- package/src/hashSum.ts +55 -55
- package/src/immutable.ts +156 -156
- package/src/index.ts +21 -21
- package/src/logging.ts +212 -212
- package/src/math.ts +92 -92
- package/src/object.ts +122 -122
- package/src/observer.ts +91 -91
- package/src/platform.ts +20 -20
- package/src/saxParser.ts +135 -135
- package/src/stack.ts +41 -41
- package/src/stateful.ts +178 -178
- package/src/string.ts +21 -21
- package/src/url.ts +27 -27
- package/src/utf8ToBase64.ts +47 -47
package/src/logging.ts
CHANGED
|
@@ -1,212 +1,212 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { isNode } from "./platform.ts";
|
|
3
|
-
import { Stack } from "./stack.ts";
|
|
4
|
-
|
|
5
|
-
export enum Level {
|
|
6
|
-
debug,
|
|
7
|
-
info,
|
|
8
|
-
notice,
|
|
9
|
-
warning,
|
|
10
|
-
error,
|
|
11
|
-
critical,
|
|
12
|
-
alert,
|
|
13
|
-
emergency
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const colours: { [key: string]: string } = {
|
|
17
|
-
debug: "cyan",
|
|
18
|
-
info: "green",
|
|
19
|
-
notice: "grey",
|
|
20
|
-
warning: "blue",
|
|
21
|
-
error: "red",
|
|
22
|
-
critical: "magenta",
|
|
23
|
-
alert: "magenta",
|
|
24
|
-
emergency: "magenta"
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export interface Writer {
|
|
28
|
-
write?(dateTime: string, level: Level, id: string, msg: string): void;
|
|
29
|
-
rawWrite?(dateTime: string, level: Level, id: string, msg: string | object): void;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
class ConsoleWriter implements Writer {
|
|
33
|
-
write(dateTime: string, level: Level, id: string, msg: string) {
|
|
34
|
-
if (isNode) {
|
|
35
|
-
console.log(`[${dateTime}] ${Level[level].toUpperCase()} ${id}: ${msg}`);
|
|
36
|
-
} else {
|
|
37
|
-
console.log(`[${dateTime}] %c${Level[level].toUpperCase()}%c ${id}: ${msg}`, `color:${colours[Level[level]]}`, "");
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export class Logging {
|
|
43
|
-
private static _instance: Logging;
|
|
44
|
-
private _levelStack = new Stack<Level>();
|
|
45
|
-
private _level = Level.info;
|
|
46
|
-
private _filter: string = "";
|
|
47
|
-
private _writer: Writer = new ConsoleWriter();
|
|
48
|
-
|
|
49
|
-
public static Instance() {
|
|
50
|
-
return this._instance || (this._instance = new this());
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
private constructor() {
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private stringify(obj: object): string {
|
|
57
|
-
const cache: any[] = [];
|
|
58
|
-
return JSON.stringify(obj, function (_key, value) {
|
|
59
|
-
if (typeof value === "object" && value !== null) {
|
|
60
|
-
if (cache.indexOf(value) !== -1) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
cache.push(value);
|
|
64
|
-
}
|
|
65
|
-
return value;
|
|
66
|
-
}, 2);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
writer(): Writer;
|
|
70
|
-
writer(_: Writer): Logging;
|
|
71
|
-
writer(_?: Writer): Writer | Logging {
|
|
72
|
-
if (_ === void 0) return this._writer;
|
|
73
|
-
this._writer = _;
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
log(level: Level, id: string, msg: string | object) {
|
|
78
|
-
if (level < this._level) return;
|
|
79
|
-
if (this._filter && this._filter !== id) return;
|
|
80
|
-
|
|
81
|
-
const dateTime = new Date().toISOString();
|
|
82
|
-
|
|
83
|
-
if (this._writer.rawWrite) {
|
|
84
|
-
this._writer.rawWrite(dateTime, level, id, msg);
|
|
85
|
-
} else {
|
|
86
|
-
if (typeof msg !== "string") {
|
|
87
|
-
msg = this.stringify(msg);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (this._writer.write) {
|
|
91
|
-
this._writer.write(dateTime, level, id, msg);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
debug(id: string, msg: string | object) {
|
|
97
|
-
this.log(Level.debug, id, msg);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
info(id: string, msg: string | object) {
|
|
101
|
-
this.log(Level.info, id, msg);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
notice(id: string, msg: string | object) {
|
|
105
|
-
this.log(Level.notice, id, msg);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
warning(id: string, msg: string | object) {
|
|
109
|
-
this.log(Level.warning, id, msg);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
error(id: string, msg: string | object) {
|
|
113
|
-
this.log(Level.error, id, msg);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
critical(id: string, msg: string | object) {
|
|
117
|
-
this.log(Level.critical, id, msg);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
alert(id: string, msg: string | object) {
|
|
121
|
-
this.log(Level.alert, id, msg);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
emergency(id: string, msg: string | object) {
|
|
125
|
-
this.log(Level.emergency, id, msg);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
level(): Level;
|
|
129
|
-
level(_: Level): this;
|
|
130
|
-
level(_?: Level): Level | this {
|
|
131
|
-
if (_ === void 0) return this._level;
|
|
132
|
-
this._level = _;
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
pushLevel(_: Level): this {
|
|
137
|
-
this._levelStack.push(this._level);
|
|
138
|
-
this._level = _;
|
|
139
|
-
return this;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
popLevel(): this {
|
|
143
|
-
this._level = this._levelStack.pop()!;
|
|
144
|
-
return this;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
filter(): string;
|
|
148
|
-
filter(_: string): this;
|
|
149
|
-
filter(_?: string): string | this {
|
|
150
|
-
if (_ === void 0) return this._filter;
|
|
151
|
-
this._filter = _;
|
|
152
|
-
return this;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
export const logger = Logging.Instance();
|
|
156
|
-
|
|
157
|
-
export class ScopedLogging {
|
|
158
|
-
protected _scopeID: string;
|
|
159
|
-
|
|
160
|
-
constructor(scopeID: string) {
|
|
161
|
-
this._scopeID = scopeID;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
debug(msg: string | object) {
|
|
165
|
-
logger.debug(this._scopeID, msg);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
info(msg: string | object) {
|
|
169
|
-
logger.info(this._scopeID, msg);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
notice(msg: string | object) {
|
|
173
|
-
logger.notice(this._scopeID, msg);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
warning(msg: string | object) {
|
|
177
|
-
logger.warning(this._scopeID, msg);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
error(msg: string | object) {
|
|
181
|
-
logger.error(this._scopeID, msg);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
critical(msg: string | object) {
|
|
185
|
-
logger.critical(this._scopeID, msg);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
alert(msg: string | object) {
|
|
189
|
-
logger.alert(this._scopeID, msg);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
emergency(msg: string | object) {
|
|
193
|
-
logger.emergency(this._scopeID, msg);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
pushLevel(_: Level): this {
|
|
197
|
-
logger.pushLevel(_);
|
|
198
|
-
return this;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
popLevel(): this {
|
|
202
|
-
logger.popLevel();
|
|
203
|
-
return this;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export function scopedLogger(scopeID: string, filter: boolean = false) {
|
|
208
|
-
if (filter) {
|
|
209
|
-
logger.filter(scopeID);
|
|
210
|
-
}
|
|
211
|
-
return new ScopedLogging(scopeID);
|
|
212
|
-
}
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import { isNode } from "./platform.ts";
|
|
3
|
+
import { Stack } from "./stack.ts";
|
|
4
|
+
|
|
5
|
+
export enum Level {
|
|
6
|
+
debug,
|
|
7
|
+
info,
|
|
8
|
+
notice,
|
|
9
|
+
warning,
|
|
10
|
+
error,
|
|
11
|
+
critical,
|
|
12
|
+
alert,
|
|
13
|
+
emergency
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const colours: { [key: string]: string } = {
|
|
17
|
+
debug: "cyan",
|
|
18
|
+
info: "green",
|
|
19
|
+
notice: "grey",
|
|
20
|
+
warning: "blue",
|
|
21
|
+
error: "red",
|
|
22
|
+
critical: "magenta",
|
|
23
|
+
alert: "magenta",
|
|
24
|
+
emergency: "magenta"
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export interface Writer {
|
|
28
|
+
write?(dateTime: string, level: Level, id: string, msg: string): void;
|
|
29
|
+
rawWrite?(dateTime: string, level: Level, id: string, msg: string | object): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class ConsoleWriter implements Writer {
|
|
33
|
+
write(dateTime: string, level: Level, id: string, msg: string) {
|
|
34
|
+
if (isNode) {
|
|
35
|
+
console.log(`[${dateTime}] ${Level[level].toUpperCase()} ${id}: ${msg}`);
|
|
36
|
+
} else {
|
|
37
|
+
console.log(`[${dateTime}] %c${Level[level].toUpperCase()}%c ${id}: ${msg}`, `color:${colours[Level[level]]}`, "");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class Logging {
|
|
43
|
+
private static _instance: Logging;
|
|
44
|
+
private _levelStack = new Stack<Level>();
|
|
45
|
+
private _level = Level.info;
|
|
46
|
+
private _filter: string = "";
|
|
47
|
+
private _writer: Writer = new ConsoleWriter();
|
|
48
|
+
|
|
49
|
+
public static Instance() {
|
|
50
|
+
return this._instance || (this._instance = new this());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private constructor() {
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private stringify(obj: object): string {
|
|
57
|
+
const cache: any[] = [];
|
|
58
|
+
return JSON.stringify(obj, function (_key, value) {
|
|
59
|
+
if (typeof value === "object" && value !== null) {
|
|
60
|
+
if (cache.indexOf(value) !== -1) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
cache.push(value);
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}, 2);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
writer(): Writer;
|
|
70
|
+
writer(_: Writer): Logging;
|
|
71
|
+
writer(_?: Writer): Writer | Logging {
|
|
72
|
+
if (_ === void 0) return this._writer;
|
|
73
|
+
this._writer = _;
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
log(level: Level, id: string, msg: string | object) {
|
|
78
|
+
if (level < this._level) return;
|
|
79
|
+
if (this._filter && this._filter !== id) return;
|
|
80
|
+
|
|
81
|
+
const dateTime = new Date().toISOString();
|
|
82
|
+
|
|
83
|
+
if (this._writer.rawWrite) {
|
|
84
|
+
this._writer.rawWrite(dateTime, level, id, msg);
|
|
85
|
+
} else {
|
|
86
|
+
if (typeof msg !== "string") {
|
|
87
|
+
msg = this.stringify(msg);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (this._writer.write) {
|
|
91
|
+
this._writer.write(dateTime, level, id, msg);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
debug(id: string, msg: string | object) {
|
|
97
|
+
this.log(Level.debug, id, msg);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
info(id: string, msg: string | object) {
|
|
101
|
+
this.log(Level.info, id, msg);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
notice(id: string, msg: string | object) {
|
|
105
|
+
this.log(Level.notice, id, msg);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
warning(id: string, msg: string | object) {
|
|
109
|
+
this.log(Level.warning, id, msg);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
error(id: string, msg: string | object) {
|
|
113
|
+
this.log(Level.error, id, msg);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
critical(id: string, msg: string | object) {
|
|
117
|
+
this.log(Level.critical, id, msg);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
alert(id: string, msg: string | object) {
|
|
121
|
+
this.log(Level.alert, id, msg);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
emergency(id: string, msg: string | object) {
|
|
125
|
+
this.log(Level.emergency, id, msg);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
level(): Level;
|
|
129
|
+
level(_: Level): this;
|
|
130
|
+
level(_?: Level): Level | this {
|
|
131
|
+
if (_ === void 0) return this._level;
|
|
132
|
+
this._level = _;
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
pushLevel(_: Level): this {
|
|
137
|
+
this._levelStack.push(this._level);
|
|
138
|
+
this._level = _;
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
popLevel(): this {
|
|
143
|
+
this._level = this._levelStack.pop()!;
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
filter(): string;
|
|
148
|
+
filter(_: string): this;
|
|
149
|
+
filter(_?: string): string | this {
|
|
150
|
+
if (_ === void 0) return this._filter;
|
|
151
|
+
this._filter = _;
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export const logger = Logging.Instance();
|
|
156
|
+
|
|
157
|
+
export class ScopedLogging {
|
|
158
|
+
protected _scopeID: string;
|
|
159
|
+
|
|
160
|
+
constructor(scopeID: string) {
|
|
161
|
+
this._scopeID = scopeID;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
debug(msg: string | object) {
|
|
165
|
+
logger.debug(this._scopeID, msg);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
info(msg: string | object) {
|
|
169
|
+
logger.info(this._scopeID, msg);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
notice(msg: string | object) {
|
|
173
|
+
logger.notice(this._scopeID, msg);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
warning(msg: string | object) {
|
|
177
|
+
logger.warning(this._scopeID, msg);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
error(msg: string | object) {
|
|
181
|
+
logger.error(this._scopeID, msg);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
critical(msg: string | object) {
|
|
185
|
+
logger.critical(this._scopeID, msg);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
alert(msg: string | object) {
|
|
189
|
+
logger.alert(this._scopeID, msg);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
emergency(msg: string | object) {
|
|
193
|
+
logger.emergency(this._scopeID, msg);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
pushLevel(_: Level): this {
|
|
197
|
+
logger.pushLevel(_);
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
popLevel(): this {
|
|
202
|
+
logger.popLevel();
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function scopedLogger(scopeID: string, filter: boolean = false) {
|
|
208
|
+
if (filter) {
|
|
209
|
+
logger.filter(scopeID);
|
|
210
|
+
}
|
|
211
|
+
return new ScopedLogging(scopeID);
|
|
212
|
+
}
|
package/src/math.ts
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* degreesToRadians - converts degrees to radians
|
|
3
|
-
* Usage: degreesToRadians(1080);
|
|
4
|
-
*
|
|
5
|
-
* @param degrees
|
|
6
|
-
* @returns Number radians
|
|
7
|
-
*/
|
|
8
|
-
export function degreesToRadians(degrees: number): number {
|
|
9
|
-
return degrees * (Math.PI / 180);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* radiansToDegrees - converts radians to degrees
|
|
14
|
-
* Usage: radiansToDegrees(7);
|
|
15
|
-
*
|
|
16
|
-
* @param radians
|
|
17
|
-
* @returns Number degreees
|
|
18
|
-
*/
|
|
19
|
-
export function radiansToDegrees(radians: number): number {
|
|
20
|
-
return radians * (180 / Math.PI);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* polarToCartesian - converts (r, theta) to {x, y}
|
|
25
|
-
* Usage: polarToCartesian(5, Math.PI);
|
|
26
|
-
*
|
|
27
|
-
* @param r radius
|
|
28
|
-
* @param theta angle in radians
|
|
29
|
-
* @returns { x: number, y: number }
|
|
30
|
-
*/
|
|
31
|
-
export function polarToCartesian(r: number, theta: number): { x:number, y:number } {
|
|
32
|
-
return {
|
|
33
|
-
x: r * Math.cos(theta),
|
|
34
|
-
y: r * Math.sin(theta)
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* cartesianToPolar - converts (x, y) to {r, theta}
|
|
40
|
-
* Usage: cartesianToPolar(100, 200);
|
|
41
|
-
*
|
|
42
|
-
* @param x
|
|
43
|
-
* @param y
|
|
44
|
-
* @returns { r: number, theta: number }
|
|
45
|
-
*/
|
|
46
|
-
export function cartesianToPolar(x: number, y: number): { r: number, theta: number } {
|
|
47
|
-
return {
|
|
48
|
-
r: Math.sqrt(x * x + y * y),
|
|
49
|
-
theta: Math.atan2(y, x)
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* normalizeRadians - normalizes a radian value to within the provided range
|
|
55
|
-
* Usage: normalizeRadians(7);
|
|
56
|
-
*
|
|
57
|
-
* @param radians value to be normalized
|
|
58
|
-
* @param min lower limit
|
|
59
|
-
* @param max upper limit
|
|
60
|
-
* @returns Number normalized to within the provided range
|
|
61
|
-
*/
|
|
62
|
-
export function normalizeRadians(radians: number, min: number = -Math.PI, max: number = Math.PI): number {
|
|
63
|
-
return normalize(radians, min, max);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* normalizeDegrees - normalizes a degree value to within the provided range
|
|
68
|
-
* Usage: normalizeDegrees(1080);
|
|
69
|
-
*
|
|
70
|
-
* @param degrees value to be normalized
|
|
71
|
-
* @param min lower limit
|
|
72
|
-
* @param max upper limit
|
|
73
|
-
* @returns Number normalized to within the provided range
|
|
74
|
-
*/
|
|
75
|
-
export function normalizeDegrees(degrees: number, min: number = -180, max: number = 180): number {
|
|
76
|
-
return normalize(degrees, min, max);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* normalize - normalizes a value to within the provided range
|
|
81
|
-
* Usage: normalize(1000, 0, 365);
|
|
82
|
-
*
|
|
83
|
-
* @param value value to be normalized
|
|
84
|
-
* @param min lower limit
|
|
85
|
-
* @param max upper limit
|
|
86
|
-
* @returns Number normalized to within the provided range
|
|
87
|
-
*/
|
|
88
|
-
export function normalize(value: number, min: number, max: number): number {
|
|
89
|
-
const spread = max - min;
|
|
90
|
-
const offsetValue = value - min;
|
|
91
|
-
return (offsetValue - (Math.floor(offsetValue / spread) * spread)) + min;
|
|
92
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* degreesToRadians - converts degrees to radians
|
|
3
|
+
* Usage: degreesToRadians(1080);
|
|
4
|
+
*
|
|
5
|
+
* @param degrees
|
|
6
|
+
* @returns Number radians
|
|
7
|
+
*/
|
|
8
|
+
export function degreesToRadians(degrees: number): number {
|
|
9
|
+
return degrees * (Math.PI / 180);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* radiansToDegrees - converts radians to degrees
|
|
14
|
+
* Usage: radiansToDegrees(7);
|
|
15
|
+
*
|
|
16
|
+
* @param radians
|
|
17
|
+
* @returns Number degreees
|
|
18
|
+
*/
|
|
19
|
+
export function radiansToDegrees(radians: number): number {
|
|
20
|
+
return radians * (180 / Math.PI);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* polarToCartesian - converts (r, theta) to {x, y}
|
|
25
|
+
* Usage: polarToCartesian(5, Math.PI);
|
|
26
|
+
*
|
|
27
|
+
* @param r radius
|
|
28
|
+
* @param theta angle in radians
|
|
29
|
+
* @returns { x: number, y: number }
|
|
30
|
+
*/
|
|
31
|
+
export function polarToCartesian(r: number, theta: number): { x:number, y:number } {
|
|
32
|
+
return {
|
|
33
|
+
x: r * Math.cos(theta),
|
|
34
|
+
y: r * Math.sin(theta)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* cartesianToPolar - converts (x, y) to {r, theta}
|
|
40
|
+
* Usage: cartesianToPolar(100, 200);
|
|
41
|
+
*
|
|
42
|
+
* @param x
|
|
43
|
+
* @param y
|
|
44
|
+
* @returns { r: number, theta: number }
|
|
45
|
+
*/
|
|
46
|
+
export function cartesianToPolar(x: number, y: number): { r: number, theta: number } {
|
|
47
|
+
return {
|
|
48
|
+
r: Math.sqrt(x * x + y * y),
|
|
49
|
+
theta: Math.atan2(y, x)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* normalizeRadians - normalizes a radian value to within the provided range
|
|
55
|
+
* Usage: normalizeRadians(7);
|
|
56
|
+
*
|
|
57
|
+
* @param radians value to be normalized
|
|
58
|
+
* @param min lower limit
|
|
59
|
+
* @param max upper limit
|
|
60
|
+
* @returns Number normalized to within the provided range
|
|
61
|
+
*/
|
|
62
|
+
export function normalizeRadians(radians: number, min: number = -Math.PI, max: number = Math.PI): number {
|
|
63
|
+
return normalize(radians, min, max);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* normalizeDegrees - normalizes a degree value to within the provided range
|
|
68
|
+
* Usage: normalizeDegrees(1080);
|
|
69
|
+
*
|
|
70
|
+
* @param degrees value to be normalized
|
|
71
|
+
* @param min lower limit
|
|
72
|
+
* @param max upper limit
|
|
73
|
+
* @returns Number normalized to within the provided range
|
|
74
|
+
*/
|
|
75
|
+
export function normalizeDegrees(degrees: number, min: number = -180, max: number = 180): number {
|
|
76
|
+
return normalize(degrees, min, max);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* normalize - normalizes a value to within the provided range
|
|
81
|
+
* Usage: normalize(1000, 0, 365);
|
|
82
|
+
*
|
|
83
|
+
* @param value value to be normalized
|
|
84
|
+
* @param min lower limit
|
|
85
|
+
* @param max upper limit
|
|
86
|
+
* @returns Number normalized to within the provided range
|
|
87
|
+
*/
|
|
88
|
+
export function normalize(value: number, min: number, max: number): number {
|
|
89
|
+
const spread = max - min;
|
|
90
|
+
const offsetValue = value - min;
|
|
91
|
+
return (offsetValue - (Math.floor(offsetValue / spread) * spread)) + min;
|
|
92
|
+
}
|