@visulima/package 3.5.1 → 3.5.3

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.
@@ -423,52 +423,6 @@ type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] =
423
423
  ? T
424
424
  : BuildTuple<L, Fill, [...T, Fill]>;
425
425
 
426
- /**
427
- Returns the maximum value from a tuple of integers.
428
-
429
- Note:
430
- - Float numbers are not supported.
431
-
432
- @example
433
- ```
434
- ArrayMax<[1, 2, 5, 3]>;
435
- //=> 5
436
-
437
- ArrayMax<[1, 2, 5, 3, 99, -1]>;
438
- //=> 99
439
- ```
440
- */
441
- type TupleMax<A extends number[], Result extends number = NegativeInfinity> = number extends A[number]
442
- ? never :
443
- A extends [infer F extends number, ...infer R extends number[]]
444
- ? GreaterThan<F, Result> extends true
445
- ? TupleMax<R, F>
446
- : TupleMax<R, Result>
447
- : Result;
448
-
449
- /**
450
- Returns the minimum value from a tuple of integers.
451
-
452
- Note:
453
- - Float numbers are not supported.
454
-
455
- @example
456
- ```
457
- ArrayMin<[1, 2, 5, 3]>;
458
- //=> 1
459
-
460
- ArrayMin<[1, 2, 5, 3, -5]>;
461
- //=> -5
462
- ```
463
- */
464
- type TupleMin<A extends number[], Result extends number = PositiveInfinity> = number extends A[number]
465
- ? never
466
- : A extends [infer F extends number, ...infer R extends number[]]
467
- ? LessThan<F, Result> extends true
468
- ? TupleMin<R, F>
469
- : TupleMin<R, Result>
470
- : Result;
471
-
472
426
  /**
473
427
  Return a string representation of the given string or number.
474
428
 
@@ -663,85 +617,46 @@ type IsNumberLike<N> =
663
617
  : false;
664
618
 
665
619
  /**
666
- Matches any primitive, `void`, `Date`, or `RegExp` value.
667
- */
668
- type BuiltIns = Primitive | void | Date | RegExp;
669
-
670
- /**
671
- Matches non-recursive types.
672
- */
673
- type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown);
674
-
675
- /**
676
- Returns the sum of two numbers.
677
-
678
- Note:
679
- - A or B can only support `-999` ~ `999`.
680
- - A and B can only be small integers, less than 1000.
681
- - If the result is negative, you can only get `number`.
620
+ Returns the number with reversed sign.
682
621
 
683
622
  @example
684
623
  ```
685
- import type {Sum} from 'type-fest';
686
-
687
- Sum<111, 222>;
688
- //=> 333
689
-
690
- Sum<-111, 222>;
691
- //=> 111
624
+ ReverseSign<-1>;
625
+ //=> 1
692
626
 
693
- Sum<111, -222>;
694
- //=> number
627
+ ReverseSign<1>;
628
+ //=> -1
695
629
 
696
- Sum<PositiveInfinity, -9999>;
630
+ ReverseSign<NegativeInfinity>
697
631
  //=> PositiveInfinity
698
632
 
699
- Sum<PositiveInfinity, NegativeInfinity>;
700
- //=> number
633
+ ReverseSign<PositiveInfinity>
634
+ //=> NegativeInfinity
701
635
  ```
636
+ */
637
+ type ReverseSign<N extends number> =
638
+ // Handle edge cases
639
+ N extends 0 ? 0 : N extends PositiveInfinity ? NegativeInfinity : N extends NegativeInfinity ? PositiveInfinity :
640
+ // Handle negative numbers
641
+ `${N}` extends `-${infer P extends number}` ? P
642
+ // Handle positive numbers
643
+ : `-${N}` extends `${infer R extends number}` ? R : never;
702
644
 
703
- @category Numeric
645
+ /**
646
+ Matches any primitive, `void`, `Date`, or `RegExp` value.
704
647
  */
