@jsb188/app 1.1.1 → 1.1.4

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/utils/object.ts CHANGED
@@ -29,7 +29,7 @@ export function uniq(arr: Array<any>, fn?: (value: any, index: number, self: Arr
29
29
  * Remove undefined (and/or null) values from an object
30
30
  */
31
31
 
32
- export function removeUndefined(obj: Record<string, any>, removeNull?: boolean) {
32
+ export function removeUndefined<T>(obj: T, removeNull?: boolean) {
33
33
  for (const key in obj) {
34
34
  if (Object.prototype.hasOwnProperty.call(obj, key)) {
35
35
  if (
@@ -276,15 +276,15 @@ export function intersection(arr1: Array<any>, arr2: Array<any>) {
276
276
  */
277
277
 
278
278
  export function getNestedValue(obj: any, path: string): any {
279
- if (!obj || !path) {
280
- return undefined;
281
- }
282
- return path.split('.').reduce((o, key) => {
283
- if (o && typeof o === 'object' && key in o) {
284
- return o[key];
285
- }
286
- return undefined;
287
- }, obj);
279
+ if (!obj || !path) {
280
+ return undefined;
281
+ }
282
+ return path.split('.').reduce((o, key) => {
283
+ if (o && typeof o === 'object' && key in o) {
284
+ return o[key];
285
+ }
286
+ return undefined;
287
+ }, obj);
288
288
  }
289
289
 
290
290
  /**
@@ -296,27 +296,27 @@ export function getNestedValue(obj: any, path: string): any {
296
296
  */
297
297
 
298
298
  export function assignToNestedObject(obj: any, path: string, value: any) {
299
- if (!obj || !path) {
300
- return;
301
- }
302
- const keys = path.split('.');
303
- const lastKey = keys.pop();
304
- const nestedObj = keys.reduce((o, key) => {
305
- if (!o[key]) {
306
- o[key] = {};
307
- }
308
- return o[key];
309
- }, obj);
310
-
311
- if (lastKey) {
312
- if (typeof nestedObj === 'object' && nestedObj !== null) {
313
- nestedObj[lastKey] = value;
314
- } else {
315
- console.warn(`(!1) Cannot assign value to ${lastKey} because its reference is not an object.`);
316
- }
317
- }
318
-
319
- return obj;
299
+ if (!obj || !path) {
300
+ return;
301
+ }
302
+ const keys = path.split('.');
303
+ const lastKey = keys.pop();
304
+ const nestedObj = keys.reduce((o, key) => {
305
+ if (!o[key]) {
306
+ o[key] = {};
307
+ }
308
+ return o[key];
309
+ }, obj);
310
+
311
+ if (lastKey) {
312
+ if (typeof nestedObj === 'object' && nestedObj !== null) {
313
+ nestedObj[lastKey] = value;
314
+ } else {
315
+ console.warn(`(!1) Cannot assign value to ${lastKey} because its reference is not an object.`);
316
+ }
317
+ }
318
+
319
+ return obj;
320
320
  }
321
321
 
322
322
  /**
@@ -336,15 +336,15 @@ export function groupCollections(collections: Array<any>, innerCollectionNames:
336
336
 
337
337
  if (i === -1) {
338
338
  innerCollectionNames.forEach((name: string) => {
339
- const ref = getNestedValue(obj, name);
339
+ const ref = getNestedValue(obj, name);
340
340
  if (!ref) {
341
- assignToNestedObject(obj, name, []);
341
+ assignToNestedObject(obj, name, []);
342
342
  } else {
343
- assignToNestedObject(obj, name, [ref]);
343
+ assignToNestedObject(obj, name, [ref]);
344
344
  }
345
345
 
346
- // Old version, but this doesn't support merged array collections
347
- // if (!obj[name]) {
346
+ // Old version, but this doesn't support merged array collections
347
+ // if (!obj[name]) {
348
348
  // obj[name] = [];
349
349
  // } else {
350
350
  // obj[name] = [obj[name]];
@@ -354,16 +354,16 @@ export function groupCollections(collections: Array<any>, innerCollectionNames:
354
354
  acc.push(obj);
355
355
  } else {
356
356
  innerCollectionNames.forEach((name: string) => {
357
- const innerObj = getNestedValue(obj, name);
358
- const ref = getNestedValue(acc[i], name);
357
+ const innerObj = getNestedValue(obj, name);
358
+ const ref = getNestedValue(acc[i], name);
359
359
 
360
360
  if (!ref) {
361
- assignToNestedObject(acc[i], name, innerObj ? [innerObj] : []);
361
+ assignToNestedObject(acc[i], name, innerObj ? [innerObj] : []);
362
362
  } else if (innerObj && !ref.find((o: any) => o.id === innerObj.id)) {
363
- assignToNestedObject(acc[i], name, ref.concat(innerObj));
363
+ assignToNestedObject(acc[i], name, ref.concat(innerObj));
364
364
  }
365
365
 
366
- // Old version, but this doesn't support merged array collections
366
+ // Old version, but this doesn't support merged array collections
367
367
  // const innerObj = obj[name];
368
368
  // if (!acc[i][name]) {
369
369
  // acc[i][name] = innerObj ? [innerObj] : [];
package/utils/string.ts CHANGED
@@ -41,20 +41,25 @@ export function shortenToSpace(
41
41
  * Shorten word to space, and remove article words
42
42
  */
43
43
 
44
- export function shortenName(str: string, len: number = 40): string {
45
- const articleReplacedStart = str?.replace(/^(a|an|the|da|de|that|there|this|than|then)\b/i, '') || '';
46
- if (articleReplacedStart.length < 5) {
47
- return shortenToSpace(str, len, '');
44
+ export function guessFirstName(str: string, len: number = 40): string {
45
+ let shortened = str?.replace(/^(a|an|the|da|de|that|there|this|than|then)\b/i, '')?.trim() || '';
46
+ const spaceIx = shortened.indexOf(' ');
47
+
48
+ if (spaceIx >= 0) {
49
+ shortened = shortened.substring(0, spaceIx + 1);
50
+ }
51
+
52
+ if (len < shortened.length) {
53
+ shortened = shortened.substring(0, len);
48
54
  }
49
55
 
50
- const shortened = shortenToSpace(articleReplacedStart, len, '');
51
56
  const articleReplacedEnd = shortened.replace(
52
57
  /\b(a|an|the|da|de|that|there|this|than|then)$/i,
53
58
  '',
54
59
  );
55
60
 
56
61
  if (articleReplacedEnd.length < 5) {
57
- return shortened;
62
+ return shortened.trim();
58
63
  }
59
64
 
60
65
  return articleReplacedEnd.trim();
@@ -191,9 +196,9 @@ export function regexFromText(text: string) {
191
196
  */
192
197
 
193
198
  export function ucFirst(str: string) {
194
- if (!str) {
195
- return '';
196
- }
199
+ if (!str) {
200
+ return '';
201
+ }
197
202
  return str.charAt(0).toUpperCase() + str.slice(1);
198
203
  }
199
204
 
@@ -261,7 +266,7 @@ export function isInvisibleString(str: string) {
261
266
  * NOTE: Userful for building AI prompts
262
267
  */
263
268
 
264
- export function buildSingleText(lines: (string | undefined | false | null)[]) {
269
+ export function buildSingleText(lines: (string | undefined | false | null)[], delemiter: string = '\n') {
265
270
  return lines
266
271
  .reduce((acc, line_) => {
267
272
  const line = line_ || '';
@@ -270,7 +275,7 @@ export function buildSingleText(lines: (string | undefined | false | null)[]) {
270
275
  }
271
276
  return acc;
272
277
  }, [] as string[])
273
- .join('\n')
278
+ .join(delemiter)
274
279
  .trim();
275
280
  }
276
281
 
@@ -284,14 +289,14 @@ export function buildSingleText(lines: (string | undefined | false | null)[]) {
284
289
  */
285
290
 
286
291
  export function joinReadable(
287
- texts: string[],
288
- separator: string = ', ',
289
- lastSeparator: string = ' and ',
292
+ texts: string[],
293
+ separator: string = ', ',
294
+ lastSeparator: string = ' and ',
290
295
  ) {
291
- const lastIx = texts.length - 1;
292
- return texts.reduce((acc, text, i) => {
293
- return acc + (!acc ? '' : i === lastIx ? lastSeparator : separator) + text;
294
- }, '');
296
+ const lastIx = texts.length - 1;
297
+ return texts.reduce((acc, text, i) => {
298
+ return acc + (!acc ? '' : i === lastIx ? lastSeparator : separator) + text;
299
+ }, '');
295
300
  }
296
301
 
297
302
  /**
@@ -304,20 +309,36 @@ export function joinReadable(
304
309
  */
305
310
 
306
311
  export function textWithBrackets(
307
- text: string,
308
- bracketsText?: string | string[],
309
- brackets: [string, string] = ['(', ')']
312
+ text: string,
313
+ bracketsText?: string | string[],
314
+ brackets: [string, string] = ['(', ')'],
310
315
  ) {
311
- let bText;
312
- if (Array.isArray(bracketsText)) {
313
- bText = bracketsText.filter(Boolean).join(' ').trim();
314
- } else {
315
- bText = bracketsText?.trim();
316
- }
317
- if (!bText) {
318
- return text;
319
- } else if (!text) {
320
- return bText;
321
- }
322
- return `${text} ${brackets[0].trimStart()}${bText}${brackets[1].trimEnd()}`;
316
+ let bText;
317
+ if (Array.isArray(bracketsText)) {
318
+ bText = bracketsText.filter(Boolean).join(' ').trim();
319
+ } else {
320
+ bText = bracketsText?.trim();
321
+ }
322
+ if (!bText) {
323
+ return text;
324
+ } else if (!text) {
325
+ return bText;
326
+ }
327
+ return `${text} ${brackets[0].trimStart()}${bText}${brackets[1].trimEnd()}`;
328
+ }
329
+
330
+ /**
331
+ * Remove all line breaks and spaces from String
332
+ * NOTE: This is designed to remove garbage string from OpenAI text generation
333
+ *
334
+ * @param str - String to clean
335
+ * @returns Cleaned string without line breaks and spaces
336
+ */
337
+
338
+ export function removeGeneratedTextGarbage(str: string) {
339
+ if (!str || typeof str !== 'string') {
340
+ return '';
341
+ }
342
+ // Remove all line breaks and spaces
343
+ return str.replace(/[\n\r\s]+/g, ' ').trim();
323
344
  }