Skip to content

Commit

Permalink
Use Promise.resolve() to make sure the passed function result is "the…
Browse files Browse the repository at this point in the history
…nable".
  • Loading branch information
MarcoGlauser authored and jackocnr committed Dec 1, 2024
1 parent ba774eb commit 69fbc29
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 50 deletions.
5 changes: 1 addition & 4 deletions build/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -3099,10 +3099,7 @@ var factoryOutput = (() => {
);
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
2 changes: 1 addition & 1 deletion build/js/intlTelInput.min.js

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions build/js/intlTelInputWithUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3094,10 +3094,7 @@ var factoryOutput = (() => {
loadCall = Promise.reject(new Error("INTENTIONALLY BROKEN: this build of intl-tel-input includes the utilities module inline, but it has incorrectly attempted to load the utilities separately. If you are seeing this message, something is broken!"));
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
2 changes: 1 addition & 1 deletion build/js/intlTelInputWithUtils.min.js

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions react/build/IntlTelInput.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3094,10 +3094,7 @@ var loadUtils = (source) => {
);
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
5 changes: 1 addition & 4 deletions react/build/IntlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -3058,10 +3058,7 @@ var loadUtils = (source) => {
);
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
5 changes: 1 addition & 4 deletions react/build/IntlTelInputWithUtils.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3090,10 +3090,7 @@ var loadUtils = (source) => {
loadCall = Promise.reject(new Error("INTENTIONALLY BROKEN: this build of intl-tel-input includes the utilities module inline, but it has incorrectly attempted to load the utilities separately. If you are seeing this message, something is broken!"));
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
5 changes: 1 addition & 4 deletions react/build/IntlTelInputWithUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3054,10 +3054,7 @@ var loadUtils = (source) => {
loadCall = Promise.reject(new Error("INTENTIONALLY BROKEN: this build of intl-tel-input includes the utilities module inline, but it has incorrectly attempted to load the utilities separately. If you are seeing this message, something is broken!"));
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
5 changes: 1 addition & 4 deletions react/demo/set-number/set-number-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26645,10 +26645,7 @@
);
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
5 changes: 1 addition & 4 deletions react/demo/simple/simple-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26645,10 +26645,7 @@
);
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
5 changes: 1 addition & 4 deletions react/demo/toggle-disabled/toggle-disabled-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26645,10 +26645,7 @@
);
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
5 changes: 1 addition & 4 deletions react/demo/validation/validation-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26645,10 +26645,7 @@
);
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
5 changes: 1 addition & 4 deletions src/js/intl-tel-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2151,10 +2151,7 @@ const loadUtils = (source: string|UtilsLoader): Promise<unknown> | null => {
loadCall = import(/* webpackIgnore: true */ /* @vite-ignore */ source);
} else if (typeof source === "function") {
try {
loadCall = source();
if (!(loadCall instanceof Promise)) {
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
}
loadCall = Promise.resolve(source());
} catch (error) {
return Promise.reject(error);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/options/loadUtilsOnInit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ describe("loadUtilsOnInit", () => {
expect(intlTelInput.utils).toBe(mockUtils.default);
});

it("works if loadUtilsOnInit returns a custom promise", async function() {
const mockUtils = { default: { mockUtils: true } };

const { iti } = initPlugin({
intlTelInput,
options: {
loadUtilsOnInit: () => ({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
then: (resolve, reject) => resolve(mockUtils),
}),
},
});

expect(intlTelInput).toHaveProperty("startedLoadingUtilsScript", true);
await iti.promise;

expect(intlTelInput.utils).toBe(mockUtils.default);
});

describe("in 'withUtils' builds", () => {
const intlTelInput = require("intl-tel-input/intlTelInputWithUtils");
resetPackageAfterEach(intlTelInput);
Expand Down
3 changes: 1 addition & 2 deletions vue/build/IntlTelInput.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2465,8 +2465,7 @@ const W = (u) => {
);
else if (typeof u == "function")
try {
if (t = u(), !(t instanceof Promise))
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof t}`);
t = Promise.resolve(u());
} catch (e) {
return Promise.reject(e);
}
Expand Down
3 changes: 1 addition & 2 deletions vue/build/IntlTelInputWithUtils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2461,8 +2461,7 @@ const V2 = (y) => {
e = Promise.reject(new Error("INTENTIONALLY BROKEN: this build of intl-tel-input includes the utilities module inline, but it has incorrectly attempted to load the utilities separately. If you are seeing this message, something is broken!"));
else if (typeof y == "function")
try {
if (e = y(), !(e instanceof Promise))
throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof e}`);
e = Promise.resolve(y());
} catch (i) {
return Promise.reject(i);
}
Expand Down

0 comments on commit 69fbc29

Please sign in to comment.