705
- // TODO: Support big integer and negative number.
706
- type Sum<A extends number, B extends number> = number extends A | B
707
- ? number
708
- : [
709
- IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
710
- IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
711
- ] extends infer R extends [boolean, boolean, boolean, boolean]
712
- ? Or<
713
- And<IsEqual<R[0], true>, IsEqual<R[3], false>>,
714
- And<IsEqual<R[2], true>, IsEqual<R[1], false>>
715
- > extends true
716
- ? PositiveInfinity
717
- : Or<
718
- And<IsEqual<R[1], true>, IsEqual<R[2], false>>,
719
- And<IsEqual<R[3], true>, IsEqual<R[0], false>>
720
- > extends true
721
- ? NegativeInfinity
722
- : true extends R[number]
723
- ? number
724
- : ([IsNegative<A>, IsNegative<B>] extends infer R
725
- ? [false, false] extends R
726
- ? [...BuildTuple<A>, ...BuildTuple<B>]['length']
727
- : [true, true] extends R
728
- ? number
729
- : TupleMax<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Max_
730
- ? TupleMin<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Min_ extends number
731
- ? Max_ extends A | B
732
- ? Subtract<Max_, Min_>
733
- : number
734
- : never
735
- : never
736
- : never) & number
737
- : never;
648
+ type BuiltIns = Primitive | void | Date | RegExp;
649
+
650
+ /**
651
+ Matches non-recursive types.
652
+ */
653
+ type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown);
738
654
 
739
655
  /**
740
656
  Returns the difference between two numbers.
741
657
 
742
658
  Note:
743
659
  - A or B can only support `-999` ~ `999`.
744
- - If the result is negative, you can only get `number`.
745
660
 
746
661
  @example
747
662
  ```
@@ -754,7 +669,10 @@ Subtract<111, -222>;
754
669
  //=> 333
755
670
 
756
671
  Subtract<-111, 222>;
757
- //=> number
672
+ //=> -333
673
+
674
+ Subtract<18, 96>;
675
+ //=> -78
758
676
 
759
677
  Subtract<PositiveInfinity, 9999>;
760
678
  //=> PositiveInfinity
@@ -765,38 +683,53 @@ Subtract<PositiveInfinity, PositiveInfinity>;
765
683
 
766
684
  @category Numeric
767
685
  */
768
- // TODO: Support big integer and negative number.
769
- type Subtract<A extends number, B extends number> = number extends A | B
770
- ? number
771
- : [
772
- IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
773
- IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
774
- ] extends infer R extends [boolean, boolean, boolean, boolean]
775
- ? Or<
776
- And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
777
- And<IsEqual<R[3], true>, IsEqual<R[1], false>>
778
- > extends true
779
- ? PositiveInfinity
780
- : Or<
781
- And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
782
- And<IsEqual<R[2], true>, IsEqual<R[0], false>>
783
- > extends true
784
- ? NegativeInfinity
785
- : true extends R[number]
786
- ? number
787
- : [IsNegative<A>, IsNegative<B>] extends infer R
788
- ? [false, false] extends R
789
- ? BuildTuple<A> extends infer R
790
- ? R extends [...BuildTuple<B>, ...infer R]
791
- ? R['length']
792
- : number
793
- : never
794
- : LessThan<A, B> extends true
795
- ? number
796
- : [false, true] extends R
797
- ? Sum<A, NumberAbsolute<B>>
798
- : Subtract<NumberAbsolute<B>, NumberAbsolute<A>>
799
- : never
686
+ // TODO: Support big integer.
687
+ type Subtract<A extends number, B extends number> =
688
+ // Handle cases when A or B is the actual "number" type
689
+ number extends A | B ? number
690
+ // Handle cases when A and B are both +/- infinity
691
+ : A extends B & (PositiveInfinity | NegativeInfinity) ? number
692
+ // Handle cases when A is - infinity or B is + infinity
693
+ : A extends NegativeInfinity ? NegativeInfinity : B extends PositiveInfinity ? NegativeInfinity
694
+ // Handle cases when A is + infinity or B is - infinity
695
+ : A extends PositiveInfinity ? PositiveInfinity : B extends NegativeInfinity ? PositiveInfinity
696
+ // Handle case when numbers are equal to each other
697
+ : A extends B ? 0
698
+ // Handle cases when A or B is 0
699
+ : A extends 0 ? ReverseSign<B> : B extends 0 ? A
700
+ // Handle remaining regular cases
701
+ : SubtractPostChecks<A, B>;
702
+
703
+ /**
704
+ Subtracts two numbers A and B, such that they are not equal and neither of them are 0, +/- infinity or the `number` type
705
+ */
706
+ type SubtractPostChecks<A extends number, B extends number, AreNegative = [IsNegative<A>, IsNegative<B>]> =
707
+ AreNegative extends [false, false]
708
+ ? SubtractPositives<A, B>
709
+ : AreNegative extends [true, true]
710
+ // When both numbers are negative we subtract the absolute values and then reverse the sign
711
+ ? ReverseSign<SubtractPositives<NumberAbsolute<A>, NumberAbsolute<B>>>
712
+ // When the signs are different we can add the absolute values and then reverse the sign if A < B
713
+ : [...BuildTuple<NumberAbsolute<A>>, ...BuildTuple<NumberAbsolute<B>>] extends infer R extends unknown[]
714
+ ? LessThan<A, B> extends true ? ReverseSign<R['length']> : R['length']
715
+ : never;
716
+
717
+ /**
718
+ Subtracts two positive numbers.
719
+ */
720
+ type SubtractPositives<A extends number, B extends number> =
721
+ LessThan<A, B> extends true
722
+ // When A < B we can reverse the result of B - A
723
+ ? ReverseSign<SubtractIfAGreaterThanB<B, A>>
724
+ : SubtractIfAGreaterThanB<A, B>;
725
+
726
+ /**
727
+ Subtracts two positive numbers A and B such that A > B.
728
+ */
729
+ type SubtractIfAGreaterThanB<A extends number, B extends number> =
730
+ // This is where we always want to end up and do the actual subtraction
731
+ BuildTuple<A> extends [...BuildTuple<B>, ...infer R]
732
+ ? R['length']
800
733
  : never;
