iterable-linq-utility 0.10.0 → 0.11.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.d.cts CHANGED
@@ -319,10 +319,12 @@ declare function every<T>(iterable: Iterable<T>, predicate: Predicate<T>): boole
319
319
 
320
320
  /**
321
321
  * Adds a method to every chain, including the chains created before the call.
322
- * Declare the method first by augmenting `IIterableLinq`, then register it once, at application start-up.
323
- * @param name - the method name; it must not exist yet (library methods, earlier extensions, `Object.prototype` members)
322
+ * Declare the method first by augmenting `IIterableLinq`, then register it at application start-up.
323
+ * Registering a name added by an earlier `extend` again replaces its implementation, so a module that runs twice
324
+ * (hot module replacement, a test runner that re-imports it) does not throw: the last registration wins.
325
+ * @param name - the method name; a new name or one added by an earlier `extend`, not a library method or an `Object.prototype` member
324
326
  * @param implementation - the method; `this` is the chain, typed `IIterableLinq<unknown>`
325
- * @throws Error if `name` already exists (use `override` to replace it), is empty, or `implementation` is not a function
327
+ * @throws Error if `name` is a library method (use `override` to replace it), an `Object.prototype` member or a name used by the chain instances, is empty, or `implementation` is not a function
326
328
  * @example
327
329
  * ```ts
328
330
  * declare module 'iterable-linq-utility' {
@@ -564,6 +566,47 @@ declare function forEachAsync<T>(iterable: Iterable<T>, action: AsyncAction<T>):
564
566
  */
565
567
  export declare function from<T>(iterable: Iterable<T>): IIterableLinq<T>;
566
568
 
569
+ /**
570
+ * Starts a chain over the properties of `object`: its entries, its keys, its values or its property descriptors.
571
+ * With the default options it yields what `Object.entries` returns: the own, enumerable, string keys.
572
+ * - `options.yield`: `'entries'` (default) `[key, value]`, `'keys'`, `'values'`, or `'descriptors'` `[key, descriptor, owner]` without calling the getters.
573
+ * - `options.inherited` also reads the prototype chain, up to `Object.prototype` excluded; a key is yielded once, from the nearest object that has it, like `for…in`.
574
+ * - `options.nonEnumerable` also reads the non-enumerable properties, `options.symbols` the symbol keys.
575
+ * - Each run of the chain reads the object again: the keys of an object when the iteration reaches it, a value when it is yielded.
576
+ * @param object - the object to read
577
+ * @param options - `yield`, `inherited`, `nonEnumerable` and `symbols`
578
+ * @returns a chain of the properties of `object`
579
+ * @throws Error if `object` is not an object or a function, `options` is not an object, `yield` is not one of its values, or a flag is not a boolean
580
+ * @example
581
+ * ```ts
582
+ * IterableLinq.fromObject({ a: 1, b: 2 }).collectToArray(); // [['a', 1], ['b', 2]]
583
+ * IterableLinq.fromObject({ a: 1, b: 2 }, { yield: 'keys' }).collectToArray(); // ['a', 'b']
584
+ * IterableLinq.fromObject({ a: 1, b: 2 }, { yield: 'values' }).collectToArray(); // [1, 2]
585
+ * ```
586
+ * @since 0.11.0
587
+ */
588
+ export declare function fromObject<O extends object, const Options extends IObjectOptions = IObjectDefaultOptions>(object: O, options?: Options): IIterableLinq<ObjectItem<O, Options>>;
589
+
590
+ /**
591
+ * Returns the properties of `object`: its entries, its keys, its values or its property descriptors.
592
+ * With the default options it yields what `Object.entries` returns: the own, enumerable, string keys.
593
+ * - `inherited` also reads the prototype chain, up to `Object.prototype` excluded; a key is yielded once, from the nearest object that has it, like `for…in`.
594
+ * - `nonEnumerable` also reads the non-enumerable properties, `symbols` the symbol keys.
595
+ * - The keys of an object are read when the iteration reaches it, a value when it is yielded: each run reads the object again.
596
+ * @operation `Transformation`
597
+ * @param object - the object to read
598
+ * @param options - `yield` (`'entries'`, `'keys'`, `'values'` or `'descriptors'`), `inherited`, `nonEnumerable` and `symbols`
599
+ * @returns a lazy, re-runnable `Iterable` of the properties of `object`
600
+ * @throws Error if `object` is not an object or a function, `options` is not an object, `yield` is not one of its values, or a flag is not a boolean
601
+ * @example
602
+ * ```ts
603
+ * Array.from(Functions.fromObject({ a: 1, b: 2 })); // [['a', 1], ['b', 2]]
604
+ * Array.from(Functions.fromObject({ a: 1, b: 2 }, { yield: 'keys' })); // ['a', 'b']
605
+ * ```
606
+ * @since 0.11.0
607
+ */
608
+ declare function fromObject_2<O extends object, const Options extends IObjectOptions = IObjectDefaultOptions>(object: O, options?: Options): Iterable<ObjectItem<O, Options>>;
609
+
567
610
  /**
568
611
  * Starts a chain of numbers from 0 up to, but not including, `end`.
569
612
  * - `options.step` is the distance between two values (default 1); its sign is ignored, the direction comes from the sign of `end`.
@@ -630,6 +673,7 @@ declare namespace Functions {
630
673
  flatMap,
631
674
  forEach,
632
675
  forEachAsync,
676
+ fromObject_2 as fromObject,
633
677
  includes,
634
678
  indexOf,
635
679
  join,
@@ -1661,6 +1705,32 @@ declare function includes<T>(iterable: Iterable<T>, value: T): boolean;
1661
1705
  */
