iterflow 0.4.0 → 0.7.0

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/dist/index.cjs CHANGED
@@ -213,6 +213,7 @@ function validateIndex(index, size, operation) {
213
213
  // src/iter-flow.ts
214
214
  var iterflow = class _iterflow {
215
215
  source;
216
+ _sourceArray;
216
217
  /**
217
218
  * Creates a new iterflow instance from an iterable or iterator.
218
219
  *
@@ -224,7 +225,13 @@ var iterflow = class _iterflow {
224
225
  * ```
225
226
  */
226
227
  constructor(source) {
227
- this.source = Symbol.iterator in source ? source[Symbol.iterator]() : source;
228
+ if (Array.isArray(source)) {
229
+ this._sourceArray = source;
230
+ this.source = source[Symbol.iterator]();
231
+ } else {
232
+ this._sourceArray = void 0;
233
+ this.source = Symbol.iterator in source ? source[Symbol.iterator]() : source;
234
+ }
228
235
  }
229
236
  // Iterator protocol
230
237
  /**
@@ -300,6 +307,9 @@ var iterflow = class _iterflow {
300
307
  * ```
301
308
  */
302
309
  take(limit) {
310
+ if (this._sourceArray) {
311
+ return new _iterflow(this._sourceArray.slice(0, limit));
312
+ }
303
313
  const self = this;
304
314
  return new _iterflow({
305
315
  *[Symbol.iterator]() {
@@ -323,6 +333,9 @@ var iterflow = class _iterflow {
323
333
  * ```
324
334
  */
325
335
  drop(count) {
336
+ if (this._sourceArray) {
337
+ return new _iterflow(this._sourceArray.slice(count));
338
+ }
326
339
  const self = this;
327
340
  return new _iterflow({
328
341
  *[Symbol.iterator]() {
@@ -475,6 +488,9 @@ var iterflow = class _iterflow {
475
488
  * ```
476
489
  */
477
490
  reverse() {
491
+ if (this._sourceArray) {
492
+ return new _iterflow(this._sourceArray.slice().reverse());
493
+ }
478
494
  const self = this;
479
495
  return new _iterflow({
480
496
  *[Symbol.iterator]() {
@@ -502,6 +518,16 @@ var iterflow = class _iterflow {
502
518
  * ```
503
519
  */
504
520
  sort() {
521
+ if (this._sourceArray) {
522
+ return new _iterflow(
523
+ this._sourceArray.slice().sort((a, b) => {
524
+ if (typeof a === "number" && typeof b === "number") {
525
+ return a - b;
526
+ }
527
+ return String(a).localeCompare(String(b));
528
+ })
529
+ );
530
+ }
505
531
  const self = this;
506
532
  return new _iterflow({
507
533
  *[Symbol.iterator]() {
@@ -534,6 +560,9 @@ var iterflow = class _iterflow {
534
560
  * ```
535
561
  */
536
562
  sortBy(compareFn) {
563
+ if (this._sourceArray) {
564
+ return new _iterflow(this._sourceArray.slice().sort(compareFn));
565
+ }
537
566
  const self = this;
538
567
  return new _iterflow({
539
568
  *[Symbol.iterator]() {
@@ -555,6 +584,9 @@ var iterflow = class _iterflow {
555
584
  * ```
556
585
  */
557
586
  toArray() {
587
+ if (this._sourceArray) {
588
+ return this._sourceArray.slice();
589
+ }
558
590
  return Array.from(this);
559
591
  }
560
592
  /**
@@ -568,6 +600,9 @@ var iterflow = class _iterflow {
568
600
  * ```
569
601
  */
570
602
  count() {
603
+ if (this._sourceArray) {
604
+ return this._sourceArray.length;
605
+ }
571
606
  let count = 0;
572
607
  for (const _ of this) {
573
608
  count++;
@@ -588,6 +623,13 @@ var iterflow = class _iterflow {
588
623
  * ```
589
624
  */
590
625
  sum() {
626
+ if (this._sourceArray) {
627
+ let total2 = 0;
628
+ for (let i = 0; i < this._sourceArray.length; i++) {
629
+ total2 += this._sourceArray[i];
630
+ }
631
+ return total2;
632
+ }
591
633
  let total = 0;
592
634
  for (const value of this) {
593
635
  total += value;
@@ -676,14 +718,14 @@ var iterflow = class _iterflow {
676
718
  * ```
677
719
  */
678
720
  median() {
679
- const values = this.toArray();
721
+ const values = this._sourceArray || this.toArray();
680
722
  if (values.length === 0) return void 0;
681
- values.sort((a, b) => a - b);
682
- const mid = Math.floor(values.length / 2);
683
- if (values.length % 2 === 0) {
684
- return (values[mid - 1] + values[mid]) / 2;
723
+ const sorted = values.slice().sort((a, b) => a - b);
724
+ const mid = Math.floor(sorted.length / 2);
725
+ if (sorted.length % 2 === 0) {
726
+ return (sorted[mid - 1] + sorted[mid]) / 2;
685
727
  } else {
686
- return values[mid];
728
+ return sorted[mid];
687
729
  }
688
730
  }
689
731
  /**
@@ -701,7 +743,7 @@ var iterflow = class _iterflow {
701
743
  * ```
702
744
  */
703
745
  variance() {
704
- const values = this.toArray();
746
+ const values = this._sourceArray || this.toArray();
705
747
  if (values.length === 0) return void 0;
706
748
  const mean = values.reduce((sum, val) => sum + val, 0) / values.length;
707
749
  let sumSquaredDiffs = 0;
@@ -749,19 +791,19 @@ var iterflow = class _iterflow {
749
791
  */
750
792
  percentile(p) {
751
793
  validateRange(p, 0, 100, "percentile", "percentile");
752
- const values = this.toArray();
794
+ const values = this._sourceArray || this.toArray();
753
795
  if (values.length === 0) return void 0;
754
- values.sort((a, b) => a - b);
755
- if (p === 0) return values[0];
756
- if (p === 100) return values[values.length - 1];
757
- const index = p / 100 * (values.length - 1);
796
+ const sorted = values.slice().sort((a, b) => a - b);
797
+ if (p === 0) return sorted[0];
798
+ if (p === 100) return sorted[sorted.length - 1];
799
+ const index = p / 100 * (sorted.length - 1);
758
800
  const lower = Math.floor(index);
759
801
  const upper = Math.ceil(index);
760
802
  if (lower === upper) {
761
- return values[lower];
803
+ return sorted[lower];
762
804
  }
763
805
  const weight = index - lower;
764
- return values[lower] * (1 - weight) + values[upper] * weight;
806
+ return sorted[lower] * (1 - weight) + sorted[upper] * weight;
765
807
  }
766
808
  /**
767
809
  * Finds the most frequent value(s) in the dataset.
@@ -779,7 +821,7 @@ var iterflow = class _iterflow {
779
821
  * ```
780
822
  */
781
823
  mode() {
782
- const values = this.toArray();
824
+ const values = this._sourceArray || this.toArray();
783
825
  if (values.length === 0) return void 0;
784
826
  const frequency = /* @__PURE__ */ new Map();
785
827
  let maxFreq = 0;
@@ -812,20 +854,20 @@ var iterflow = class _iterflow {
812
854
  * ```
813
855
  */
814
856
  quartiles() {
815
- const values = this.toArray();
857
+ const values = this._sourceArray || this.toArray();
816
858
  if (values.length === 0) return void 0;
817
- values.sort((a, b) => a - b);
859
+ const sorted = values.slice().sort((a, b) => a - b);
818
860
  const calculatePercentile = (p) => {
819
- if (p === 0) return values[0];
820
- if (p === 100) return values[values.length - 1];
821
- const index = p / 100 * (values.length - 1);
861
+ if (p === 0) return sorted[0];
862
+ if (p === 100) return sorted[sorted.length - 1];
863
+ const index = p / 100 * (sorted.length - 1);
822
864
  const lower = Math.floor(index);
823
865
  const upper = Math.ceil(index);
824
866
  if (lower === upper) {
825
- return values[lower];
867
+ return sorted[lower];
826
868
  }
827
869
  const weight = index - lower;
828
- return values[lower] * (1 - weight) + values[upper] * weight;
870
+ return sorted[lower] * (1 - weight) + sorted[upper] * weight;
829
871
  };
830
872
  return {
831
873
  Q1: calculatePercentile(25),
@@ -897,7 +939,7 @@ var iterflow = class _iterflow {
897
939
  * ```
898
940
  */
899
941
  covariance(other) {
900
- const values1 = this.toArray();
942
+ const values1 = this._sourceArray || this.toArray();
901
943
  const values2 = Array.from(other);
902
944
  if (values1.length === 0 || values2.length === 0 || values1.length !== values2.length) {
903
945
  return void 0;
@@ -928,7 +970,7 @@ var iterflow = class _iterflow {
928
970
  * ```
929
971
  */
930
972
  correlation(other) {
931
- const values1 = this.toArray();
973
+ const values1 = this._sourceArray || this.toArray();
932
974
  const values2 = Array.from(other);
933
975
  if (values1.length === 0 || values2.length === 0 || values1.length !== values2.length) {
934
976
  return void 0;
@@ -1352,6 +1394,9 @@ var iterflow = class _iterflow {
1352
1394
  * ```
1353
1395
  */
1354
1396
  first(defaultValue) {
1397
+ if (this._sourceArray) {
1398
+ return this._sourceArray.length > 0 ? this._sourceArray[0] : defaultValue;
1399
+ }
1355
1400
  const result = this.source.next();
1356
1401
  return result.done ? defaultValue : result.value;
1357
1402
  }
@@ -1369,6 +1414,9 @@ var iterflow = class _iterflow {
1369
1414
  * ```
1370
1415
  */
1371
1416
  last(defaultValue) {
1417
+ if (this._sourceArray) {
1418
+ return this._sourceArray.length > 0 ? this._sourceArray[this._sourceArray.length - 1] : defaultValue;
1419
+ }
1372
1420
  let lastValue = defaultValue;
1373
1421
  let hasValue = false;
1374
1422
  for (const value of this) {
@@ -1391,6 +1439,9 @@ var iterflow = class _iterflow {
1391
1439
  * ```
1392
1440
  */
1393
1441
  nth(index) {
1442
+ if (this._sourceArray) {
1443
+ return index >= 0 && index < this._sourceArray.length ? this._sourceArray[index] : void 0;
1444
+ }
1394
1445
  if (index < 0) {
1395
1446
  return void 0;
1396
1447
  }
@@ -1415,6 +1466,9 @@ var iterflow = class _iterflow {
1415
1466
  * ```
1416
1467
  */
1417
1468
  isEmpty() {
1469
+ if (this._sourceArray) {
1470
+ return this._sourceArray.length === 0;
1471
+ }
1418
1472
  const result = this.source.next();
1419
1473
  return result.done === true;
1420
1474
  }
@@ -1433,6 +1487,9 @@ var iterflow = class _iterflow {
1433
1487
  * ```
1434
1488
  */
1435
1489
  includes(searchValue) {
1490
+ if (this._sourceArray) {
1491
+ return this._sourceArray.includes(searchValue);
1492
+ }
1436
1493
  for (const value of this) {
1437
1494
  if (value === searchValue) {
1438
1495
  return true;
@@ -3375,12 +3432,12 @@ var DebugState = class {
3375
3432
  /**
3376
3433
  * Enable debug mode with optional configuration
3377
3434
  */
3378
- enable(config2) {
3435
+ enable(config) {
3379
3436
  this.config = {
3380
3437
  ...this.config,
3381
3438
  enabled: true,
3382
3439
  traceOperations: true,
3383
- ...config2
3440
+ ...config
3384
3441
  };
3385
3442
  if (this.config.logToConsole) {
3386
3443
  console.log("[iterflow Debug] Debug mode enabled", this.config);
@@ -3500,8 +3557,8 @@ var DebugState = class {
3500
3557
  }
3501
3558
  };
3502
3559
  var debugState = new DebugState();
3503
- function enableDebug(config2) {
3504
- debugState.enable(config2);
3560
+ function enableDebug(config) {
3561
+ debugState.enable(config);
3505
3562
  }
3506
3563
  function disableDebug() {
3507
3564
  debugState.disable();
@@ -3734,85 +3791,6 @@ function errorBoundary(fn, options = {}) {
3734
3791
  };
3735
3792
  }
3736
3793
 
3737
- // src/deprecation.ts
3738
- var config = {
3739
- enabled: typeof process !== "undefined" && process.env.NODE_ENV !== "production",
3740
- showStackTrace: false
3741
- };
3742
- var warnedFeatures = /* @__PURE__ */ new Set();
3743
- function configureDeprecation(options) {
3744
- config = { ...config, ...options };
3745
- }
3746
- function getDeprecationConfig() {
3747
- return { ...config };
3748
- }
3749
- function clearDeprecationWarnings() {
3750
- warnedFeatures.clear();
3751
- }
3752
- function emitWarning(warning) {
3753
- if (!config.enabled) return;
3754
- if (warnedFeatures.has(warning.feature)) return;
3755
- warnedFeatures.add(warning.feature);
3756
- if (config.handler) {
3757
- config.handler(warning);
3758
- return;
3759
- }
3760
- let message = `[iterflow] DEPRECATED: ${warning.feature} has been deprecated since v${warning.since}`;
3761
- if (warning.removeIn) {
3762
- message += ` and will be removed in v${warning.removeIn}`;
3763
- }
3764
- if (warning.alternative) {
3765
- message += `
3766
- Please use ${warning.alternative} instead.`;
3767
- }
3768
- if (warning.message) {
3769
- message += `
3770
- ${warning.message}`;
3771
- }
3772
- if (typeof process !== "undefined" && process.emitWarning) {
3773
- const options = {
3774
- type: "DeprecationWarning",
3775
- code: "ITERFLOW_DEPRECATION",
3776
- detail: warning.message
3777
- };
3778
- process.emitWarning(message, options);
3779
- } else {
3780
- console.warn(message);
3781
- }
3782
- if (config.showStackTrace && warning.stack) {
3783
- console.warn("Stack trace:", warning.stack);
3784
- }
3785
- }
3786
- function deprecate(options) {
3787
- const warning = {
3788
- ...options,
3789
- stack: config.showStackTrace ? new Error().stack : void 0
3790
- };
3791
- emitWarning(warning);
3792
- }
3793
- function deprecated(options) {
3794
- return function(target, propertyKey, descriptor) {
3795
- const originalMethod = descriptor.value;
3796
- if (!originalMethod) return;
3797
- const className = target.constructor?.name || "Object";
3798
- const feature = `${className}.${propertyKey}`;
3799
- descriptor.value = function(...args) {
3800
- deprecate({ ...options, feature });
3801
- return originalMethod.apply(this, args);
3802
- };
3803
- return descriptor;
3804
- };
3805
- }
3806
- function deprecatedFunction(fn, options) {
3807
- return function(...args) {
3808
- deprecate(options);
3809
- return fn.apply(this, args);
3810
- };
3811
- }
3812
- function hasDeprecationWarning(feature) {
3813
- return warnedFeatures.has(feature);
3814
- }
3815
-
3816
3794
  // src/index.ts
3817
3795
  function iter(source) {
3818
3796
  return new iterflow(source);
@@ -3979,21 +3957,14 @@ exports.TypeConversionError = TypeConversionError;
3979
3957
  exports.ValidationError = ValidationError;
3980
3958
  exports.addTrace = addTrace;
3981
3959
  exports.asyncIter = asyncIter;
3982
- exports.clearDeprecationWarnings = clearDeprecationWarnings;
3983
3960
  exports.clearTraces = clearTraces;
3984
- exports.configureDeprecation = configureDeprecation;
3985
- exports.deprecate = deprecate;
3986
- exports.deprecated = deprecated;
3987
- exports.deprecatedFunction = deprecatedFunction;
3988
3961
  exports.disableDebug = disableDebug;
3989
3962
  exports.enableDebug = enableDebug;
3990
3963
  exports.errorBoundary = errorBoundary;
3991
3964
  exports.getDebugConfig = getDebugConfig;
3992
- exports.getDeprecationConfig = getDeprecationConfig;
3993
3965
  exports.getTraceSummary = getTraceSummary;
3994
3966
  exports.getTraces = getTraces;
3995
3967
  exports.getTracesForOperation = getTracesForOperation;
3996
- exports.hasDeprecationWarning = hasDeprecationWarning;
3997
3968
  exports.isDebugEnabled = isDebugEnabled;
3998
3969
  exports.iter = iter;
3999
3970
  exports.iterflow = iterflow;