801
734
 
802
735
  /**
@@ -844,11 +777,89 @@ type PathsOptions = {
844
777
  ```
845
778
  */
846
779
  bracketNotation?: boolean;
780
+
781
+ /**
782
+ Only include leaf paths in the output.
783
+
784
+ @default false
785
+
786
+ @example
787
+ ```
788
+ type Post = {
789
+ id: number;
790
+ author: {
791
+ id: number;
792
+ name: {
793
+ first: string;
794
+ last: string;
795
+ };
796
+ };
797
+ };
798
+
799
+ type AllPaths = Paths<Post, {leavesOnly: false}>;
800
+ //=> 'id' | 'author' | 'author.id' | 'author.name' | 'author.name.first' | 'author.name.last'
801
+
802
+ type LeafPaths = Paths<Post, {leavesOnly: true}>;
803
+ //=> 'id' | 'author.id' | 'author.name.first' | 'author.name.last'
804
+ ```
805
+
806
+ @example
807
+ ```
808
+ type ArrayExample = {
809
+ array: Array<{foo: string}>;
810
+ tuple: [string, {bar: string}];
811
+ };
812
+
813
+ type AllPaths = Paths<ArrayExample, {leavesOnly: false}>;
814
+ //=> 'array' | `array.${number}` | `array.${number}.foo` | 'tuple' | 'tuple.0' | 'tuple.1' | 'tuple.1.bar'
815
+
816
+ type LeafPaths = Paths<ArrayExample, {leavesOnly: true}>;
817
+ //=> `array.${number}.foo` | 'tuple.0' | 'tuple.1.bar'
818
+ ```
819
+ */
820
+ leavesOnly?: boolean;
821
+
822
+ /**
823
+ Only include paths at the specified depth. By default all paths up to {@link PathsOptions.maxRecursionDepth | `maxRecursionDepth`} are included.
824
+
825
+ Note: Depth starts at `0` for root properties.
826
+
827
+ @default number
828
+
829
+ @example
830
+ ```
831
+ type Post = {
832
+ id: number;
833
+ author: {
834
+ id: number;
835
+ name: {
836
+ first: string;
837
+ last: string;
838
+ };
839
+ };
840
+ };
841
+
842
+ type DepthZero = Paths<Post, {depth: 0}>;
843
+ //=> 'id' | 'author'
844
+
845
+ type DepthOne = Paths<Post, {depth: 1}>;
846
+ //=> 'author.id' | 'author.name'
847
+
848
+ type DepthTwo = Paths<Post, {depth: 2}>;
849
+ //=> 'author.name.first' | 'author.name.last'
850
+
851
+ type LeavesAtDepthOne = Paths<Post, {leavesOnly: true; depth: 1}>;
852
+ //=> 'author.id'
853
+ ```
854
+ */
855
+ depth?: number;
847
856
  };
848
857
 
849
858
  type DefaultPathsOptions = {
850
859
  maxRecursionDepth: 10;
851
860
  bracketNotation: false;
861
+ leavesOnly: false;
862
+ depth: number;
852
863
  };
853
864
 