1662
1706
  declare function indexOf<T>(iterable: Iterable<T>, value: T): number;
1663
1707
 
1708
+ /**
1709
+ * The options of `fromObject` when none are given: the type of the values follows `Object.entries`.
1710
+ * @since 0.11.0
1711
+ */
1712
+ export declare interface IObjectDefaultOptions extends IObjectOptions {
1713
+ yield?: 'entries';
1714
+ inherited?: false;
1715
+ nonEnumerable?: false;
1716
+ symbols?: false;
1717
+ }
1718
+
1719
+ /**
1720
+ * Options of `fromObject`. The defaults read what `Object.keys` reads: the own, enumerable, string keys.
1721
+ * @since 0.11.0
1722
+ */
1723
+ export declare interface IObjectOptions {
1724
+ /** `'entries'` (default) yields `[key, value]`, `'keys'` the keys, `'values'` the values, `'descriptors'` `[key, descriptor, owner]` without calling the getters. */
1725
+ yield?: ObjectYield;
1726
+ /** Also reads the properties of the prototype chain, up to `Object.prototype` excluded; defaults to `false`. */
1727
+ inherited?: boolean;
1728
+ /** Also reads the non-enumerable properties; defaults to `false`. */
1729
+ nonEnumerable?: boolean;
1730
+ /** Also reads the symbol keys; defaults to `false`. */
1731
+ symbols?: boolean;
1732
+ }
1733
+
1664
1734
  /**
1665
1735
  * Options of `range`.
1666
1736
  * @since 0.1.0
@@ -1811,6 +1881,39 @@ declare function memoize<T>(iterable: Iterable<T>, options?: IMemoizeOptions): I
1811
1881
  */
1812
1882
  declare function min<T>(iterable: Iterable<T>, comparer?: Comparer<T>): T | undefined;
1813
1883
 
1884
+ /**
1885
+ * The type of the values of `fromObject(object, options)`, chosen by `options.yield` (default `'entries'`).
1886
+ * @since 0.11.0
1887
+ */
1888
+ export declare type ObjectItem<O, Options extends IObjectOptions> = ObjectItemOf<O, Options, Options extends {
1889
+ yield: infer Y;
1890
+ } ? Y : Options extends {
1891
+ yield?: infer Y;
1892
+ } ? Y | undefined : undefined>;
1893
+
1894
+ /** Distributes over `Y`, so a `yield` typed as a union gives the union of the items; `undefined` gives the entries. */
1895
+ declare type ObjectItemOf<O, Options extends IObjectOptions, Y> = Y extends 'keys' ? ObjectKey<O, Options> : Y extends 'values' ? ObjectValue<O, Options> : Y extends 'descriptors' ? [ObjectKey<O, Options>, PropertyDescriptor, object] : [ObjectKey<O, Options>, ObjectValue<O, Options>];
1896
+
1897
+ /**
1898
+ * The type of the keys `fromObject` yields: the keys of `O` as strings, as they are at runtime,
1899
+ * or `string` (and `symbol`) when the options read keys that `O` does not declare.
1900
+ * @since 0.11.0
1901
+ */
1902
+ export declare type ObjectKey<O, Options extends IObjectOptions> = ReadsWide<Options> extends true ? string | (ReadsSymbols<Options> extends true ? symbol : never) : `${Extract<keyof O, string | number>}` | (ReadsSymbols<Options> extends true ? Extract<keyof O, symbol> : never);
1903
+
1904
+ /**
1905
+ * The type of the values `fromObject` yields: the values of the keys of `O` it reads, or `unknown`
1906
+ * when the options read keys that `O` does not declare.
1907
+ * @since 0.11.0
1908
+ */
1909
+ export declare type ObjectValue<O, Options extends IObjectOptions> = ReadsWide<Options> extends true ? unknown : O[Extract<keyof O, string | number> | (ReadsSymbols<Options> extends true ? Extract<keyof O, symbol> : never)];
1910
+
1911
+ /**
1912
+ * What `fromObject` yields for each property.
1913
+ * @since 0.11.0
1914
+ */
1915
+ export declare type ObjectYield = 'entries' | 'keys' | 'values' | 'descriptors';
1916
+
1814
1917
  /**
1815
1918
  * Replaces an existing method of every chain: a library method or one added with `extend`.
1816
1919
  * Typical use: a library release adds a method with the same name as one of your extensions,
@@ -1890,6 +1993,21 @@ declare function range(end: number, options?: IRangeOptions): Iterable<number>;
1890
1993
  */
