atomic-fns 0.4.2 → 1.1.1

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.
Files changed (39) hide show
  1. package/README.md +6 -17
  2. package/dist/collections/BTree.d.ts +1 -1
  3. package/dist/collections/Trie.d.ts +42 -0
  4. package/dist/collections/Trie.js +104 -0
  5. package/dist/collections/deque.d.ts +2 -2
  6. package/dist/collections/deque.js +6 -8
  7. package/dist/collections/index.d.ts +208 -106
  8. package/dist/collections/index.js +179 -436
  9. package/dist/decimal/index.d.ts +147 -0
  10. package/dist/decimal/index.js +376 -0
  11. package/dist/functools/index.d.ts +26 -18
  12. package/dist/functools/index.js +36 -50
  13. package/dist/globals/decimal.d.ts +142 -0
  14. package/dist/globals/decimal.js +371 -0
  15. package/dist/globals/index.d.ts +19 -31
  16. package/dist/globals/index.js +9 -24
  17. package/dist/i18n/currency.d.ts +24 -0
  18. package/dist/i18n/currency.js +85 -0
  19. package/dist/i18n/date/format.d.ts +16 -0
  20. package/dist/i18n/date/format.js +313 -0
  21. package/dist/i18n/date/index.d.ts +9 -9
  22. package/dist/i18n/date/test.html +72 -0
  23. package/dist/i18n/date/utils.d.ts +3 -3
  24. package/dist/i18n/date/utils.js +6 -28
  25. package/dist/i18n/format.d.ts +2 -2
  26. package/dist/i18n/format.js +1 -1
  27. package/dist/i18n/index.d.ts +1 -0
  28. package/dist/i18n/index.js +1 -0
  29. package/dist/itertools/index.d.ts +52 -24
  30. package/dist/itertools/index.js +4 -32
  31. package/dist/math/index.d.ts +7 -8
  32. package/dist/math/index.js +3 -1
  33. package/dist/operators/index.d.ts +2 -2
  34. package/dist/operators/index.js +0 -6
  35. package/dist/random/index.d.ts +1 -1
  36. package/dist/random/index.js +2 -2
  37. package/dist/types.d.ts +19 -0
  38. package/dist/types.js +1 -0
  39. package/package.json +1 -1
@@ -1,10 +1,10 @@
1
1
  import { Duration, DurationUnit, TDuration } from '../duration.js';
2
2
  import { asDate, formatDate, strftime } from '../format.js';
3
- import { DateParts, daysInMonth, daysInYear, isLeapYear, maxDate, minDate, weeksInYear } from './utils.js';
3
+ import { DateObject, daysInMonth, daysInYear, isLeapYear, maxDate, minDate, weeksInYear } from './utils.js';
4
4
  export { asDate, daysInMonth, daysInYear, formatDate, isLeapYear, maxDate, minDate, strftime, weeksInYear };
5
- declare type DateInput = number | string | Date;
6
- declare type DateLike = DateInput | DateParts | IntlDate;
7
- export { DateInput, DateParts, DateLike };
5
+ export { DateObject };
6
+ export declare type DateInput = number | string | Date;
7
+ export declare type DateLike = DateInput | IntlDate | DateObject;
8
8
  /**
9
9
  * Creates a date tied to a given locale (default system locale) which can be formatted in that locale's language using the native {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis} directly or using a formatting string.
10
10
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl Intl Apis}
@@ -49,7 +49,7 @@ export declare class IntlDate {
49
49
  /** Get the number of days in this date's month. */
50
50
  daysInMonth(): number;
51
51
  /** Get the number of days in this date's year. */
52
- daysInYear(): 366 | 365;
52
+ daysInYear(): number;
53
53
  dayOfYear(): number;
54
54
  /** Returns `true` if this date's year is a leap year. */
55
55
  isLeapYear(): boolean;
