atomic-fns 0.3.2 → 0.3.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.
@@ -107,6 +107,15 @@ findRight([1, 2, 3, 4], (n) => n % 2 === 1)
107
107
  * @see {@link find}
108
108
  */
109
109
  export declare function findRight(arr: any, fn: Iteratee | PropertyKey | Object): any;
110
+ /**
111
+ * Performs an efficient array insert operation in the given array. If the index or the array is invalid, it just returns the given array.
112
+ *
113
+ * @param {Array<*>} arr The given array to insert into
114
+ * @param {number} index The index of the array insert operation.
115
+ * @param {*} value The value to insert in the array at the given `index`.
116
+ * @returns {Array<*>} The given array.
117
+ */
118
+ export declare function insert(arr: any[], index: number, value: any): any[];
110
119
  /**
111
120
  * Creates a function that performs a partial deep comparison between a given object and `shape`, returning `true` if the given object has equivalent property values, else `false`.
112
121
  * @example
@@ -501,7 +510,7 @@ export declare function union(...args: Array<Iterable<any>>): Generator<unknown,
501
510
  *
502
511
  * @param arr The collection to iterate over.
503
512
  * @param [func=id] The iteratee to transform keys.
504
- * @returns Returns the composed aggregate object.
513
+ * @returns Returns the aggregated object.
505
514
  *
506
515
  * @example
507
516
  ```js
@@ -514,6 +523,25 @@ groupBy(['one', 'two', 'three'], 'length')
514
523
  ```
515
524
  */
516
525
  export declare function groupBy(arr: Iterable<any> | Object, func?: Iteratee | PropertyKey): Object;
