@jvsoft/utils 1.0.0-alpha.4 → 1.0.0-alpha.5

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.
@@ -262,6 +262,30 @@ function eliminarElementos(origen, elementosAEliminar, claves) {
262
262
  return !clavesSet.has(key);
263
263
  });
264
264
  }
265
+ /**
266
+ * Filtra datos localmente según un valor de búsqueda
267
+ * @param data - Array de datos a filtrar
268
+ * @param value - Valor de búsqueda
269
+ * @param campoBuscar - Campo(s) por el cual buscar
270
+ * @returns Array filtrado
271
+ */
272
+ function filtrarDatosLocal(data, value, campoBuscar) {
273
+ if (!value)
274
+ return data;
275
+ const normalizar = (val) => val?.toString()?.toLowerCase() ?? '';
276
+ const esNum = !isNaN(Number(value));
277
+ return data.filter(item => {
278
+ const campos = Array.isArray(campoBuscar) ? campoBuscar : [campoBuscar];
279
+ return campos.some(campo => {
280
+ const campoVal = item[campo];
281
+ if (campoVal == null)
282
+ return false;
283
+ return esNum
284
+ ? campoVal.toString().includes(value.toString())
285
+ : normalizar(campoVal).includes(normalizar(value));
286
+ });
287
+ });
288
+ }
265
289
 
266
290
  function mostrarValorEnBusqueda(campos, idxSel) {
267
291
  const impDataMostrar = () => {
@@ -484,17 +508,192 @@ function changeSelectApi(control, queryService, formControl, tipo, dataExtra = {
484
508
  }
485
509
  });
486
510
  }
511
+ /**
512
+ * Comprueba si un valor es una Promesa
513
+ * @param valor - Valor a verificar
514
+ * @returns true si es una Promise
515
+ */
516
+ function esPromise(valor) {
517
+ return !!valor && typeof valor.then === 'function';
518
+ }
519
+ /**
520
+ * Selecciona todo el texto de un input
521
+ * @param event - Evento del input
522
+ */
523
+ function seleccionarTextoInput$1(event) {
524
+ event.target.select();
525
+ }
526
+ /**
527
+ * Función genérica para vincular un FormControl con datos desde API o Promise.
528
+ * Preparada para migrar a signals en el futuro.
529
+ */
530
+ function changeSelectReformateado(config) {
531
+ const { objThis, tipoReq, formControl, queryService, campoId, minLength = 3, dataExtra = {}, dataExtraVariable = [], anonimo = false, variableResultado = tipoReq, } = config;
532
+ formControl.valueChanges.pipe(debounceTime(500), tap(() => {
533
+ objThis.filtrados[variableResultado + 'tmp'] = isObservable(objThis.filtrados[variableResultado])
534
+ ? []
535
+ : objThis.filtrados[variableResultado] || [];
536
+ if (objThis.filtrados[variableResultado] !== objThis.filtrados[variableResultado + 'tmp']) {
537
+ objThis.filtrados[variableResultado] = [];
538
+ }
539
+ objThis.isLoading = true;
540
+ }), switchMap(value => {
541
+ if (campoId) {
542
+ const existe = objThis.filtrados[variableResultado + 'tmp']
543
+ .findIndex((item) => item[campoId] === value);
544
+ if (existe >= 0) {
545
+ return of({ [tipoReq]: objThis.filtrados[variableResultado + 'tmp'] });
546
+ }
547
+ }
548
+ if (!value || value.length < minLength) {
549
+ objThis.isLoading = false;
550
+ return of({ [tipoReq]: [] });
551
+ }
552
+ const extraVars = Object.fromEntries(dataExtraVariable.map((v) => [v.campo, v.ctrlValue.value]));
553
+ const query = queryService.getDataMethod('GET', tipoReq, { ...dataExtra, ...extraVars, txtBuscar: value }, anonimo);
554
+ if (esPromise(query)) {
555
+ return query
556
+ .then((data) => ({ [tipoReq]: data[tipoReq] ?? [] }))
557
+ .finally(() => { objThis.isLoading = false; });
558
+ }
559
+ return query.pipe(finalize(() => { objThis.isLoading = false; }));
560
+ })).subscribe((data) => {
561
+ objThis.filtrados[variableResultado] = data[tipoReq] ?? [];
562
+ });
563
+ }
487
564
 
488
565
  function seleccionarTextoInput(event) {
489
566
  event.target.select();
490
567
  }
491
568
 
569
+ /**
570
+ * Codifica un string a base64 (método legacy)
571
+ * @param val - String a codificar
572
+ * @returns String codificado en base64
573
+ * @deprecated Use encodeBase64String() en su lugar
574
+ */
492
575
  function b64Encode(val) {
493
576
  return Buffer.from(val, 'binary').toString('base64');
494
577
  }
