@react-pakistan/util-functions 1.24.83 → 1.24.85
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.
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a remote image URL to a base64 string
|
|
3
|
+
* @param url - The remote URL of the image
|
|
4
|
+
* @returns Promise<string> - The base64 encoded string of the image
|
|
5
|
+
* @throws Error if the image cannot be fetched or converted
|
|
6
|
+
*/
|
|
7
|
+
export declare const imageUrlToBase64: (url: string) => Promise<string>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a remote image URL to a base64 string
|
|
4
|
+
* @param url - The remote URL of the image
|
|
5
|
+
* @returns Promise<string> - The base64 encoded string of the image
|
|
6
|
+
* @throws Error if the image cannot be fetched or converted
|
|
7
|
+
*/
|
|
8
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
18
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
19
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
20
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
21
|
+
function step(op) {
|
|
22
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
23
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
24
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
25
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
26
|
+
switch (op[0]) {
|
|
27
|
+
case 0: case 1: t = op; break;
|
|
28
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
29
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
30
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
31
|
+
default:
|
|
32
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
33
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
34
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
35
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
36
|
+
if (t[2]) _.ops.pop();
|
|
37
|
+
_.trys.pop(); continue;
|
|
38
|
+
}
|
|
39
|
+
op = body.call(thisArg, _);
|
|
40
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
41
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.imageUrlToBase64 = void 0;
|
|
46
|
+
var imageUrlToBase64 = function (url) { return __awaiter(void 0, void 0, void 0, function () {
|
|
47
|
+
var response, blob_1, error_1;
|
|
48
|
+
return __generator(this, function (_a) {
|
|
49
|
+
switch (_a.label) {
|
|
50
|
+
case 0:
|
|
51
|
+
_a.trys.push([0, 3, , 4]);
|
|
52
|
+
return [4 /*yield*/, fetch(url)];
|
|
53
|
+
case 1:
|
|
54
|
+
response = _a.sent();
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
throw new Error("Failed to fetch image: ".concat(response.statusText));
|
|
57
|
+
}
|
|
58
|
+
return [4 /*yield*/, response.blob()];
|
|
59
|
+
case 2:
|
|
60
|
+
blob_1 = _a.sent();
|
|
61
|
+
// Convert blob to base64
|
|
62
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
63
|
+
var reader = new FileReader();
|
|
64
|
+
reader.onloadend = function () {
|
|
65
|
+
var base64String = reader.result;
|
|
66
|
+
resolve(base64String);
|
|
67
|
+
};
|
|
68
|
+
reader.onerror = function () {
|
|
69
|
+
reject(new Error('Failed to convert image to base64'));
|
|
70
|
+
};
|
|
71
|
+
reader.readAsDataURL(blob_1);
|
|
72
|
+
})];
|
|
73
|
+
case 3:
|
|
74
|
+
error_1 = _a.sent();
|
|
75
|
+
throw new Error("Error converting image URL to base64: ".concat(error_1 instanceof Error ? error_1.message : 'Unknown error'));
|
|
76
|
+
case 4: return [2 /*return*/];
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}); };
|
|
80
|
+
exports.imageUrlToBase64 = imageUrlToBase64;
|
package/general/index.d.ts
CHANGED
|
@@ -37,6 +37,7 @@ export * from './get-month-number';
|
|
|
37
37
|
export * from './get-number-of-days-in-month';
|
|
38
38
|
export * from './get-pathname';
|
|
39
39
|
export * from './get-week-day';
|
|
40
|
+
export * from './image-url-to-base64';
|
|
40
41
|
export * from './is-next-button-disabled';
|
|
41
42
|
export * from './is-odd';
|
|
42
43
|
export * from './is-previous-button-disabled';
|
package/general/index.js
CHANGED
|
@@ -53,6 +53,7 @@ __exportStar(require("./get-month-number"), exports);
|
|
|
53
53
|
__exportStar(require("./get-number-of-days-in-month"), exports);
|
|
54
54
|
__exportStar(require("./get-pathname"), exports);
|
|
55
55
|
__exportStar(require("./get-week-day"), exports);
|
|
56
|
+
__exportStar(require("./image-url-to-base64"), exports);
|
|
56
57
|
__exportStar(require("./is-next-button-disabled"), exports);
|
|
57
58
|
__exportStar(require("./is-odd"), exports);
|
|
58
59
|
__exportStar(require("./is-previous-button-disabled"), exports);
|