@@ -137,7 +137,7 @@ export declare class IntlDate {
137
137
  * Returns an object containing year, month, day-of-month, hours, minutes, seconds, milliseconds.
138
138
  * @returns {Object} An object like `{year, month, date, hours, minutes, seconds, ms}`
139
139
  */
140
- toObject(): DateParts;
140
+ toObject(): DateObject;
141
141
  /** Returns the number of milliseconds since the Unix Epoch (January 1, 1970 UTC) */
142
142
  getTime(): number;
143
143
  /** Alias of {@link IntlDate.getTime} */
@@ -176,7 +176,7 @@ export declare class IntlDate {
176
176
  lt(other: Date | IntlDate): boolean;
177
177
  eq(other: Date | IntlDate): boolean;
178
178
  compare(other: Date | IntlDate): number;
179
- set(values: DateParts): IntlDate;
179
+ set(values: DateObject): IntlDate;
180
180
  get relativeFormat(): Intl.RelativeTimeFormat;
181
181
  relativeTime(n: number, unit?: Intl.RelativeTimeFormatUnit): string;
182
182
  fromNow(unit?: Intl.RelativeTimeFormatUnit): string;
@@ -193,7 +193,7 @@ export declare class IntlDate {
193
193
  static min(...args: any[]): any;
194
194
  inspect(): string;
195
195
  }
196
- export declare function addDuration(date: DateInput, duration: number | TDuration | Duration, exact?: boolean): Date;
196
+ export declare function addDuration(date: DateInput, duration: number | Duration | TDuration, exact?: boolean): Date;
197
197
  /**
198
198
  * Subtract the specified years, months, weeks, days, hours, minutes and seconds from the given date.
199
199
  * @param date
@@ -201,4 +201,4 @@ export declare function addDuration(date: DateInput, duration: number | TDuratio
201
201
  * @param exact
202
202
  * @returns
203
203
  */
204
- export declare function subDuration(date: DateInput, duration: TDuration, exact?: boolean): Date;
204
+ export declare function subDuration(date: DateInput, duration: number | Duration | TDuration, exact?: boolean): Date;
@@ -0,0 +1,72 @@
1
+ <script type="module">
2
+ import { IntlDate } from './index.js'
3
+ window.IntlDate = IntlDate
4
+
5
+ let d = IntlDate.now()
6
+ console.log('toString', d.toString())
7
+
8
+ console.log('%A', d.format('%A'))
9
+ console.log('%a', d.format('%a'))
10
+
11
+ console.log('%%d', d.format('%d'))
12
+ console.log('%-d', d.format('%-d'))
13
+
14
+ console.log('%m', d.format('%m'))
15
+ console.log('%-m', d.format('%-m'))
16
+ console.log('%B', d.format('%B'))
17
+ console.log('%b', d.format('%b'))
18
+ console.log('%bb', d.format('%bb'))
19
+
20
+ console.log('%Y', d.format('%Y'))
21
+ console.log('%y', d.format('%y'))
22
+
23
+ console.log('%N', d.format('%N'))
24
+ console.log('%n', d.format('%n'))
25
+
26
+ console.log('%I', d.format('%I'))
27
+ console.log('%-I', d.format('%-I'))
28
+ console.log('%H', d.format('%H'))
29
+ console.log('%-H', d.format('%-H'))
30
+ console.log('%K', d.format('%K'))
31
+ console.log('%k', d.format('%k'))
32
+
33
+ console.log('%M', d.format('%M'))
34
+ console.log('%-M', d.format('%-M'))
35
+
36
+ console.log('%S', d.format('%S'))
37
+ console.log('%-S', d.format('%-S'))
38
+
39
+ console.log('%P', d.format('%P'))
40
+ console.log('%p', d.format('%p'))
41
+ console.log('%pp', d.format('%pp'))
42
+
43
+ console.log('%Z', d.format('%Z'))
44
+ console.log('%-Z', d.format('%-Z'))
45
+ console.log('%z', d.format('%z'))
46
+ console.log('%-z', d.format('%-z'))
47
+ console.log('%ZZ', d.format('%ZZ'))
48
+ console.log('%-ZZ', d.format('%-ZZ'))
49
+
50
+ console.log('Z', d.format('Z'))
51
+ console.log('ZZ', d.format('ZZ'))
52
+ console.log('z', d.format('z'))
53
+ console.log('zzz', d.format('zzz'))
54
+
55
+ console.log(d.toObject())
56
+ console.log(d.zoneName())
57
+ console.log(d.offset())
58
+ console.log(d.offsetInMinutes())
59
+
60
+ console.log('month starts', d.startOf('month').toDate())
61
+ console.log('month ends', d.endOf('month').toDate())
62
+ console.log('1 day', d.add({ days: 1 }).toDate())
63
+ console.log('60 days', d.add({ days: 60 }).toDate())
64
+ console.log('2 months', d.add({ months: 2 }).toDate())
65
+ console.log('4 months', d.add({ months: 4, weeks: 2 }).toDate())
66
+ console.log('14 days', d.add({ days: 14 }).toDate())
67
+ console.log('2 week', d.add({ weeks: 2 }).toDate())
68
+ console.log('1 month', d.add({ months: 1 }).toDate())
69
+ console.log('1 year', d.add({ years: 1 }).toDate())
70
+
71
+ console.log('-30 years', d.subtract({ years: 30 }, true).toDate())
72
+ </script>
@@ -1,4 +1,4 @@
1
- export declare type DateParts = {
1
+ export declare type DateObject = {
2
2
  year?: number;
3
3
  month?: number;
4
4
  day?: number;
@@ -33,7 +33,7 @@ export declare function isLeapYear(year: number): boolean;
33
33
  * @param year The date year
34
34
  * @returns {number} Number of days in year
35
35
  */
36
- export declare function daysInYear(year: number): 366 | 365;
36
+ export declare function daysInYear(year: number): number;
37
37
  /**
38
38
  * Get the number of days in the given date's month and year.
39
39
  * @param date The specified date
@@ -61,5 +61,5 @@ export declare function weekOfYear(date: string | number | Date, firstDayOfWeek?
61
61
  week: any;
62
62
  year: any;
63
63
  };
64
- export declare function isoWeeksInYear(year: any): number;
64
+ export declare function isoWeeksInYear(year: number): number;
65
65
  export declare function dayOfYear(input: string | number | Date): number;
@@ -1,25 +1,3 @@
1
- import { isInteger, ValueError } from '../../globals/index.js';
2
- function bestBy(arr, by, compare) {
3
- if (arr.length === 0) {
4
- return undefined;
5
- }
6
- return arr.reduce((best, next) => {
7
- const pair = [by(next), next];
8
- if (!best) {
9
- return pair;
10
- }
11
- else if (compare(best[0], pair[0]) === best[0]) {
12
- return best;
13
- }
14
- else {
15
- return pair;
16
- }
17
- }, null)[1];
18
- }
19
- // NUMBERS AND STRINGS
20
- function integerBetween(thing, bottom, top) {
21
- return isInteger(thing) && thing >= bottom && thing <= top;
22
- }
23
1
  /**
24
2
  * Returns x % n but takes the sign of n instead of x
25
3
  * @param x
@@ -138,13 +116,13 @@ function parseZoneInfo(ts, offsetFormat, locale, timeZone) {
138
116
  day: '2-digit',
139
117
  hour: '2-digit',
140
118
  minute: '2-digit',
141
- timeZone
119
+ timeZone,
120
+ timeZoneName: offsetFormat
142
121
  };
143
- const modified = { timeZoneName: offsetFormat, ...intlOpts };
144
- const parsed = new Intl.DateTimeFormat(locale, modified)
122
+ const parsed = new Intl.DateTimeFormat(locale, intlOpts)
145
123
  .formatToParts(date)
146
- .find((m) => m.type.toLowerCase() === 'timezonename');
147
- return parsed ? parsed.value : null;
124
+ .find((m) => m.type === 'timeZoneName');
125
+ return parsed?.value;
148
126
  }
149
127
  // signedOffset('-5', '30') -> -330
150
128
  function signedOffset(offHourStr, offMinuteStr) {
@@ -160,7 +138,7 @@ function signedOffset(offHourStr, offMinuteStr) {
160
138
  export function asNumber(value) {
161
139
  const numericValue = Number(value);
162
140
  if (typeof value === 'boolean' || value === '' || Number.isNaN(numericValue))
163
- throw new ValueError(`Invalid unit value ${value}`);
141
+ throw new Error(`Invalid unit value ${value}`);
164
142
  return numericValue;
165
143
  }
166
144
  function formatOffset(offset, format) {
@@ -18,9 +18,9 @@ export declare function formatDate(formatStr: string, date: Date, locale?: strin
18
18
  /**
19
19
  * Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
20
20
  * @param {string} fmt The format string to apply
21
- * @param {number | Date} date The date value
21
+ * @param {Date} date The date value
22
22
  * @param {?string} locale The locale to use when formatting (default is system locale).
23
23
  * @returns {string} The string representation of date
24
24
  * @see {@link https://strftime.org/ strftime format}
25
25
  */
26
- export declare function strftime(fmt: string, date: number | Date, locale?: any): string;
26
+ export declare function strftime(fmt: string, date: Date, locale?: string): string;
@@ -217,7 +217,7 @@ export function formatDate(formatStr, date, locale) {
217
217
  /**
218
218
  * Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
219
219
  * @param {string} fmt The format string to apply
220
- * @param {number | Date} date The date value
220
+ * @param {Date} date The date value
221
221
  * @param {?string} locale The locale to use when formatting (default is system locale).
222
222
  * @returns {string} The string representation of date
223
223
  * @see {@link https://strftime.org/ strftime format}
@@ -3,5 +3,6 @@
3
3
  *
4
4
  * @module i18n
5
5
  */
6
+ export * from './currency.js';
6
7
  export * from './date/index.js';
7
8
  export { Duration, TDuration, DurationUnit } from './duration.js';
@@ -3,5 +3,6 @@
3
3
  *
4
4
  * @module i18n
5
5
  */
6
+ export * from './currency.js';
6
7
  export * from './date/index.js';
7
8
  export { Duration } from './duration.js';
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * @module Iterators
5
5
  */
6
- import { Function, Iteratee, Predicate } from '../globals/index.js';
7
6
  import { add, Comparer } from '../operators/index.js';
7
+ import { Function, Iteratee, Predicate } from '../types';
8
8
  /**
9
9
  * Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional `func` argument).
10
10
  * @param iterable The iterable to accumulate
@@ -31,7 +31,7 @@ all([2, 4, 6] => (n) => n > 1)
31
31
  ```
32
32
  @see {@link any}
33
33
  */
34
- export declare const all: <T>(iterable: Iterable<T>, fn?: Predicate<T>) => boolean;
34
+ export declare const all: <T>(iterable: Iterable<T>, fn?: Predicate<T, any>) => boolean;
35
35
  /**
36
36
  * Returns true when any of the items in iterable are truthy. An optional predicate function can be used to define what truthiness means for this specific collection.
37
37
  * @param iterable
@@ -50,7 +50,7 @@ any([1, 4, 5] => (n) => n % 2 === 0)
50
50
  ```
51
51
  @see {@link all}
52
52
  */
53
- export declare const any: <T>(iterable: Iterable<T>, fn?: Predicate<T>) => boolean;
53
+ export declare const any: <T>(iterable: Iterable<T>, fn?: Predicate<T, any>) => boolean;
54
54
  export declare function chain<T>(...iterables: Array<Iterable<T>>): Generator<T, void, unknown>;
55
55
  /**
56
56
  * Checks if the `value` is in `collection`.
@@ -81,9 +81,9 @@ export declare function contains(collection: Iterable<any>, value: any): any;
81
81
  * @param predicate A function invoked per element to check if it should be dropped.
82
82
  * @returns Elements
83
83
  */
84
- export declare function dropWhile<T>(iterable: Iterable<T>, predicate: Predicate): Generator<any, any, unknown>;
84
+ export declare function dropWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Generator<T, T, unknown>;
85
85
  export declare function iforEach<T>(iterable: Iterable<T>, fun: Iteratee<T>): void;
86
- export declare function IterableIterator<T = any>(next: any): Iterator<T, any, undefined> & Iterable<T>;
86
+ export declare function IterableIterator<T = any>(next: Iterator<T>['next']): Iterable<T> & Iterator<T>;
87
87
  /**
88
88
  * Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
89
89
  * @template T
@@ -100,13 +100,13 @@ console.log([...enumerate(['hello', 'world'], 1)])
100
100
  // [[1, 'hello'], [2, 'world']]
101
101
  ```
102
102
  */
103
- export declare function enumerate<T = any>(iterable: Iterable<T>, start?: number): Generator<(number | T)[], void, unknown>;
103
+ export declare function enumerate<T>(iterable: Iterable<T>, start?: number): Generator<[number, T], void, unknown>;
104
104
  /**
105
105
  * Returns the first element in an iterable.
106
106
  * @param iterable The iterable to inspect.
107
107
  * @returns The first element found or undefined if iterable is empty.
108
108
  */
109
- export declare function first<T>(iterable: Iterable<T>): any;
109
+ export declare function first<T>(iterable: Iterable<T>): T;
110
110
  /**
111
111
  * Creates a new generator that filters all falsy and empty values from iterable.
112
112
  * @param arr The array to compact
@@ -121,8 +121,8 @@ export declare function icompact<T>(iterable: Iterable<T>): Generator<T, void, u
121
121
  /**
122
122
  * Iterates over elements of collection, producing only those elements where predicate returns a `truthy` value.
123
123
  */
124
- export declare function ifilter<T>(iterable: Iterable<T>, predicate: Predicate): Generator<T, void, unknown>;
125
- export declare function imap<T, TReturn = any>(iterable: Iterable<T>, mapFn: (x: T) => TReturn): Generator<TReturn, void, unknown>;
124
+ export declare function ifilter<T>(iterable: Iterable<T>, predicate: Predicate<T, boolean>): Generator<T, void, unknown>;
125
+ export declare function imap<T, TReturn = any>(iterable: Iterable<T>, mapFn: Predicate<T, TReturn>): Generator<TReturn, void, unknown>;
126
126
  /**
127
127
  * Returns a generator flattening one level of nesting in a list of lists.
128
128
  * @param iterables The iterable to flatten
@@ -134,15 +134,15 @@ export declare function imap<T, TReturn = any>(iterable: Iterable<T>, mapFn: (x:
134
134
  */
135
135
  export declare function iflatten<T = any>(iterables: Iterable<Iterable<T>>): Generator<T, void, unknown>;
136
136
  export declare function flatMap<T>(iterable: Iterable<T>, mapFn?: (x: T) => any): Generator<T, void, unknown>;
137
- export declare function islice<T>(iterable: Iterable<T>, start: number, stop?: number, step?: number): Generator<number | T, number | T, unknown>;
138
- export declare function itake<T>(n: number, iterable: Iterable<T>): Generator<any, void, unknown>;
137
+ export declare function islice<T>(iterable: Iterable<T>, start: number, stop?: number, step?: number): Generator<T, T, unknown>;
138
+ export declare function itake<T>(n: number, iterable: Iterable<T>): Generator<T, void, unknown>;
139
139
  /**
140
140
  * Returns an Iterator from the elements of a collection or the object keys.
141
141
  * @param obj The given collection to iterate over.
142
142
  * @returns An Iterator type.
143
143
  */
144
- export declare function iter<T>(obj: any): Iterator<T>;
145
- export declare function partition<T>(iterable: Iterable<T>, predicate: Predicate<T>): T[][];
144
+ export declare function iter<T>(obj: any): Iterator<T, T>;
145
+ export declare function partition<T>(iterable: Iterable<T>, predicate: Predicate<T, boolean>): [T[], T[]];
146
146
  /**
147
147
  * Returns a generator for a sequence of numbers in the given range, starting from `start` (default 0) to `stop` (not-inclusive) in increments of `step` (default 1).
148
148
  *
@@ -187,23 +187,51 @@ export declare function reduce<T>(iterable: Iterable<T>, reducer: (prev: T, valu
187
187
  * @returns A new array with the elements reversed.
188
188
  */
189
189
  export declare function reversed<T>(iterable: Iterable<T>): T[];
190
- export declare function sorted(args: any[]): any[];
191
- export declare function sorted(args: any[], reverse: boolean): any[];
192
- export declare function sorted(args: any[], reverse: boolean, comp: Comparer): any[];
193
- export declare function sorted(args: any[], key: Function): any[];
194
- export declare function sorted(args: any[], key: Function, reverse: boolean): any[];
195
- export declare function sort(args: any[]): any[];
196
- export declare function sort(args: any[], reverse: boolean): any[];
197
- export declare function sort(args: any[], compareFn: Comparer): any[];
198
- export declare function sort(args: any[], reverse: boolean, compareFn: Comparer): any[];
199
- export declare function take<T>(n: number, iterable: Iterable<T>): any[];
190
+ /**
191
+ * Creates a new sorted list from the elements in the array. This can be called many ways:
192
+ * - sorted([...], true) => reverse order
193
+ * - sorted([...], fn = (x) => any) => using a key fn
194
+ * - sorted([...], fn = (x) => any, true) => reverse order using key
195
+ * - sorted([...], false, (x, y) => number) => using custom compare
196
+ * - sorted([...], fn = (x) => any, true, (x, y) => number) => custom key, reverse and compare
197
+ *
198
+ * If `args` is an Object, returns the sorted keys.
199
+ *
200
+ *
201
+ * @param {(Array | Object)} args
202
+ * @param {(boolean | Function)} [key]
203
+ * @param {(boolean | Comparer)} [reverse]
204
+ * @param {Comparer} [compareFn]
205
+ * @return
206
+ */
207
+ export declare function sorted<T>(args: T[]): T[];
208
+ export declare function sorted<T>(args: T[], reverse: boolean): T[];
209
+ export declare function sorted<T>(args: T[], reverse: boolean, comp: Comparer): T[];
210
+ export declare function sorted<T>(args: T[], key: Function): T[];
211
+ export declare function sorted<T>(args: T[], key: Function, reverse: boolean): T[];
212
+ /**
213
+ * Sort `args` in place. Can be called like this:
214
+ * - sort([...], true) => reverse order
215
+ * - sort([...], (x, y) => number) => using custom compare
216
+ * - sort([...], true, (x, y) => number) => reverse and using custom compare
217
+ *
218
+ * @param {any[]} args
219
+ * @param {(boolean | Comparer)} [reverse]
220
+ * @param {Comparer} [compareFn]
221
+ * @return
222
+ */
223
+ export declare function sort<T>(args: T[]): T[];
224
+ export declare function sort<T>(args: T[], reverse: boolean): T[];
225
+ export declare function sort<T>(args: T[], compareFn: Comparer): T[];
226
+ export declare function sort<T>(args: T[], reverse: boolean, compareFn: Comparer): T[];
227
+ export declare function take<T>(n: number, iterable: Iterable<T>): T[];
200
228
  /**
201
229
  * Returns a generator that takes elements from the iterable as long as the predicate is `true`.
202
230
  * @param iterable The iterable to inspect.
203
231
  * @param predicate A function invoked per element to check if it should be taken.
204
232
  * @returns Elements
205
233
  */
206
- export declare function takeWhile<T>(iterable: Iterable<T>, predicate: Predicate): Generator<T, void, unknown>;
234
+ export declare function takeWhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Generator<T, void, unknown>;
207
235
  /** Returns a generator that calls `fn(index)` for each index < n */
208
236
  /**
209
237
  * Returns a generator that produces `fn(index)` for each `index < n`.
@@ -109,7 +109,6 @@ export function contains(collection, value) {
109
109
  return includes;
110
110
  return any(collection, (x) => eq(value, x));
111
111
  }
112
- const compKey = (compare, key) => (x, y) => compare(key(x), key(y));
113
112
  /**
114
113
  * Returns a generator that drops elements from the iterable as long as the predicate is `true`; afterwards, returns every remaining element. Note, the generator does not produce any output until the predicate first becomes `false`.
115
114
  * @param iterable The iterable to inspect.
@@ -142,13 +141,12 @@ export function iforEach(iterable, fun) {
142
141
  }
143
142
  }
144
143
  export function IterableIterator(next) {
145
- const it = {
144
+ return {
146
145
  next,
147
146
  [Symbol.iterator]() {
148
147
  return this;
149
148
  }
150
149
  };
151
- return it;
152
150
  }
153
151
  /**
154
152
  * Yields elements like `[index, item]` from an iterable. Index starts at zero by default.
@@ -353,26 +351,11 @@ export function reduce(iterable, reducer, initial) {
353
351
  export function reversed(iterable) {
354
352
  return Array.from(iterable).reverse();
355
353
  }
356
- /**
357
- * Creates a new sorted list from the elements in the array. This can be called many ways:
358
- * - sorted([...], true) => reverse order
359
- * - sorted([...], fn = (x) => any) => using a key fn
360
- * - sorted([...], fn = (x) => any, true) => reverse order using key
361
- * - sorted([...], false, (x, y) => number) => using custom compare
362
- * - sorted([...], fn = (x) => any, true, (x, y) => number) => custom key, reverse and compare
363
- *
364
- * If `args` is an Object, returns the sorted keys.
365
- *
366
- *
367
- * @param {(Array | Object)} args
368
- * @param {(boolean | Function)} [key]
369
- * @param {(boolean | Comparer)} [reverse]
370
- * @param {Comparer} [compareFn]
371
- * @return
372
- */
354
+ const compKey = (compare, key) => (x, y) => compare(key(x), key(y));
373
355
  export function sorted(args, key, reverse, compareFn) {
374
- if (isObject(args))
356
+ if (isObject(args)) {
375
357
  args = Object.keys(args);
358
+ }
376
359
  if (typeof key === 'boolean') {
377
360
  if (typeof reverse === 'function') {
378
361
  compareFn = reverse;
@@ -390,17 +373,6 @@ export function sorted(args, key, reverse, compareFn) {
390
373
  copy.reverse();
391
374
  return copy;
392
375
  }
393
- /**
394
- * Sort `args` in place. Can be called like this:
395
- * - sort([...], true) => reverse order
396
- * - sort([...], (x, y) => number) => using custom compare
397
- * - sort([...], true, (x, y) => number) => reverse and using custom compare
398
- *
399
- * @param {any[]} args
400
- * @param {(boolean | Comparer)} [reverse]
401
- * @param {Comparer} [compareFn]
402
- * @return
403
- */
404
376
  export function sort(args, reverse, compareFn) {
405
377
  if (typeof reverse === 'function') {
406
378
  compareFn = reverse;
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * @module Math
5
5
  */
6
- import { Predicate } from '../globals/index.js';
6
+ import { Predicate } from '../types.js';
7
7
  /**
8
8
  * Returns the absolute value of a number. The argument may be a number or an object implementing the `abs()`.
9
9
  * @param x The value.
@@ -43,7 +43,7 @@ export declare const ceil: (x: number) => number;
43
43
  * @param y
44
44
  * @returns
45
45
  */
46
- export declare function divmod(x: number, y: number): number[];
46
+ export declare function divmod(x: number, y: number): [number, number];
47
47
  export declare function log2(x: any): number;
48
48
  export declare function logBase(x: any, y: any): number;
49
49
  /**
@@ -66,7 +66,7 @@ export declare function max<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
66
66
  * Computes the mean of the values in `iterable` and accepts an optional `key` function which is invoked for each element to generate the values to be averaged.
67
67
  * @param {Array<number>|Iterable<T>} iterable
68
68
  * @param {Function} [key] Optional iteratee invoked per element.
69
- * @returns Returns the mean value or `false` if no elements.
69
+ * @returns Returns the mean value or `NaN` if no elements.
70
70
  * @template T
71
71
  * @example
72
72
  ```js
@@ -76,8 +76,8 @@ mean(objects, (o) => o.n)
76
76
  // 5
77
77
  ```
78
78
  */
79
- export declare function mean(iterable: number[]): false | number;
80
- export declare function mean<T>(iterable: Iterable<T>, key?: Predicate<T>): false | number;
79
+ export declare function mean(iterable: number[]): number;
80
+ export declare function mean<T>(iterable: Iterable<T>, key?: Predicate<T>): number;
81
81
  /**
82
82
  * Sums `initial` and the items of an iterable and returns the total.
83
83
  * @param args The elements to add to `initial`.
@@ -92,6 +92,5 @@ sum([1, 2, 3, 4], 5)
92
92
  // 15
93
93
  ```
94
94
  */
95
- export declare function sum(iterable: number[]): number;
96
- export declare function sum<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
97
- export declare function sum<T>(iterable: Iterable<T>, key?: Predicate<T>): T;
95
+ export declare function sum(args: number[]): number;
96
+ export declare function sum<T>(args: Iterable<T>, key?: Predicate<T>): number;
@@ -81,7 +81,9 @@ export function mean(iterable, key = id) {
81
81
  total += key(value);
82
82
  count++;
83
83
  }
84
- return count !== 0 && total / count;
84
+ if (count !== 0)
85
+ return total / count;
86
+ return Number.NaN;
85
87
  }
86
88
  export function sum(args, key = id) {
87
89
  let initial = 0;
@@ -72,7 +72,7 @@ export declare function lte(x: any, y: any): boolean;
72
72
  * @param {*} y
73
73
  * @returns {boolean} `true` if `x` is strictly greater than `y` or `false` otherwise.
74
74
  */
75
- export declare function gt(x: any, y: any): any;
75
+ export declare function gt(x: any, y: any): boolean;
76
76
  /**
77
77
  * Compares two values to check if `x >= y`. If `x` is an object with a `gte` method, it returns `x.gte(y)`, also checks `x.eq(y)` and `x.gt(y)`.
78
78
  * Otherwise it returns the result of `x >= y`.
@@ -145,4 +145,4 @@ export declare function shallowEqual(obj: any, other: any): boolean;
145
145
  * @param other
146
146
  * @returns `true` if the objects are considered equal.
147
147
  */
148
- export declare function deepEqual(obj: any, other: any, checker?: typeof eq, seen?: WeakSet<object>): boolean;
148
+ export declare function deepEqual(obj: any, other: any, checker?: Function, seen?: any): boolean;
@@ -231,12 +231,6 @@ export function shallowEqual(obj, other) {
231
231
  }
232
232
  return false;
233
233
  }
234
- /**
235
- * Performs a recursive comparison of two objects or arrays to check if they have the same keys, length and values. Unlike {@link shallowEqual} this method compares nested values also.
236
- * @param obj
237
- * @param other
238
- * @returns `true` if the objects are considered equal.
239
- */
240
234
  export function deepEqual(obj, other, checker = eq, seen = new WeakSet()) {
241
235
  if (checker(obj, other))
242
236
  return true;
@@ -22,7 +22,7 @@ export declare const randomInt: (min?: number, max?: number) => number;
22
22
  * @returns {T}
23
23
  * @template T
24
24
  */
25
- export declare const choice: <T>(arr: T[]) => T;
25
+ export declare function choice<T>(arr: T[]): T;
26
26
  /**
27
27
  * Returns a random sample of size k from a list of items.
28
28
  * @param {Array} arr
@@ -23,11 +23,11 @@ export const randomInt = (min = 0, max = 1) => Math.round(random(min, max));
23
23
  * @returns {T}
24
24
  * @template T
25
25
  */
26
- export const choice = (arr) => {
26
+ export function choice(arr) {
27
27
  if (!arr?.length)
28
28
  return;
29
29
  return arr[randomInt(0, arr.length - 1)];
30
- };
30
+ }
31
31
  /**
32
32
  * Returns a random sample of size k from a list of items.
33
33
  * @param {Array} arr
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A generic function type with arbitrary arguments and return.
3
+ */
4
+ export declare type Function<TReturn = any> = (...args: any) => TReturn;
5
+ /**
6
+ * An iteratee function used for collection methods.
7
+ * @see {@link Collections.filter}
8
+ * @see {@link Collections.find}
9
+ * @see {@link Collections.forEach}
10
+ * @see {@link Collections.index}
11
+ * @see {@link Collections.map}
12
+ * @see {@link Collections.pick}
13
+ * @see {@link Collections.omit}
14
+ * @see {@link Collections.uniq}
15
+ * @see {@link Collections.sortedUniq}
16
+ * @see {@link Collections.groupBy}
17
+ */
18
+ export declare type Iteratee<T = any, K = any, TResult = any> = (value: T, key?: K, arr?: any) => TResult;
19
+ export declare type Predicate<T = any, TReturn = any> = (value: T) => TReturn;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "atomic-fns",
3
3
  "description": "Like Lodash, but for ESNext and with types. Stop shipping code built for browsers from 2015.",
4
- "version": "0.4.2",
4
+ "version": "1.1.1",
5
5
  "files": [
6
6
  "dist/*"
7
7
  ],