526
+ /**
527
+ * Similar to {@link groupBy} but it returns a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map Map} object with the results.
528
+ *
529
+ * @param arr The collection to iterate over.
530
+ * @param {Function | PropertyKey} [func=id] The iteratee to transform keys.
531
+ * @returns {Map} Returns the aggregated map object.
532
+ * @template K, V
533
+ *
534
+ * @example
535
+ ```js
536
+ groupByMap([6.1, 4.2, 6.3], Math.floor)
537
+ // => Map { 4: [4.2], 6: [6.1, 6.3] }
538
+
539
+ // The `property` iteratee shorthand.
540
+ groupByMap(['one', 'two', 'three'], 'length')
541
+ // => Map { 3: ['one', 'two'], 5: ['three'] }
542
+ ```
543
+ */
544
+ export declare function groupByMap<K = any, V = any>(arr: Iterable<any> | Object, func?: Iteratee | PropertyKey): Map<K, V[]>;
517
545
  /**
518
546
  * Removes all elements from array that `func` returns truthy for and returns an array of the removed elements.
519
547
  * @param arr The array to remove from.
@@ -156,6 +156,20 @@ export function findRight(arr, fn) {
156
156
  }
157
157
  }
158
158
  }
159
+ /**
160
+ * Performs an efficient array insert operation in the given array. If the index or the array is invalid, it just returns the given array.
161
+ *
162
+ * @param {Array<*>} arr The given array to insert into
163
+ * @param {number} index The index of the array insert operation.
164
+ * @param {*} value The value to insert in the array at the given `index`.
165
+ * @returns {Array<*>} The given array.
166
+ */
167
+ export function insert(arr, index, value) {
168
+ if (!arr || arr.length <= index || index < 0)
169
+ return arr;
170
+ arr.splice(index, 0, value);
171
+ return arr;
172
+ }
159
173
  /**
160
174
  * Creates a function that performs a partial deep comparison between a given object and `shape`, returning `true` if the given object has equivalent property values, else `false`.
161
175
  * @example
@@ -898,7 +912,7 @@ export function* union(...args) {
898
912
  *
899
913
  * @param arr The collection to iterate over.
900
914
  * @param [func=id] The iteratee to transform keys.
901
- * @returns Returns the composed aggregate object.
915
+ * @returns Returns the aggregated object.
902
916
  *
903
917
  * @example
904
918
  ```js
@@ -921,6 +935,35 @@ export function groupBy(arr, func = id) {
921
935
  });
922
936
  return results;
923
937
  }
938
+ /**
939
+ * Similar to {@link groupBy} but it returns a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map Map} object with the results.
940
+ *
941
+ * @param arr The collection to iterate over.
942
+ * @param {Function | PropertyKey} [func=id] The iteratee to transform keys.
943
+ * @returns {Map} Returns the aggregated map object.
944
+ * @template K, V
945
+ *
946
+ * @example
947
+ ```js
948
+ groupByMap([6.1, 4.2, 6.3], Math.floor)
949
+ // => Map { 4: [4.2], 6: [6.1, 6.3] }
950
+
951
+ // The `property` iteratee shorthand.
952
+ groupByMap(['one', 'two', 'three'], 'length')
953
+ // => Map { 3: ['one', 'two'], 5: ['three'] }
954
+ ```
955
+ */
956
+ export function groupByMap(arr, func = id) {
957
+ const results = new Map();
958
+ const useKey = typeof func === 'function' ? func : partial(get, func);
959
+ forEach(arr, (value) => {
960
+ const groupKey = useKey(value);
961
+ const values = results.get(groupKey) || [];
962
+ values.push(value);
963
+ results.set(groupKey, values);
964
+ });
965
+ return results;
966
+ }
924
967
  /**
925
968
  * Removes all elements from array that `func` returns truthy for and returns an array of the removed elements.
926
969
  * @param arr The array to remove from.
@@ -180,6 +180,20 @@ export function findRight(arr, fn: Iteratee | PropertyKey | Object) {
180
180
  }
181
181
  }
182
182
 
183
+ /**
184
+ * Performs an efficient array insert operation in the given array. If the index or the array is invalid, it just returns the given array.
185
+ *
186
+ * @param {Array<*>} arr The given array to insert into
187
+ * @param {number} index The index of the array insert operation.
188
+ * @param {*} value The value to insert in the array at the given `index`.
189
+ * @returns {Array<*>} The given array.
190
+ */
191
+ export function insert(arr: any[], index: number, value) {
192
+ if (!arr || arr.length <= index || index < 0) return arr
193
+ arr.splice(index, 0, value)
194
+ return arr
195
+ }
196
+
183
197
  /**
184
198
  * Creates a function that performs a partial deep comparison between a given object and `shape`, returning `true` if the given object has equivalent property values, else `false`.
185
199
  * @example
@@ -933,7 +947,7 @@ export function* union(...args: Array<Iterable<any>>) {
933
947
  *
934
948
  * @param arr The collection to iterate over.
935
949
  * @param [func=id] The iteratee to transform keys.
936
- * @returns Returns the composed aggregate object.
950
+ * @returns Returns the aggregated object.
937
951
  *
938
952
  * @example
939
953
  ```js
@@ -957,6 +971,39 @@ export function groupBy(arr: Iterable<any> | Object, func: Iteratee | PropertyKe
957
971
  return results
958
972
  }
959
973
 
974
+ /**
975
+ * Similar to {@link groupBy} but it returns a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map Map} object with the results.
976
+ *
977
+ * @param arr The collection to iterate over.
978
+ * @param {Function | PropertyKey} [func=id] The iteratee to transform keys.
979
+ * @returns {Map} Returns the aggregated map object.
980
+ * @template K, V
981
+ *
982
+ * @example
983
+ ```js
984
+ groupByMap([6.1, 4.2, 6.3], Math.floor)
985
+ // => Map { 4: [4.2], 6: [6.1, 6.3] }
986
+
987
+ // The `property` iteratee shorthand.
988
+ groupByMap(['one', 'two', 'three'], 'length')
989
+ // => Map { 3: ['one', 'two'], 5: ['three'] }
990
+ ```
991
+ */
992
+ export function groupByMap<K = any, V = any>(
993
+ arr: Iterable<any> | Object,
994
+ func: Iteratee | PropertyKey = id
995
+ ): Map<K, V[]> {
996
+ const results = new Map<K, V[]>()
997
+ const useKey = typeof func === 'function' ? func : partial(get, func)
998
+ forEach(arr, (value: V) => {
999
+ const groupKey = useKey(value)
1000
+ const values = results.get(groupKey) || []
1001
+ values.push(value)
1002
+ results.set(groupKey, values)
1003
+ })
1004
+ return results
1005
+ }
1006
+
960
1007
  /**
961
1008
  * Removes all elements from array that `func` returns truthy for and returns an array of the removed elements.
962
1009
  * @param arr The array to remove from.
@@ -3,4 +3,43 @@
3
3
  *
4
4
  * @module Date
5
5
  */