1891
1994
  declare function range(start: number, end: number, options?: IRangeOptions): Iterable<number>;
1892
1995
 
1996
+ declare type ReadsSymbols<Options extends IObjectOptions> = Options extends {
1997
+ yield?: unknown;
1998
+ inherited?: unknown;
1999
+ nonEnumerable?: unknown;
2000
+ symbols?: false;
2001
+ } ? false : true;
2002
+
2003
+ /** `true` when the options may read keys that are not in `keyof O`: inherited or non-enumerable ones. */
2004
+ declare type ReadsWide<Options extends IObjectOptions> = Options extends {
2005
+ yield?: unknown;
2006
+ inherited?: false;
2007
+ nonEnumerable?: false;
2008
+ symbols?: unknown;
2009
+ } ? false : true;
2010
+
1893
2011
  /**
1894
2012
  * Accumulates the values of `iterable` into a single result, starting from the first value.
1895
2013
  * @operation `Action`
package/dist/index.d.ts CHANGED
@@ -319,10 +319,12 @@ declare function every<T>(iterable: Iterable<T>, predicate: Predicate<T>): boole
319
319
 
320
320
  /**
321
321
  * Adds a method to every chain, including the chains created before the call.
322
- * Declare the method first by augmenting `IIterableLinq`, then register it once, at application start-up.
323
- * @param name - the method name; it must not exist yet (library methods, earlier extensions, `Object.prototype` members)
322
+ * Declare the method first by augmenting `IIterableLinq`, then register it at application start-up.
323
+ * Registering a name added by an earlier `extend` again replaces its implementation, so a module that runs twice
324
+ * (hot module replacement, a test runner that re-imports it) does not throw: the last registration wins.
325
+ * @param name - the method name; a new name or one added by an earlier `extend`, not a library method or an `Object.prototype` member
324
326
  * @param implementation - the method; `this` is the chain, typed `IIterableLinq<unknown>`
325
- * @throws Error if `name` already exists (use `override` to replace it), is empty, or `implementation` is not a function
327
+ * @throws Error if `name` is a library method (use `override` to replace it), an `Object.prototype` member or a name used by the chain instances, is empty, or `implementation` is not a function
326
328
  * @example
327
329
  * ```ts
328
330
  * declare module 'iterable-linq-utility' {
@@ -564,6 +566,47 @@ declare function forEachAsync<T>(iterable: Iterable<T>, action: AsyncAction<T>):
564
566
  */
565
567
  export declare function from<T>(iterable: Iterable<T>): IIterableLinq<T>;
566
568
 