854
865
  /**
@@ -897,6 +908,10 @@ type Paths<T, Options extends PathsOptions = {}> = _Paths<T, {
897
908
  maxRecursionDepth: Options['maxRecursionDepth'] extends number ? Options['maxRecursionDepth'] : DefaultPathsOptions['maxRecursionDepth'];
898
909
  // Set default bracketNotation to false
899
910
  bracketNotation: Options['bracketNotation'] extends boolean ? Options['bracketNotation'] : DefaultPathsOptions['bracketNotation'];
911
+ // Set default leavesOnly to false
912
+ leavesOnly: Options['leavesOnly'] extends boolean ? Options['leavesOnly'] : DefaultPathsOptions['leavesOnly'];
913
+ // Set default depth to number
914
+ depth: Options['depth'] extends number ? Options['depth'] : DefaultPathsOptions['depth'];
900
915
  }>;
901
916
 
902
917
  type _Paths<T, Options extends Required<PathsOptions>> =
@@ -936,11 +951,30 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
936
951
  ) extends infer TranformedKey extends string | number ?
937
952
  // 1. If style is 'a[0].b' and 'Key' is a numberlike value like 3 or '3', transform 'Key' to `[${Key}]`, else to `${Key}` | Key
938
953
  // 2. If style is 'a.0.b', transform 'Key' to `${Key}` | Key
939
- | TranformedKey
954
+ | ((Options['leavesOnly'] extends true
955
+ ? MaxDepth extends 0
956
+ ? TranformedKey
957
+ : T[Key] extends EmptyObject | readonly [] | NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
958
+ ? TranformedKey
959
+ : never
960
+ : TranformedKey
961
+ ) extends infer _TransformedKey
962
+ // If `depth` is provided, the condition becomes truthy only when it reaches `0`.
963
+ // Otherwise, since `depth` defaults to `number`, the condition is always truthy, returning paths at all depths.
964
+ ? 0 extends Options['depth']
965
+ ? _TransformedKey
966
+ : never
967
+ : never)
940
968
  | (
941
969
  // Recursively generate paths for the current key
942
970
  GreaterThan<MaxDepth, 0> extends true // Limit the depth to prevent infinite recursion
943
- ? _Paths<T[Key], {bracketNotation: Options['bracketNotation']; maxRecursionDepth: Subtract<MaxDepth, 1>}> extends infer SubPath
971
+ ? _Paths<T[Key],
972
+ {
973
+ bracketNotation: Options['bracketNotation'];
974
+ maxRecursionDepth: Subtract<MaxDepth, 1>;
975
+ leavesOnly: Options['leavesOnly'];
976
+ depth: Subtract<Options['depth'], 1>;
977
+ }> extends infer SubPath
944
978
  ? SubPath extends string | number
945
979
  ? (
946
980
  Options['bracketNotation'] extends true
@@ -1860,10 +1894,10 @@ type NormalizedReadResult = {
1860
1894
  };
1861
1895
  declare const findPackageJson: (cwd?: URL | string, options?: ReadOptions) => Promise<NormalizedReadResult>;
1862
1896
  declare const findPackageJsonSync: (cwd?: URL | string, options?: ReadOptions) => NormalizedReadResult;
1863
- declare const writePackageJson: <T = PackageJson$1>(data: T, options?: WriteJsonOptions & {
1897
+ declare const writePackageJson: <T = PackageJson>(data: T, options?: WriteJsonOptions & {
1864
1898
  cwd?: URL | string;
1865
1899
  }) => Promise<void>;
1866
- declare const writePackageJsonSync: <T = PackageJson$1>(data: T, options?: WriteJsonOptions & {
1900
+ declare const writePackageJsonSync: <T = PackageJson>(data: T, options?: WriteJsonOptions & {
1867
1901
  cwd?: URL | string;
1868
1902
  }) => void;
1869
1903
  declare const parsePackageJson: (packageFile: JsonObject | string, options?: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@visulima/package",
3
- "version": "3.5.1",
3
+ "version": "3.5.3",
4
4
  "description": "One Package to rule them all, finds your root-dir, monorepo, or package manager.",
5
5
  "keywords": [
6
6
  "anolilab",
@@ -146,9 +146,9 @@
146
146
  ],
147
147
  "dependencies": {
148
148
  "@antfu/install-pkg": "^1.0.0",
149
- "@inquirer/confirm": "^5.1.3",
150
- "@visulima/fs": "3.1.0",
151
- "@visulima/path": "1.3.4",
149
+ "@inquirer/confirm": "^5.1.6",
150
+ "@visulima/fs": "3.1.2",
151
+ "@visulima/path": "1.3.5",
152
152
  "normalize-package-data": "^7.0.0"
153
153
  },
154
154
  "engines": {