@pistonite/pure 0.26.0 → 0.26.2
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/package.json +1 -1
- package/src/pref/locale.ts +17 -4
- package/src/result/index.ts +27 -8
package/package.json
CHANGED
package/src/pref/locale.ts
CHANGED
|
@@ -224,14 +224,27 @@ export const setLocale = (newLocale: string): boolean => {
|
|
|
224
224
|
export const convertToSupportedLocale = (
|
|
225
225
|
newLocale: string,
|
|
226
226
|
): string | undefined => {
|
|
227
|
-
|
|
227
|
+
return convertToSupportedLocaleIn(newLocale, supportedLocales);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* See {@link convertToSupportedLocale}
|
|
232
|
+
*
|
|
233
|
+
* This takes the supported locale array so it can be used
|
|
234
|
+
* outside of the locale system
|
|
235
|
+
*/
|
|
236
|
+
export const convertToSupportedLocaleIn = (
|
|
237
|
+
newLocale: string,
|
|
238
|
+
supportedLocalesToCheck: string[] | readonly string[],
|
|
239
|
+
): string | undefined => {
|
|
240
|
+
if (supportedLocalesToCheck.includes(newLocale)) {
|
|
228
241
|
return newLocale;
|
|
229
242
|
}
|
|
230
243
|
const language = newLocale.split("-", 2)[0];
|
|
231
|
-
const len =
|
|
244
|
+
const len = supportedLocalesToCheck.length;
|
|
232
245
|
for (let i = 0; i < len; i++) {
|
|
233
|
-
if (
|
|
234
|
-
return
|
|
246
|
+
if (supportedLocalesToCheck[i].startsWith(language)) {
|
|
247
|
+
return supportedLocalesToCheck[i];
|
|
235
248
|
}
|
|
236
249
|
}
|
|
237
250
|
return undefined;
|
package/src/result/index.ts
CHANGED
|
@@ -197,19 +197,38 @@ export async function tryAsync<T, E = unknown>(
|
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
/** Try best effort converting an error to a string */
|
|
200
|
-
export function errstr(e: unknown): string {
|
|
200
|
+
export function errstr(e: unknown, recursing?: boolean): string {
|
|
201
201
|
if (typeof e === "string") {
|
|
202
202
|
return e;
|
|
203
203
|
}
|
|
204
|
-
if (e) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
204
|
+
if (!e) {
|
|
205
|
+
return `${e}`;
|
|
206
|
+
}
|
|
207
|
+
if (typeof e === "object" && "message" in e) {
|
|
208
|
+
if (!recursing) {
|
|
209
|
+
return errstr(e.message, true);
|
|
210
|
+
}
|
|
211
|
+
return `${e.message}`;
|
|
212
|
+
}
|
|
213
|
+
if (typeof e === "object" && "toString" in e) {
|
|
214
|
+
const s = e.toString();
|
|
215
|
+
if (!recursing) {
|
|
216
|
+
return errstr(s, true);
|
|
209
217
|
}
|
|
210
|
-
|
|
211
|
-
|
|
218
|
+
return `${s}`;
|
|
219
|
+
}
|
|
220
|
+
// try less-likely fields
|
|
221
|
+
if (typeof e === "object" && "msg" in e) {
|
|
222
|
+
if (!recursing) {
|
|
223
|
+
return errstr(e.msg, true);
|
|
224
|
+
}
|
|
225
|
+
return `${e.msg}`;
|
|
226
|
+
}
|
|
227
|
+
if (typeof e === "object" && "code" in e) {
|
|
228
|
+
if (!recursing) {
|
|
229
|
+
return `error code: ${errstr(e.code, true)}`;
|
|
212
230
|
}
|
|
231
|
+
return `${e.code}`;
|
|
213
232
|
}
|
|
214
233
|
return `${e}`;
|
|
215
234
|
}
|