578
+ /**
579
+ * Decodifica un string desde base64 (método legacy)
580
+ * @param val - String en base64 a decodificar
581
+ * @returns String decodificado
582
+ * @deprecated Use decodeBase64String() en su lugar
583
+ */
495
584
  function b64Decode(val) {
496
585
  return Buffer.from(val, 'base64').toString('binary');
497
586
  }
587
+ /**
588
+ * Codificar string a Base64 (UTF-8 seguro)
589
+ * @param str - String a codificar
590
+ * @returns String codificado en base64
591
+ *
592
+ * @example
593
+ * ```typescript
594
+ * const encoded = encodeBase64String('Hola Mundo ñ');
595
+ * // encoded: "SG9sYSBNdW5kbyDDsQ=="
596
+ * ```
597
+ */
598
+ function encodeBase64String(str) {
599
+ const encoder = new TextEncoder();
600
+ const bytes = encoder.encode(str);
601
+ let binary = '';
602
+ bytes.forEach(b => binary += String.fromCharCode(b));
603
+ return btoa(binary);
604
+ }
605
+ /**
606
+ * Decodificar Base64 a string (UTF-8 seguro)
607
+ * @param b64 - String en base64 a decodificar
608
+ * @returns String decodificado
609
+ *
610
+ * @example
611
+ * ```typescript
612
+ * const decoded = decodeBase64String('SG9sYSBNdW5kbyDDsQ==');
613
+ * // decoded: "Hola Mundo ñ"
614
+ * ```
615
+ */
616
+ function decodeBase64String(b64) {
617
+ const binary = atob(b64);
618
+ const bytes = new Uint8Array(binary.length);
619
+ for (let i = 0; i < binary.length; i++) {
620
+ bytes[i] = binary.charCodeAt(i);
621
+ }
622
+ const decoder = new TextDecoder();
623
+ return decoder.decode(bytes);
624
+ }
625
+ /**
626
+ * Codificar un objeto a base64 (UTF-8 seguro)
627
+ * @param obj - Objeto a codificar
628
+ * @returns String codificado en base64
629
+ *
630
+ * @example
631
+ * ```typescript
632
+ * const obj = { nombre: 'Juan', edad: 25 };
633
+ * const encoded = encodeBase64Object(obj);
634
+ * ```
635
+ */
636
+ function encodeBase64Object(obj) {
637
+ return encodeBase64String(JSON.stringify(obj));
638
+ }
639
+ /**
640
+ * Decodificar un base64 y obtener el objeto original
641
+ * @param b64 - String en base64 a decodificar
642
+ * @returns Objeto decodificado
643
+ *
644
+ * @example
645
+ * ```typescript
646
+ * const obj = decodeBase64Object<{ nombre: string, edad: number }>(encoded);
647
+ * // obj: { nombre: 'Juan', edad: 25 }
648
+ * ```
649
+ */
650
+ function decodeBase64Object(b64) {
651
+ return JSON.parse(decodeBase64String(b64));
652
+ }
653
+ /**
654
+ * Codificar archivo a Base64 (retorna solo el contenido, sin "data:...")
655
+ * @param file - Archivo o Blob a codificar
656
+ * @returns Promise con el string en base64
657
+ *
658
+ * @example
659
+ * ```typescript
660
+ * const file = event.target.files[0];
661
+ * const base64 = await encodeBase64File(file);
662
+ * // base64: "iVBORw0KGgoAAAANSUhEUgAA..."
663
+ * ```
664
+ */
665
+ function encodeBase64File(file) {
666
+ return new Promise((resolve, reject) => {
667
+ const reader = new FileReader();
668
+ reader.onload = () => {
669
+ const result = reader.result;
670
+ resolve(result.split(',')[1]); // quita el prefijo "data:...;base64,"
671
+ };
672
+ reader.onerror = reject;
673
+ reader.readAsDataURL(file);
674
+ });
675
+ }
676
+ /**
677
+ * Decodificar Base64 a Blob (para reconstruir archivos en Angular)
678
+ * @param b64 - String en base64
679
+ * @param mimeType - Tipo MIME del archivo (opcional)
680
+ * @returns Blob con el contenido decodificado
681
+ *
682
+ * @example
683
+ * ```typescript
684
+ * const blob = decodeBase64ToBlob(base64String, 'image/png');
685
+ * const url = URL.createObjectURL(blob);
686
+ * // Usar url para mostrar imagen o descargar
687
+ * ```
688
+ */
689
+ function decodeBase64ToBlob(b64, mimeType = 'application/octet-stream') {
690
+ const byteChars = atob(b64);
691
+ const byteNumbers = new Array(byteChars.length);
692
+ for (let i = 0; i < byteChars.length; i++) {
693
+ byteNumbers[i] = byteChars.charCodeAt(i);
694
+ }
695
+ return new Blob([new Uint8Array(byteNumbers)], { type: mimeType });
696
+ }
498
697
 