6
+ export declare const MINUTE_SECONDS = 60;
7
+ export declare const HOUR_SECONDS: number;
8
+ export declare const DAY_SECONDS: number;
9
+ export declare const WEEK_SECONDS: number;
10
+ export declare const MILLISECONDS = 1000;
11
+ export declare const MINUTE_MILLISECONDS: number;
12
+ export declare const HOUR_MILLISECONDS: number;
13
+ export declare const DAY_MILLISECONDS: number;
14
+ export declare const WEEK_MILLISECONDS: number;
15
+ export declare const MS = "millisecond";
16
+ export declare const S = "second";
17
+ export declare const MIN = "minute";
18
+ export declare const H = "hour";
19
+ export declare const D = "day";
20
+ export declare const W = "week";
21
+ export declare const M = "month";
22
+ export declare const Q = "quarter";
23
+ export declare const Y = "year";
24
+ export declare const DATE = "date";
25
+ export declare const FORMAT_DEFAULT = "YYYY-MM-DDTHH:mm:ssZ";
26
+ export declare const INVALID_DATE_STRING = "Invalid Date";
27
+ export declare const REGEX_PARSE: RegExp;
28
+ export declare const REGEX_FORMAT: RegExp;
6
29
  export declare const now: number;
30
+ export declare type DatetimeOptions = {
31
+ year: number;
32
+ month?: number;
33
+ day?: number;
34
+ hour?: number;
35
+ minute?: number;
36
+ second?: number;
37
+ millisecond?: number;
38
+ locale?: string;
39
+ utc?: boolean;
40
+ };
41
+ export declare class Datetime {
42
+ #private;
43
+ constructor(opts?: string | number | Date | DatetimeOptions);
44
+ format(locale?: string, opts?: Intl.DateTimeFormatOptions): string;
45
+ }
@@ -3,4 +3,69 @@
3
3
  *
4
4
  * @module Date
5
5
  */
6
+ import { get } from '../globals';
7
+ // Constants
8
+ export const MINUTE_SECONDS = 60;
9
+ export const HOUR_SECONDS = MINUTE_SECONDS * 60;
10
+ export const DAY_SECONDS = HOUR_SECONDS * 24;
11
+ export const WEEK_SECONDS = DAY_SECONDS * 7;
12
+ export const MILLISECONDS = 1e3;
13
+ export const MINUTE_MILLISECONDS = MINUTE_SECONDS * MILLISECONDS;
14
+ export const HOUR_MILLISECONDS = HOUR_SECONDS * MILLISECONDS;
15
+ export const DAY_MILLISECONDS = DAY_SECONDS * MILLISECONDS;
16
+ export const WEEK_MILLISECONDS = WEEK_SECONDS * MILLISECONDS;
17
+ // English locales
18
+ export const MS = 'millisecond';
19
+ export const S = 'second';
20
+ export const MIN = 'minute';
21
+ export const H = 'hour';
22
+ export const D = 'day';
23
+ export const W = 'week';
24
+ export const M = 'month';
25
+ export const Q = 'quarter';
26
+ export const Y = 'year';
27
+ export const DATE = 'date';
28
+ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ';
29
+ export const INVALID_DATE_STRING = 'Invalid Date';
30
+ // regex
31
+ export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/;
32
+ export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g;
6
33
  export const now = new Date().getTime();
34
+ function parseDate(date) {
35
+ if (date === null)
36
+ return new Date(NaN);
37
+ if (!date)
38
+ return new Date();
39
+ if (date instanceof Date)
40
+ return new Date(date);
41
+ const utc = date.utc;
42
+ if (typeof date === 'string' && !/Z$/i.test(date)) {
43
+ const d = date.match(REGEX_PARSE);
44
+ if (d) {
45
+ const m = d[2] - 1 || 0;
46
+ const ms = (d[7] || '0').slice(0, 3);
47
+ if (utc) {
48
+ return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
49
+ }
50
+ return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
51
+ }
52
+ }
53
+ return new Date(date);
54
+ }
55
+ export class Datetime {
56
+ #date = new Date(); // defaults to now
57
+ #UTC = false; // use current locale by default
58
+ constructor(opts) {
59
+ this.#date = parseDate(opts);
60
+ this.#UTC = get('utc', opts, false);
61
+ // this.init()
62
+ }
63
+ // Display
64
+ // Format
65
+ format(locale, opts) {
66
+ if (opts && opts.timeZone === undefined && this.#UTC) {
67
+ opts.timeZone = 'utc';
68
+ }
69
+ return this.#date.toLocaleDateString(locale, opts);
70
+ }
71
+ }
@@ -4,4 +4,125 @@
4
4
  * @module Date
5
5
  */
6
6
 