569
+ /**
570
+ * Starts a chain over the properties of `object`: its entries, its keys, its values or its property descriptors.
571
+ * With the default options it yields what `Object.entries` returns: the own, enumerable, string keys.
572
+ * - `options.yield`: `'entries'` (default) `[key, value]`, `'keys'`, `'values'`, or `'descriptors'` `[key, descriptor, owner]` without calling the getters.
573
+ * - `options.inherited` also reads the prototype chain, up to `Object.prototype` excluded; a key is yielded once, from the nearest object that has it, like `for…in`.
574
+ * - `options.nonEnumerable` also reads the non-enumerable properties, `options.symbols` the symbol keys.
575
+ * - Each run of the chain reads the object again: the keys of an object when the iteration reaches it, a value when it is yielded.
576
+ * @param object - the object to read
577
+ * @param options - `yield`, `inherited`, `nonEnumerable` and `symbols`
578
+ * @returns a chain of the properties of `object`
579
+ * @throws Error if `object` is not an object or a function, `options` is not an object, `yield` is not one of its values, or a flag is not a boolean
580
+ * @example
581
+ * ```ts
582
+ * IterableLinq.fromObject({ a: 1, b: 2 }).collectToArray(); // [['a', 1], ['b', 2]]
583
+ * IterableLinq.fromObject({ a: 1, b: 2 }, { yield: 'keys' }).collectToArray(); // ['a', 'b']
584
+ * IterableLinq.fromObject({ a: 1, b: 2 }, { yield: 'values' }).collectToArray(); // [1, 2]
585
+ * ```
586
+ * @since 0.11.0
587
+ */
588
+ export declare function fromObject<O extends object, const Options extends IObjectOptions = IObjectDefaultOptions>(object: O, options?: Options): IIterableLinq<ObjectItem<O, Options>>;
589
+
590
+ /**
591
+ * Returns the properties of `object`: its entries, its keys, its values or its property descriptors.
592
+ * With the default options it yields what `Object.entries` returns: the own, enumerable, string keys.
593
+ * - `inherited` also reads the prototype chain, up to `Object.prototype` excluded; a key is yielded once, from the nearest object that has it, like `for…in`.
594
+ * - `nonEnumerable` also reads the non-enumerable properties, `symbols` the symbol keys.
595
+ * - The keys of an object are read when the iteration reaches it, a value when it is yielded: each run reads the object again.
596
+ * @operation `Transformation`
597
+ * @param object - the object to read
598
+ * @param options - `yield` (`'entries'`, `'keys'`, `'values'` or `'descriptors'`), `inherited`, `nonEnumerable` and `symbols`
599
+ * @returns a lazy, re-runnable `Iterable` of the properties of `object`
600
+ * @throws Error if `object` is not an object or a function, `options` is not an object, `yield` is not one of its values, or a flag is not a boolean
601
+ * @example
602
+ * ```ts
603
+ * Array.from(Functions.fromObject({ a: 1, b: 2 })); // [['a', 1], ['b', 2]]
604
+ * Array.from(Functions.fromObject({ a: 1, b: 2 }, { yield: 'keys' })); // ['a', 'b']
605
+ * ```
606
+ * @since 0.11.0
607
+ */
608
+ declare function fromObject_2<O extends object, const Options extends IObjectOptions = IObjectDefaultOptions>(object: O, options?: Options): Iterable<ObjectItem<O, Options>>;
609
+
567
610
  /**
568
611
  * Starts a chain of numbers from 0 up to, but not including, `end`.
569
612
  * - `options.step` is the distance between two values (default 1); its sign is ignored, the direction comes from the sign of `end`.
@@ -630,6 +673,7 @@ declare namespace Functions {
630
673
  flatMap,
631
674
  forEach,
632
675
  forEachAsync,
676
+ fromObject_2 as fromObject,
633
677
  includes,
634
678
  indexOf,
635
679
  join,
@@ -1661,6 +1705,32 @@ declare function includes<T>(iterable: Iterable<T>, value: T): boolean;
1661
1705
  */
1662
1706
  declare function indexOf<T>(iterable: Iterable<T>, value: T): number;
1663
1707
 
1708
+ /**
1709
+ * The options of `fromObject` when none are given: the type of the values follows `Object.entries`.
1710
+ * @since 0.11.0
1711
+ */
1712
+ export declare interface IObjectDefaultOptions extends IObjectOptions {
1713
+ yield?: 'entries';
1714
+ inherited?: false;
1715
+ nonEnumerable?: false;
1716
+ symbols?: false;
1717
+ }
1718
+
1719
+ /**
1720
+ * Options of `fromObject`. The defaults read what `Object.keys` reads: the own, enumerable, string keys.
1721
+ * @since 0.11.0
1722
+ */
1723
+ export declare interface IObjectOptions {
1724
+ /** `'entries'` (default) yields `[key, value]`, `'keys'` the keys, `'values'` the values, `'descriptors'` `[key, descriptor, owner]` without calling the getters. */
1725
+ yield?: ObjectYield;
1726
+ /** Also reads the properties of the prototype chain, up to `Object.prototype` excluded; defaults to `false`. */
1727
+ inherited?: boolean;
1728
+ /** Also reads the non-enumerable properties; defaults to `false`. */
1729
+ nonEnumerable?: boolean;
1730
+ /** Also reads the symbol keys; defaults to `false`. */
1731
+ symbols?: boolean;
1732
+ }
1733
+
1664
1734
  /**
1665
1735
  * Options of `range`.
1666
1736
  * @since 0.1.0
@@ -1811,6 +1881,39 @@ declare function memoize<T>(iterable: Iterable<T>, options?: IMemoizeOptions): I
1811
1881
  */
