@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pistonite/pure",
3
- "version": "0.26.0",
3
+ "version": "0.26.2",
4
4
  "type": "module",
5
5
  "description": "Pure TypeScript libraries for my projects",
6
6
  "homepage": "https://github.com/Pistonite/pure",
@@ -224,14 +224,27 @@ export const setLocale = (newLocale: string): boolean => {
224
224
  export const convertToSupportedLocale = (
225
225
  newLocale: string,
226
226
  ): string | undefined => {
227
- if (supportedLocales.includes(newLocale)) {
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 = supportedLocales.length;
244
+ const len = supportedLocalesToCheck.length;
232
245
  for (let i = 0; i < len; i++) {
233
- if (supportedLocales[i].startsWith(language)) {
234
- return supportedLocales[i];
246
+ if (supportedLocalesToCheck[i].startsWith(language)) {
247
+ return supportedLocalesToCheck[i];
235
248
  }
236
249
  }
237
250
  return undefined;
@@ -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
- if (typeof e === "object" && "message" in e) {
206
- if (typeof e.message === "string") {
207
- return e.message;
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
- if (typeof e === "object" && "toString" in e) {
211
- return e.toString();
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
  }