7
+ import { get } from '../globals'
8
+
9
+ // Constants
10
+ export const MINUTE_SECONDS = 60
11
+ export const HOUR_SECONDS = MINUTE_SECONDS * 60
12
+ export const DAY_SECONDS = HOUR_SECONDS * 24
13
+ export const WEEK_SECONDS = DAY_SECONDS * 7
14
+
15
+ export const MILLISECONDS = 1e3
16
+ export const MINUTE_MILLISECONDS = MINUTE_SECONDS * MILLISECONDS
17
+ export const HOUR_MILLISECONDS = HOUR_SECONDS * MILLISECONDS
18
+ export const DAY_MILLISECONDS = DAY_SECONDS * MILLISECONDS
19
+ export const WEEK_MILLISECONDS = WEEK_SECONDS * MILLISECONDS
20
+
21
+ // English locales
22
+ export const MS = 'millisecond'
23
+ export const S = 'second'
24
+ export const MIN = 'minute'
25
+ export const H = 'hour'
26
+ export const D = 'day'
27
+ export const W = 'week'
28
+ export const M = 'month'
29
+ export const Q = 'quarter'
30
+ export const Y = 'year'
31
+ export const DATE = 'date'
32
+
33
+ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
34
+
35
+ export const INVALID_DATE_STRING = 'Invalid Date'
36
+
37
+ // regex
38
+ export const REGEX_PARSE =
39
+ /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/
40
+ export const REGEX_FORMAT =
41
+ /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
42
+
7
43
  export const now = new Date().getTime()
44
+
45
+ export type DatetimeOptions = {
46
+ year: number
47
+ month?: number
48
+ day?: number
49
+ hour?: number
50
+ minute?: number
51
+ second?: number
52
+ millisecond?: number
53
+ locale?: string
54
+ utc?: boolean
55
+ }
56
+
57
+ function parseDate(date) {
58
+ if (date === null) return new Date(NaN)
59
+ if (!date) return new Date()
60
+ if (date instanceof Date) return new Date(date)
61
+ const utc = date.utc
62
+ if (typeof date === 'string' && !/Z$/i.test(date)) {
63
+ const d = date.match(REGEX_PARSE) as any
64
+ if (d) {
65
+ const m = d[2] - 1 || 0
66
+ const ms = (d[7] || '0').slice(0, 3)
67
+ if (utc) {
68
+ return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
69
+ }
70
+ return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
71
+ }
72
+ }
73
+ return new Date(date)
74
+ }
75
+
76
+ export class Datetime {
77
+ #date = new Date() // defaults to now
78
+ #UTC = false // use current locale by default
79
+
80
+ constructor(opts?: string | number | Date | DatetimeOptions) {
81
+ this.#date = parseDate(opts)
82
+ this.#UTC = get('utc', opts, false)
83
+ // this.init()
84
+ }
85
+
86
+ // Display
87
+ // Format
88
+ format(locale?: string, opts?: Intl.DateTimeFormatOptions) {
89
+ if (opts && opts.timeZone === undefined && this.#UTC) {
90
+ opts.timeZone = 'utc'
91
+ }
92
+ return this.#date.toLocaleDateString(locale, opts)
93
+ }
94
+
95
+ // Time from now
96
+ // Time from X
97
+ // Difference
98
+
99
+ // Parsing
100
+ // Parse String + Date Format
101
+ // Parse String + Time Format
102
+ // Parse String + Format + locale
103
+
104
+ // Getter/Setters
105
+ // Hour/Minute/Second/Millis
106
+ // Day of Month
107
+ // Day of Week
108
+ // Day of Year
109
+ // Week of Year
110
+ // Days in Month
111
+ // Weeks in Year
112
+ // max of two dates
113
+ // min of two dates
114
+
115
+ // Manipulate
116
+ // Add
117
+ // Subtract
118
+ // Start of Time
119
+ // End of Time
120
+
121
+ // Query
122
+ // Is Before
123
+ // Is Same
124
+ // Is After
125
+ // Is Between
126
+ // Is Leap Year
127
+ // Is a Date
128
+ }
package/core/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from './collections/index.js';
2
- export * from './datetime/index.js';
3
2
  export * from './functools/index.js';
4
3
  export * from './globals/index.js';
5
4
  export * from './itertools/index.js';
package/core/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from './collections/index.js';
2
- export * from './datetime/index.js';
3
2
  export * from './functools/index.js';
4
3
  export * from './globals/index.js';
5
4
  export * from './itertools/index.js';
package/core/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from './collections/index.js'
2
- export * from './datetime/index.js'
3
2
  export * from './functools/index.js'
4
3
  export * from './globals/index.js'
5
4
  export * from './itertools/index.js'
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.3.2",
4
+ "version": "0.3.3",
5
5
  "main": "core/index.js",
6
6
  "type": "module",
7
7
  "types": "core/index.d.ts",
File without changes