Skip to content

Commit 99c36b9

Browse files
committed
More Util Typings
I'm considering whether I should do this with a fork of the library instead, and work on modernizing that instead of the original, which seems to be a few development iteration types back (ESM, TypeScript, browser-first). https://github.com/telerik/jszip-esm It seems like a lot of work for modernizing older projects like these are that they are for Node to start with, and there are a lot of polyfills set up to make it work for modern code, rather than the other way around. To be fair, that's the same issue I'm having with my own projects from a few years ago too, so it's just going to happen, and it's still just something we have to deal with. I guess it's good that the same language has been around long enough for it to grow so much to where that is a concern. People see that as a downside to the JS ecosystem, but it also really shows how much things have grown over the years. I wish not as many new TS projects were OOP, it's still the same language under the hood, so I find it interesting how the typings for things in that manner seems to have brought non-FP concepts over to JS's fairly functional neighborhood (at least for packages, it seems like most user-facing APIs use functions where possible, and maybe the underlying implementation may use a few classes).
1 parent c6eb5ab commit 99c36b9

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

lib/external.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,4 @@ if (typeof Promise !== "undefined") {
1313
/**
1414
* Let the user use/change some implementations.
1515
*/
16-
module.exports = {
17-
Promise: ES6Promise
18-
};
16+
module.exports.Promise = ES6Promise;

lib/nodejsUtils.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
"use strict";
22

3-
module.exports = {
43
/**
54
* True if this is running in Nodejs, will be undefined in a browser.
65
* In a browser, browserify won't include this file and the whole module
76
* will be resolved an empty object.
87
*/
9-
isNode : typeof Buffer !== "undefined",
8+
exports.isNode = typeof Buffer !== "undefined";
109
/**
1110
* Create a new nodejs Buffer from an existing content.
12-
* @param {Object} data the data to pass to the constructor.
13-
* @param {String} encoding the encoding to use.
11+
* @param {string} data the data to pass to the constructor.
12+
* @param {BufferEncoding} encoding the encoding to use.
1413
* @return {Buffer} a new Buffer.
1514
*/
16-
newBufferFrom: function(data, encoding) {
15+
exports.newBufferFrom = function(data, encoding) {
1716
if (Buffer.from && Buffer.from !== Uint8Array.from) {
1817
return Buffer.from(data, encoding);
1918
} else {
@@ -24,34 +23,37 @@ module.exports = {
2423
}
2524
return new Buffer(data, encoding);
2625
}
27-
},
26+
};
2827
/**
2928
* Create a new nodejs Buffer with the specified size.
30-
* @param {Integer} size the size of the buffer.
29+
* @param {number} size the size of the buffer.
3130
* @return {Buffer} a new Buffer.
3231
*/
33-
allocBuffer: function (size) {
32+
exports.allocBuffer = function (size) {
3433
if (Buffer.alloc) {
3534
return Buffer.alloc(size);
3635
} else {
3736
var buf = new Buffer(size);
3837
buf.fill(0);
3938
return buf;
4039
}
41-
},
40+
};
4241
/**
4342
* Find out if an object is a Buffer.
44-
* @param {Object} b the object to test.
45-
* @return {Boolean} true if the object is a Buffer, false otherwise.
43+
* @param {any} b the object to test.
44+
* @return {b is Buffer} true if the object is a Buffer, false otherwise.
4645
*/
47-
isBuffer : function(b){
46+
exports.isBuffer = function(b){
4847
return Buffer.isBuffer(b);
49-
},
48+
};
5049

51-
isStream : function (obj) {
50+
/**
51+
* @param {any} obj
52+
* @returns {obj is import("readable-stream").Readable}
53+
*/
54+
exports.isStream = function (obj) {
5255
return obj &&
5356
typeof obj.on === "function" &&
5457
typeof obj.pause === "function" &&
5558
typeof obj.resume === "function";
56-
}
57-
};
59+
};

lib/utils.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ require("setimmediate");
1212
* Convert a string that pass as a "binary string": it should represent a byte
1313
* array but may have > 255 char codes. Be sure to take only the first byte
1414
* and returns the byte array.
15-
* @param {String} str the string to transform.
16-
* @return {Array<number>|Uint8Array} the string in a binary format.
15+
* @param {string} str the string to transform.
16+
* @return {number[] | Uint8Array} the string in a binary format.
1717
*/
1818
function string2binary(str) {
1919
var result = null;
@@ -27,15 +27,15 @@ function string2binary(str) {
2727

2828
/**
2929
* Create a new blob with the given content and the given type.
30-
* @param {String|ArrayBuffer} part the content to put in the blob. DO NOT use
30+
* @param {string | ArrayBuffer} part the content to put in the blob. DO NOT use
3131
* an Uint8Array because the stock browser of android 4 won't accept it (it
3232
* will be silently converted to a string, "[object Uint8Array]").
3333
*
3434
* Use only ONE part to build the blob to avoid a memory leak in IE11 / Edge:
3535
* when a large amount of Array is used to create the Blob, the amount of
3636
* memory consumed is nearly 100 times the original data amount.
3737
*
38-
* @param {String} type the mime type of the blob.
38+
* @param {string} type the mime type of the blob.
3939
* @return {Blob} the created blob.
4040
*/
4141
exports.newBlob = function(part, type) {
@@ -50,7 +50,7 @@ exports.newBlob = function(part, type) {
5050
catch (e) {
5151

5252
try {
53-
// deprecated, browser only, old way
53+
// @ts-expect-error // deprecated, browser only, old way
5454
var Builder = self.BlobBuilder || self.WebKitBlobBuilder || self.MozBlobBuilder || self.MSBlobBuilder;
5555
var builder = new Builder();
5656
builder.append(part);
@@ -67,8 +67,10 @@ exports.newBlob = function(part, type) {
6767
};
6868
/**
6969
* The identity function.
70-
* @param {Object} input the input.
71-
* @return {Object} the same input.
70+
*
71+
* @template {object} T
72+
* @param {T} input the input.
73+
* @return {T} the same input.
7274
*/
7375
function identity(input) {
7476
return input;
@@ -373,12 +375,13 @@ exports.getTypeOf = function(input) {
373375
* @throws {Error} an Error if the browser doesn't support the requested type.
374376
* @returns {asserts type is keyof support}
375377
*/
376-
exports.checkSupport = function(type) {
378+
function checkSupport(type) {
377379
var supported = support[/** @type {keyof support} */ (type.toLowerCase())];
378380
if (!supported) {
379381
throw new Error(type + " is not supported by this platform");
380382
}
381-
};
383+
}
384+
exports.checkSupport = checkSupport;
382385

383386
exports.MAX_VALUE_16BITS = 65535;
384387
exports.MAX_VALUE_32BITS = -1; // well, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" is parsed as -1

0 commit comments

Comments
 (0)