1812
1882
  declare function min<T>(iterable: Iterable<T>, comparer?: Comparer<T>): T | undefined;
1813
1883
 
1884
+ /**
1885
+ * The type of the values of `fromObject(object, options)`, chosen by `options.yield` (default `'entries'`).
1886
+ * @since 0.11.0
1887
+ */
1888
+ export declare type ObjectItem<O, Options extends IObjectOptions> = ObjectItemOf<O, Options, Options extends {
1889
+ yield: infer Y;
1890
+ } ? Y : Options extends {
1891
+ yield?: infer Y;
1892
+ } ? Y | undefined : undefined>;
1893
+
1894
+ /** Distributes over `Y`, so a `yield` typed as a union gives the union of the items; `undefined` gives the entries. */
1895
+ declare type ObjectItemOf<O, Options extends IObjectOptions, Y> = Y extends 'keys' ? ObjectKey<O, Options> : Y extends 'values' ? ObjectValue<O, Options> : Y extends 'descriptors' ? [ObjectKey<O, Options>, PropertyDescriptor, object] : [ObjectKey<O, Options>, ObjectValue<O, Options>];
1896
+
1897
+ /**
1898
+ * The type of the keys `fromObject` yields: the keys of `O` as strings, as they are at runtime,
1899
+ * or `string` (and `symbol`) when the options read keys that `O` does not declare.
1900
+ * @since 0.11.0
1901
+ */
1902
+ export declare type ObjectKey<O, Options extends IObjectOptions> = ReadsWide<Options> extends true ? string | (ReadsSymbols<Options> extends true ? symbol : never) : `${Extract<keyof O, string | number>}` | (ReadsSymbols<Options> extends true ? Extract<keyof O, symbol> : never);
1903
+
1904
+ /**
1905
+ * The type of the values `fromObject` yields: the values of the keys of `O` it reads, or `unknown`
1906
+ * when the options read keys that `O` does not declare.
1907
+ * @since 0.11.0
1908
+ */
1909
+ export declare type ObjectValue<O, Options extends IObjectOptions> = ReadsWide<Options> extends true ? unknown : O[Extract<keyof O, string | number> | (ReadsSymbols<Options> extends true ? Extract<keyof O, symbol> : never)];
1910
+
1911
+ /**
1912
+ * What `fromObject` yields for each property.
1913
+ * @since 0.11.0
1914
+ */
1915
+ export declare type ObjectYield = 'entries' | 'keys' | 'values' | 'descriptors';
1916
+
1814
1917
  /**
1815
1918
  * Replaces an existing method of every chain: a library method or one added with `extend`.
1816
1919
  * Typical use: a library release adds a method with the same name as one of your extensions,
@@ -1890,6 +1993,21 @@ declare function range(end: number, options?: IRangeOptions): Iterable<number>;
1890
1993
  */
1891
1994
  declare function range(start: number, end: number, options?: IRangeOptions): Iterable<number>;
1892
1995
 
1996
+ declare type ReadsSymbols<Options extends IObjectOptions> = Options extends {
1997
+ yield?: unknown;
1998
+ inherited?: unknown;
1999
+ nonEnumerable?: unknown;
2000
+ symbols?: false;
2001
+ } ? false : true;
2002
+
2003
+ /** `true` when the options may read keys that are not in `keyof O`: inherited or non-enumerable ones. */
2004
+ declare type ReadsWide<Options extends IObjectOptions> = Options extends {
2005
+ yield?: unknown;
2006
+ inherited?: false;
2007
+ nonEnumerable?: false;
2008
+ symbols?: unknown;
2009
+ } ? false : true;
2010
+
1893
2011
  /**
1894
2012
  * Accumulates the values of `iterable` into a single result, starting from the first value.
1895
2013
  * @operation `Action`