499
698
  function getBrowserName() {
500
699
  const agent = window.navigator.userAgent.toLowerCase();
@@ -1921,5 +2120,5 @@ function zeroFill(value, digitos, ...args) {
1921
2120
  * Generated bundle index. Do not edit.
1922
2121
  */
1923
2122
 
1924
- export { DataEnListaPipe, DataModel, DateDiffStringPipe, FiltroPipe, FormControlIsRequiredPipe, JsonParsePipe, NoSanitizePipe, TipoValorFuncionPipe, ZeroFillPipe, b64Decode, b64Encode, buscarPorCampo, changeSelect, changeSelectApi, changeSelectData, changeSelectDataApi, convertirBytes, dataEnLista, dateDiffString, deepClone, deepMerge, delLocalStorage, desencriptar, devError, devGroup, devGroupEnd, devLog, devTable, devTime, devTimeEnd, devWarn, downLoadFileStream, eliminarColumnaPorIndex, eliminarDuplicados, eliminarElementos, encriptar, esNumero, establecerQuitarRequired, extraerDominio, formControlIsRequired, formatearFecha, formatearFechaCadena, formatearFechaFormato, generateRandomString, getBrowserName, getCambiarPwd, getDataArchivoFromPath, getFormValidationErrors, getLocalStorage, getUniqueValues, getUniqueValuesByProperty, groupBy, inicializarVariablesSessionStorage, isEmail, isProduction, jwtToken, jwtTokenData, jwtTokenExpiracion, jwtTokenExpiracionFaltante, localStorageKeys, markAsTouchedWithoutEmitEvent, maskEmail, mensajeAlerta, mensajeConfirmacion, mensajeTimer, mensajeToast, mensajesDeError, mensajesErrorFormControl, mostrarValorEnBusqueda, nestGroupsBy, numberToWords, objectPropertiesBoolean, objectPropertiesToType, obtenerHostDesdeUrl, obtenerMimeType, obtenerUltimoOrden, ordenarArray, ordenarPorPropiedad, ordenarPorPropiedades, roundToDecimal, sanitizarNombreArchivo, seleccionarTextoInput, setCambiarPwd, setControlDesdeLista, setJwtTokenData, setLocalStorage, setProductionMode, sumarObjetos, sumarPropiedades, tipoValorFuncion, toFormData, transformarFechasPorNombreDeCampo, verificarRUC, zeroFill };
2123
+ export { DataEnListaPipe, DataModel, DateDiffStringPipe, FiltroPipe, FormControlIsRequiredPipe, JsonParsePipe, NoSanitizePipe, TipoValorFuncionPipe, ZeroFillPipe, b64Decode, b64Encode, buscarPorCampo, changeSelect, changeSelectApi, changeSelectData, changeSelectDataApi, changeSelectReformateado, convertirBytes, dataEnLista, dateDiffString, decodeBase64Object, decodeBase64String, decodeBase64ToBlob, deepClone, deepMerge, delLocalStorage, desencriptar, devError, devGroup, devGroupEnd, devLog, devTable, devTime, devTimeEnd, devWarn, downLoadFileStream, eliminarColumnaPorIndex, eliminarDuplicados, eliminarElementos, encodeBase64File, encodeBase64Object, encodeBase64String, encriptar, esNumero, esPromise, establecerQuitarRequired, extraerDominio, filtrarDatosLocal, formControlIsRequired, formatearFecha, formatearFechaCadena, formatearFechaFormato, generateRandomString, getBrowserName, getCambiarPwd, getDataArchivoFromPath, getFormValidationErrors, getLocalStorage, getUniqueValues, getUniqueValuesByProperty, getValueByPath, groupBy, inicializarVariablesSessionStorage, isEmail, isProduction, jwtToken, jwtTokenData, jwtTokenExpiracion, jwtTokenExpiracionFaltante, localStorageKeys, markAsTouchedWithoutEmitEvent, maskEmail, mensajeAlerta, mensajeConfirmacion, mensajeTimer, mensajeToast, mensajesDeError, mensajesErrorFormControl, mostrarValorEnBusqueda, nestGroupsBy, numberToWords, objectPropertiesBoolean, objectPropertiesToType, obtenerHostDesdeUrl, obtenerMimeType, obtenerUltimoOrden, ordenarArray, ordenarPorPropiedad, ordenarPorPropiedades, roundToDecimal, sanitizarNombreArchivo, seleccionarTextoInput, setCambiarPwd, setControlDesdeLista, setJwtTokenData, setLocalStorage, setProductionMode, sumarObjetos, sumarPropiedades, tipoValorFuncion, toFormData, transformarFechasPorNombreDeCampo, verificarRUC, zeroFill };
1925
2124
  //# sourceMappingURL=jvsoft-utils.mjs.map