@oliversalzburg/js-utils 0.0.24-dev.11 → 0.0.24-dev.13
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/errors/NotImplementedError.d.ts +1 -1
- package/errors/NotImplementedError.js +1 -1
- package/errors/NotImplementedError.js.map +1 -1
- package/graphics/canvas-sandbox.d.ts +10 -8
- package/graphics/canvas-sandbox.js +4 -3
- package/graphics/canvas-sandbox.js.map +1 -1
- package/graphics/canvas.d.ts +28 -143
- package/graphics/canvas.js +12 -277
- package/graphics/canvas.js.map +1 -1
- package/graphics/canvas2d.d.ts +168 -0
- package/graphics/canvas2d.js +324 -0
- package/graphics/canvas2d.js.map +1 -0
- package/graphics/canvas3d.d.ts +56 -0
- package/graphics/canvas3d.js +75 -0
- package/graphics/canvas3d.js.map +1 -0
- package/graphics/index.d.ts +3 -0
- package/graphics/index.js +3 -0
- package/graphics/index.js.map +1 -1
- package/graphics/render-loop.d.ts +1 -1
- package/graphics/render-loop.js +5 -44
- package/graphics/render-loop.js.map +1 -1
- package/graphics/shader.d.ts +70 -0
- package/graphics/shader.js +90 -0
- package/graphics/shader.js.map +1 -0
- package/math/vector3.d.ts +2 -2
- package/nil.d.ts +35 -0
- package/nil.js +63 -0
- package/nil.js.map +1 -1
- package/package.json +1 -1
package/graphics/index.d.ts
CHANGED
package/graphics/index.js
CHANGED
package/graphics/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/graphics/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/graphics/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC"}
|
|
@@ -69,7 +69,7 @@ export declare class RenderLoop {
|
|
|
69
69
|
/**
|
|
70
70
|
* Constructs a new {@linkcode RenderLoop}.
|
|
71
71
|
* @param renderLoop - The function to call when a new frame should be drawn.
|
|
72
|
-
* @param canvas - The
|
|
72
|
+
* @param canvas - The canvas we're rendering to.
|
|
73
73
|
* @param options - The configuration for thie {@linkcode RenderLoop}.
|
|
74
74
|
*/
|
|
75
75
|
constructor(renderLoop: RenderLoopCallback, canvas: Canvas, options?: Partial<RenderLoopOptions>);
|
package/graphics/render-loop.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { blend } from "./core.js";
|
|
2
1
|
/**
|
|
3
2
|
* The amount of milliseconds your can spend in a frame, if you want to
|
|
4
3
|
* reach a frame rate of 60 FPS.
|
|
@@ -59,7 +58,7 @@ export class RenderLoop {
|
|
|
59
58
|
/**
|
|
60
59
|
* Constructs a new {@linkcode RenderLoop}.
|
|
61
60
|
* @param renderLoop - The function to call when a new frame should be drawn.
|
|
62
|
-
* @param canvas - The
|
|
61
|
+
* @param canvas - The canvas we're rendering to.
|
|
63
62
|
* @param options - The configuration for thie {@linkcode RenderLoop}.
|
|
64
63
|
*/
|
|
65
64
|
constructor(renderLoop, canvas, options = {}) {
|
|
@@ -103,6 +102,9 @@ export class RenderLoop {
|
|
|
103
102
|
}
|
|
104
103
|
const timestamp = new Date().getTime();
|
|
105
104
|
const timeDelta = timestamp - this.previousTimestampDraw;
|
|
105
|
+
if (this.options.onRefresh === "clear") {
|
|
106
|
+
this.canvas.clearWith(0);
|
|
107
|
+
}
|
|
106
108
|
this.renderLoop(timeDelta, timestamp);
|
|
107
109
|
const frameTime = new Date().getTime() - timestamp;
|
|
108
110
|
if (this.options.drawFps) {
|
|
@@ -110,49 +112,8 @@ export class RenderLoop {
|
|
|
110
112
|
}
|
|
111
113
|
this.previousTimestampDraw = timestamp;
|
|
112
114
|
this.canvas.update();
|
|
113
|
-
if (this.options.onRefresh === "clear") {
|
|
114
|
-
this.canvas.clearWith(0);
|
|
115
|
-
}
|
|
116
115
|
if (this.options.drawFps) {
|
|
117
|
-
|
|
118
|
-
if (max < frameTime) {
|
|
119
|
-
max = frameTime;
|
|
120
|
-
}
|
|
121
|
-
return max;
|
|
122
|
-
}, 0);
|
|
123
|
-
const sumFrameTimes = this.#frameTimes.reduce((sum, frameTime) => sum + frameTime, 0);
|
|
124
|
-
const fpsAll = Math.round(this.#frameTimes.length / (sumFrameTimes / 1000)).toString();
|
|
125
|
-
const fpsFrame = 1000 / frameTime;
|
|
126
|
-
const fpsDelta = 1000 / timeDelta;
|
|
127
|
-
this.#frameTimes.splice(0, this.#frameTimes.length - this.canvas.width);
|
|
128
|
-
const frameTimes = this.#frameTimes.toReversed();
|
|
129
|
-
let currentX = this.canvas.width;
|
|
130
|
-
for (const frameTime of frameTimes) {
|
|
131
|
-
let fillColor = 0x00ff00ff;
|
|
132
|
-
let width = 1;
|
|
133
|
-
if (MS_PER_FRAME_60FPS < frameTime) {
|
|
134
|
-
const quality = frameTime / maxFrameTime;
|
|
135
|
-
fillColor = blend(0x00ff00ff, 0xff0000ff, Math.trunc(quality * 255)) >>> 0;
|
|
136
|
-
width = Math.round(frameTime / MS_PER_FRAME_60FPS);
|
|
137
|
-
}
|
|
138
|
-
this.canvas.context.fillStyle = `#${fillColor.toString(16).padStart(8, "0")}`;
|
|
139
|
-
this.canvas.context.globalAlpha = 255;
|
|
140
|
-
this.canvas.context.fillRect(currentX - width, this.canvas.height, width, (frameTime / Math.max(100, maxFrameTime)) * -100);
|
|
141
|
-
currentX -= width;
|
|
142
|
-
}
|
|
143
|
-
this.canvas.context.beginPath();
|
|
144
|
-
this.canvas.context.strokeStyle = "#fff";
|
|
145
|
-
this.canvas.context.moveTo(0, this.canvas.height - MS_PER_FRAME_60FPS);
|
|
146
|
-
this.canvas.context.lineTo(this.canvas.width, this.canvas.height - MS_PER_FRAME_60FPS);
|
|
147
|
-
this.canvas.context.stroke();
|
|
148
|
-
const fpsString = `${Math.round(fpsFrame)}f ${fpsAll}∑ ${Math.round(fpsDelta)}δ`;
|
|
149
|
-
this.canvas.context.font = "13px monospace";
|
|
150
|
-
this.canvas.context.strokeStyle = "rgba( 255, 255, 255, 1)";
|
|
151
|
-
this.canvas.context.textAlign = "right";
|
|
152
|
-
this.canvas.context.textRendering = "geometricPrecision";
|
|
153
|
-
this.canvas.context.strokeText(fpsString, this.canvas.width - 3, this.canvas.height - 3);
|
|
154
|
-
this.canvas.context.fillStyle = "#000000";
|
|
155
|
-
this.canvas.context.fillText(fpsString, this.canvas.width - 3, this.canvas.height - 3);
|
|
116
|
+
this.canvas.renderFpsInfo(this.#frameTimes, frameTime, timeDelta);
|
|
156
117
|
}
|
|
157
118
|
this.canvas.render();
|
|
158
119
|
this.unblock();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"render-loop.js","sourceRoot":"","sources":["../../source/graphics/render-loop.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"render-loop.js","sourceRoot":"","sources":["../../source/graphics/render-loop.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,eAAe,CAAC,IAAI,GAAG,EAAE,CAAC;AAyB5D;;;;GAIG;AACH,MAAM,OAAO,UAAU;IACrB;;OAEG;IACM,MAAM,CAAS;IAExB;;OAEG;IACM,UAAU,CAAqB;IAExC;;OAEG;IACM,OAAO,CAAuC;IAEvD;;OAEG;IACK,qBAAqB,CAAS;IAEtC;;OAEG;IACK,wBAAwB,CAAS;IAEzC;;;OAGG;IACM,eAAe,CAAa;IAErC;;OAEG;IACK,cAAc,GAAkB,IAAI,CAAC;IAE7C;;;OAGG;IACK,WAAW,GAAyC,IAAI,CAAC;IAEjE;;OAEG;IACH,aAAa,GAAG,CAA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,GAAG,SAAS,GAAG,IAAI,CAAC,qBAAqB,CAAC;QAEzD,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,KAAK,OAAO,EAAE,CAAC;YACvC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QAC3B,CAAC;QAED,IAAI,CAAC,UAAU,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;QAEtC,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,SAAS,CAAC;QAEnD,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACnC,CAAC;QAED,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;QAEvC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QAErB,IAAI,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACzB,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,WAAW,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;QACpE,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QAErB,IAAI,CAAC,OAAO,EAAE,CAAC;QAEf,IAAI,CAAC,wBAAwB,GAAG,SAAS,CAAC;QAC1C,EAAE,IAAI,CAAC,aAAa,CAAC;IACvB,CAAC;CACF"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/// <reference types="@types/web" />
|
|
2
|
+
/**
|
|
3
|
+
* A template literal tag, that allows syntax highliters to treat the
|
|
4
|
+
* wrapped code as GLSL.
|
|
5
|
+
* @param input - Should only be the source of a fragment shader.
|
|
6
|
+
* @returns The fragment shader as-is.
|
|
7
|
+
* @group Graphics
|
|
8
|
+
* @group GLSL
|
|
9
|
+
* @group WebGL
|
|
10
|
+
*/
|
|
11
|
+
export declare const frag: (input: TemplateStringsArray) => string;
|
|
12
|
+
/**
|
|
13
|
+
* A template literal tag, that allows syntax highliters to treat the
|
|
14
|
+
* wrapped code as GLSL.
|
|
15
|
+
* @param input - Should only be the source of a vertex shader.
|
|
16
|
+
* @returns The vertex shader as-is.
|
|
17
|
+
* @group Graphics
|
|
18
|
+
* @group GLSL
|
|
19
|
+
* @group WebGL
|
|
20
|
+
*/
|
|
21
|
+
export declare const vert: (input: TemplateStringsArray) => string;
|
|
22
|
+
/**
|
|
23
|
+
* Construction options for a {@linkcode !WebGLShader}.
|
|
24
|
+
* @group Graphics
|
|
25
|
+
* @group GLSL
|
|
26
|
+
* @group WebGL
|
|
27
|
+
*/
|
|
28
|
+
export interface ShaderOptions {
|
|
29
|
+
/**
|
|
30
|
+
* The GLSL source code of the shader.
|
|
31
|
+
*/
|
|
32
|
+
source: string;
|
|
33
|
+
/**
|
|
34
|
+
* The type of the shader.
|
|
35
|
+
*/
|
|
36
|
+
type: WebGL2RenderingContext["FRAGMENT_SHADER"] | WebGL2RenderingContext["VERTEX_SHADER"];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Constructs a new shader.
|
|
40
|
+
* @param gl - The rendering context in which to create the shader.
|
|
41
|
+
* @param options - The construction options for the shader.
|
|
42
|
+
* @returns The shader.
|
|
43
|
+
* @group Graphics
|
|
44
|
+
* @group GLSL
|
|
45
|
+
* @group WebGL
|
|
46
|
+
*/
|
|
47
|
+
export declare const createShader: (gl: WebGL2RenderingContext, options: ShaderOptions) => WebGLShader;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new WebGL program.
|
|
50
|
+
* @param gl - The rendering context in which to create the program.
|
|
51
|
+
* @param shaders - The shaders we want to create.
|
|
52
|
+
* @param transformFeedbackVaryings - Which values to record in transform
|
|
53
|
+
* feedback buffer? See {@linkcode !WebGLTransformFeedback}.
|
|
54
|
+
* @returns The program.
|
|
55
|
+
* @group Graphics
|
|
56
|
+
* @group GLSL
|
|
57
|
+
* @group WebGL
|
|
58
|
+
*/
|
|
59
|
+
export declare const createGLProgram: (gl: WebGL2RenderingContext, shaders: Array<ShaderOptions>, transformFeedbackVaryings: Array<string> | null) => WebGLProgram;
|
|
60
|
+
/**
|
|
61
|
+
* Create a buffer with random data in 2 channels (red/green).
|
|
62
|
+
* @param width - Width of the buffer.
|
|
63
|
+
* @param height - Height of the buffer.
|
|
64
|
+
* @returns The buffer.
|
|
65
|
+
* @group Graphics
|
|
66
|
+
* @group GLSL
|
|
67
|
+
* @group WebGL
|
|
68
|
+
* @group Random
|
|
69
|
+
*/
|
|
70
|
+
export declare const randomRGData: (width: number, height: number) => Uint8Array;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { InvalidOperationError } from "../errors/InvalidOperationError.js";
|
|
2
|
+
import { mustExist } from "../nil.js";
|
|
3
|
+
/**
|
|
4
|
+
* A template literal tag, that allows syntax highliters to treat the
|
|
5
|
+
* wrapped code as GLSL.
|
|
6
|
+
* @param input - Should only be the source of a fragment shader.
|
|
7
|
+
* @returns The fragment shader as-is.
|
|
8
|
+
* @group Graphics
|
|
9
|
+
* @group GLSL
|
|
10
|
+
* @group WebGL
|
|
11
|
+
*/
|
|
12
|
+
export const frag = (input) => input.join();
|
|
13
|
+
/**
|
|
14
|
+
* A template literal tag, that allows syntax highliters to treat the
|
|
15
|
+
* wrapped code as GLSL.
|
|
16
|
+
* @param input - Should only be the source of a vertex shader.
|
|
17
|
+
* @returns The vertex shader as-is.
|
|
18
|
+
* @group Graphics
|
|
19
|
+
* @group GLSL
|
|
20
|
+
* @group WebGL
|
|
21
|
+
*/
|
|
22
|
+
export const vert = (input) => input.join();
|
|
23
|
+
/**
|
|
24
|
+
* Constructs a new shader.
|
|
25
|
+
* @param gl - The rendering context in which to create the shader.
|
|
26
|
+
* @param options - The construction options for the shader.
|
|
27
|
+
* @returns The shader.
|
|
28
|
+
* @group Graphics
|
|
29
|
+
* @group GLSL
|
|
30
|
+
* @group WebGL
|
|
31
|
+
*/
|
|
32
|
+
export const createShader = (gl, options) => {
|
|
33
|
+
const shader = mustExist(gl.createShader(options.type));
|
|
34
|
+
const shader_source = options.source.replace(/^\s*/, "");
|
|
35
|
+
gl.shaderSource(shader, shader_source);
|
|
36
|
+
gl.compileShader(shader);
|
|
37
|
+
const compileStatus = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
|
|
38
|
+
if (!compileStatus) {
|
|
39
|
+
const errorMessage = gl.getShaderInfoLog(shader);
|
|
40
|
+
throw new InvalidOperationError(`Could not compile shader\n${errorMessage}`);
|
|
41
|
+
}
|
|
42
|
+
return shader;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new WebGL program.
|
|
46
|
+
* @param gl - The rendering context in which to create the program.
|
|
47
|
+
* @param shaders - The shaders we want to create.
|
|
48
|
+
* @param transformFeedbackVaryings - Which values to record in transform
|
|
49
|
+
* feedback buffer? See {@linkcode !WebGLTransformFeedback}.
|
|
50
|
+
* @returns The program.
|
|
51
|
+
* @group Graphics
|
|
52
|
+
* @group GLSL
|
|
53
|
+
* @group WebGL
|
|
54
|
+
*/
|
|
55
|
+
export const createGLProgram = (gl, shaders, transformFeedbackVaryings) => {
|
|
56
|
+
const program = mustExist(gl.createProgram());
|
|
57
|
+
for (const shader_info of shaders) {
|
|
58
|
+
const shader = createShader(gl, shader_info);
|
|
59
|
+
gl.attachShader(program, shader);
|
|
60
|
+
}
|
|
61
|
+
if (transformFeedbackVaryings !== null) {
|
|
62
|
+
gl.transformFeedbackVaryings(program, transformFeedbackVaryings, gl.INTERLEAVED_ATTRIBS);
|
|
63
|
+
}
|
|
64
|
+
gl.linkProgram(program);
|
|
65
|
+
const link_status = gl.getProgramParameter(program, gl.LINK_STATUS);
|
|
66
|
+
if (!link_status) {
|
|
67
|
+
const error_message = gl.getProgramInfoLog(program);
|
|
68
|
+
throw new InvalidOperationError(`Could not link program.\n${error_message}`);
|
|
69
|
+
}
|
|
70
|
+
return program;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Create a buffer with random data in 2 channels (red/green).
|
|
74
|
+
* @param width - Width of the buffer.
|
|
75
|
+
* @param height - Height of the buffer.
|
|
76
|
+
* @returns The buffer.
|
|
77
|
+
* @group Graphics
|
|
78
|
+
* @group GLSL
|
|
79
|
+
* @group WebGL
|
|
80
|
+
* @group Random
|
|
81
|
+
*/
|
|
82
|
+
export const randomRGData = (width, height) => {
|
|
83
|
+
const data = new Array();
|
|
84
|
+
for (let i = 0; i < width * height; ++i) {
|
|
85
|
+
data.push(Math.random() * 255.0);
|
|
86
|
+
data.push(Math.random() * 255.0);
|
|
87
|
+
}
|
|
88
|
+
return new Uint8Array(data);
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=shader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shader.js","sourceRoot":"","sources":["../../source/graphics/shader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,KAA2B,EAAU,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;AAE1E;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,KAA2B,EAAU,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;AAoB1E;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,EAA0B,EAAE,OAAsB,EAAe,EAAE;IAC9F,MAAM,MAAM,GAAgB,SAAS,CAAC,EAAE,CAAC,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IACrE,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACzD,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACvC,EAAE,C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,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAa,EAAE,MAAc,EAAE,EAAE;IAC5D,MAAM,IAAI,GAAG,IAAI,KAAK,EAAU,CAAC;IACjC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,GAAG,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC;QACxC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;IACnC,CAAC;IACD,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;AAC9B,CAAC,CAAC"}
|
package/math/vector3.d.ts
CHANGED
package/nil.d.ts
CHANGED
|
@@ -53,6 +53,16 @@ export declare class UnexpectedNilError extends Error {
|
|
|
53
53
|
* @group Nullability
|
|
54
54
|
*/
|
|
55
55
|
export declare function mustExist<TSubject>(subject: Maybe<TSubject>, errorMessage?: string): TSubject;
|
|
56
|
+
/**
|
|
57
|
+
* Ensure that the passed subject is not nil; throw otherwise.
|
|
58
|
+
* @param subject - A subject that is possible nil.
|
|
59
|
+
* @param errorMessage - An optional error message to throw when the subject is nil.
|
|
60
|
+
* @typeParam TSubject - The type of the subject.
|
|
61
|
+
* @returns The subject, if it isn't nil.
|
|
62
|
+
* @throws {@linkcode UnexpectedNilError} When the subject is nil.
|
|
63
|
+
* @group Nullability
|
|
64
|
+
*/
|
|
65
|
+
export declare function mustExistAll<TSubject>(subject: Array<Maybe<TSubject>>, errorMessage?: string): Array<TSubject>;
|
|
56
66
|
/**
|
|
57
67
|
* Ensure that the passed subject is not nil; throw otherwise.
|
|
58
68
|
* @param subject - A subject that is possibly nil.
|
|
@@ -61,3 +71,28 @@ export declare function mustExist<TSubject>(subject: Maybe<TSubject>, errorMessa
|
|
|
61
71
|
* @group Nullability
|
|
62
72
|
*/
|
|
63
73
|
export declare function assertExists<TSubject>(subject: Maybe<TSubject>): asserts subject is TSubject;
|
|
74
|
+
/**
|
|
75
|
+
* Convert a nilable into a real value, if it is nil.
|
|
76
|
+
* @param nilable - The subject to convert to an optional.
|
|
77
|
+
* @param to - The value to coalese to.
|
|
78
|
+
* @returns The input value, if it wasn't nil, or the value to coalesce to.
|
|
79
|
+
* @group Nullability
|
|
80
|
+
*/
|
|
81
|
+
export declare function coalesce<T>(nilable: Maybe<T>, to: T): T;
|
|
82
|
+
/**
|
|
83
|
+
* Drop all nil values from an array, or replaces them with another value.
|
|
84
|
+
* @param nilables - The subject to convert.
|
|
85
|
+
* @param to - The value to coalese to.
|
|
86
|
+
* @returns An array with where all values are not nil.
|
|
87
|
+
* @group Nullability
|
|
88
|
+
* @group Array
|
|
89
|
+
*/
|
|
90
|
+
export declare function coalesceArray<T>(nilables: Array<Maybe<T>>, to?: Maybe<T>): Array<T>;
|
|
91
|
+
/**
|
|
92
|
+
* Convert a nilable into an optional argument.
|
|
93
|
+
* This means `null` is normalized to `undefined`.
|
|
94
|
+
* @param nilable - The subject to convert to an optional.
|
|
95
|
+
* @returns The value, normalized to `undefined`, if it was nil.
|
|
96
|
+
* @group Nullability
|
|
97
|
+
*/
|
|
98
|
+
export declare function toOptional<T>(nilable: Maybe<T>): T | undefined;
|
package/nil.js
CHANGED
|
@@ -51,6 +51,23 @@ export function mustExist(subject, errorMessage) {
|
|
|
51
51
|
}
|
|
52
52
|
return subject;
|
|
53
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Ensure that the passed subject is not nil; throw otherwise.
|
|
56
|
+
* @param subject - A subject that is possible nil.
|
|
57
|
+
* @param errorMessage - An optional error message to throw when the subject is nil.
|
|
58
|
+
* @typeParam TSubject - The type of the subject.
|
|
59
|
+
* @returns The subject, if it isn't nil.
|
|
60
|
+
* @throws {@linkcode UnexpectedNilError} When the subject is nil.
|
|
61
|
+
* @group Nullability
|
|
62
|
+
*/
|
|
63
|
+
export function mustExistAll(subject, errorMessage) {
|
|
64
|
+
for (const element of subject) {
|
|
65
|
+
if (isNil(element)) {
|
|
66
|
+
throw new UnexpectedNilError(errorMessage);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return subject;
|
|
70
|
+
}
|
|
54
71
|
/**
|
|
55
72
|
* Ensure that the passed subject is not nil; throw otherwise.
|
|
56
73
|
* @param subject - A subject that is possibly nil.
|
|
@@ -63,4 +80,50 @@ export function assertExists(subject) {
|
|
|
63
80
|
throw new UnexpectedNilError();
|
|
64
81
|
}
|
|
65
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Convert a nilable into a real value, if it is nil.
|
|
85
|
+
* @param nilable - The subject to convert to an optional.
|
|
86
|
+
* @param to - The value to coalese to.
|
|
87
|
+
* @returns The input value, if it wasn't nil, or the value to coalesce to.
|
|
88
|
+
* @group Nullability
|
|
89
|
+
*/
|
|
90
|
+
export function coalesce(nilable, to) {
|
|
91
|
+
if (isNil(nilable)) {
|
|
92
|
+
return to;
|
|
93
|
+
}
|
|
94
|
+
return nilable;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Drop all nil values from an array, or replaces them with another value.
|
|
98
|
+
* @param nilables - The subject to convert.
|
|
99
|
+
* @param to - The value to coalese to.
|
|
100
|
+
* @returns An array with where all values are not nil.
|
|
101
|
+
* @group Nullability
|
|
102
|
+
* @group Array
|
|
103
|
+
*/
|
|
104
|
+
export function coalesceArray(nilables, to) {
|
|
105
|
+
const result = new Array();
|
|
106
|
+
for (const nilable of nilables) {
|
|
107
|
+
if (!isNil(nilable)) {
|
|
108
|
+
result.push(nilable);
|
|
109
|
+
}
|
|
110
|
+
else if (!isNil(to)) {
|
|
111
|
+
result.push(to);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Convert a nilable into an optional argument.
|
|
118
|
+
* This means `null` is normalized to `undefined`.
|
|
119
|
+
* @param nilable - The subject to convert to an optional.
|
|
120
|
+
* @returns The value, normalized to `undefined`, if it was nil.
|
|
121
|
+
* @group Nullability
|
|
122
|
+
*/
|
|
123
|
+
export function toOptional(nilable) {
|
|
124
|
+
if (isNil(nilable)) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
return nilable;
|
|
128
|
+
}
|
|
66
129
|
//# sourceMappingURL=nil.js.map
|
package/nil.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nil.js","sourceRoot":"","sources":["../source/nil.ts"],"names":[],"mappings":"AAgBA;;;;;;;GAOG;AACH,MAAM,UAAU,KAAK,CAAW,OAAwB;IACtD,OAAO,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,SAAS,CAAC;AACnD,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,EAAE,CAChB,OAAwB,EACxB,SAAkC;IAElC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,OAAO,YAAY,SAAS,CAAC;AACzD,CAAC;AAED;;;;GAIG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAC3C;;;OAGG;IACH,YAAY,OAAO,GAAG,sBAAsB;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CAAW,OAAwB,EAAE,YAAqB;IACjF,IAAI,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;QACnB,MAAM,IAAI,kBAAkB,CAAC,YAAY,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAW,OAAwB;IAC7D,IAAI,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;QACnB,MAAM,IAAI,kBAAkB,EAAE,CAAC;IACjC,CAAC;AACH,CAAC"}
|
|
1
|
+
{"version":3,"file":"nil.js","sourceRoot":"","sources":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