atomic-fns 0.4.3 → 1.1.2

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.
@@ -1,12 +1,12 @@
1
- import { Decimal } from '../globals/decimal.js';
1
+ import { Decimal } from '../decimal/index.js';
2
2
  /**
3
3
  * Currency represents a local currency amount that can be formatted using the `Intl` apis.
4
4
  */
5
5
  export declare class Currency extends Decimal {
6
- locale: string;
7
- currency: string;
8
- numberFormat: Intl.NumberFormat;
9
- options: Intl.NumberFormatOptions;
6
+ private readonly locale;
7
+ private readonly currency;
8
+ private readonly numberFormat;
9
+ private readonly options;
10
10
  constructor(amount: any, currency: string, locale?: string, opts?: Intl.NumberFormatOptions);
11
11
  clone(n: any): Currency;
12
12
  negated(): Currency;
@@ -1,6 +1,6 @@
1
1
  import { defaults } from '../collections/index.js';
2
2
  import { isObject } from '../globals/index.js';
3
- import { Decimal } from '../globals/decimal.js';
3
+ import { Decimal } from '../decimal/index.js';
4
4
  /**
5
5
  * Currency represents a local currency amount that can be formatted using the `Intl` apis.
6
6
  */
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Returns a new Date from the given arguments.
3
+ * @param {string | number | Date} date The date value
4
+ * @param {?boolean} [utc=false] Interprets the given value as a UTC date
5
+ * @returns {Date} The new date object
6
+ */
7
+ export declare function getDate(date: string | number | Date, utc?: boolean): Date;
8
+ /**
9
+ * Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
10
+ * @param {string} fmt The format string to apply
11
+ * @param {number | Date} date The date value
12
+ * @param {?string} locale The locale to use when formatting (default is system locale).
13
+ * @returns {string} The string representation of date
14
+ * @see {@link https://strftime.org/ strftime format}
15
+ */
16
+ export declare function format(fmt: string, date: number | Date, locale?: any): string;
@@ -0,0 +1,313 @@
1
+ const NATIVE_DATE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/;
2
+ /**
3
+ * Returns a new Date from the given arguments.
4
+ * @param {string | number | Date} date The date value
5
+ * @param {?boolean} [utc=false] Interprets the given value as a UTC date
6
+ * @returns {Date} The new date object
7
+ */
8
+ export function getDate(date, utc = false) {
9
+ if (date === null)
10
+ return new Date(NaN);
11
+ if (!date)
12
+ return new Date();
13
+ if (date instanceof Date)
14
+ return new Date(date);
15
+ if (typeof date === 'string' && !/Z$/i.test(date)) {
16
+ const d = date.match(NATIVE_DATE);
17
+ if (d) {
18
+ const m = d[2] - 1 || 0;
19
+ const ms = (d[7] || '0').slice(0, 3);
20
+ if (utc) {
21
+ return new Date(Date.UTC(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms));
22
+ }
23
+ return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
24
+ }
25
+ }
26
+ return new Date(date);
27
+ }
28
+ const n = 'numeric';
29
+ const s = 'short';
30
+ const l = 'long';
31
+ const dd = '2-digit';
32
+ const TOKEN_MAP = {
33
+ // weekday
34
+ '%A': { weekday: l },
35
+ '%a': { weekday: s },
36
+ '%aa': { weekday: n },
37
+ // day
38
+ '%d': { day: dd },
39
+ '%-d': { day: n },
40
+ // month
41
+ '%m': { month: dd },
42
+ '%-m': { month: n },
43
+ '%B': { month: l },
44
+ '%b': { month: s },
45
+ '%bb': { month: n },
46
+ // year
47
+ '%y': { year: n },
48
+ '%Y': { year: dd },
49
+ // hour
50
+ '%K': { hourCycle: 'h11', hour: dd },
51
+ '%I': { hourCycle: 'h12', hour: dd },
52
+ '%-I': { hourCycle: 'h12', hour: n },
53
+ '%H': { hourCycle: 'h23', hour: dd },
54
+ '%-H': { hourCycle: 'h23', hour: n },
55
+ '%k': { hourCycle: 'h24', hour: n },
56
+ // minutes
57
+ '%M': { minute: dd },
58
+ '%-M': { minute: n },
59
+ // seconds
60
+ '%S': { second: dd },
61
+ '%-S': { second: n },
62
+ // day period
63
+ '%P': { dayPeriod: l },
64
+ '%p': { dayPeriod: s },
65
+ '%pp': { dayPeriod: 'narrow' },
66
+ // era
67
+ '%N': { era: l, year: dd },
68
+ '%n': { era: s, year: dd },
69
+ '%nn': { era: n, year: 'narrow' },
70
+ // timeZoneName
71
+ '%Z': { timeZoneName: 'longGeneric', year: dd },
72
+ '%-Z': { timeZoneName: 'shortGeneric', year: dd },
73
+ '%z': { timeZoneName: 'longOffset', year: dd },
74
+ '%-z': { timeZoneName: 'shortOffset', year: dd },
75
+ '%ZZ': { timeZoneName: l, year: dd },
76
+ '%-ZZ': { timeZoneName: s, year: dd }
77
+ };
78
+ const HOUR_PARTS = { '%I': 1, '%-I': 1, '%K': 1, '%k': 1 };
79
+ const ERA_PARTS = { '%n': 1, '%N': 1, '%nn': 1 };
80
+ const TZ_PARTS = { '%ZZ': 1, '%-ZZ': 1, '%-z': 1, '%z': 1, '%-Z': 1, '%Z': 1 };
81
+ const formatter = (locale, opts) => new Intl.DateTimeFormat(locale, opts);
82
+ /**
83
+ * Formats a date using a formatting string in the {@link https://strftime.org/ strftime} format, in any given locale.
84
+ * @param {string} fmt The format string to apply
85
+ * @param {number | Date} date The date value
86
+ * @param {?string} locale The locale to use when formatting (default is system locale).
87
+ * @returns {string} The string representation of date
88
+ * @see {@link https://strftime.org/ strftime format}
89
+ */
90
+ export function format(fmt, date, locale) {
91
+ const results = [];
92
+ let dayPeriod = '';
93
+ let dayPeriodIndex = -1;
94
+ for (let i = 0; i < fmt.length; i++) {
95
+ let part = fmt[i];
96
+ if (part === '%') {
97
+ // check for escaped '%'
98
+ if (fmt[++i] === '%') {
99
+ results.push('%');
100
+ continue;
101
+ }
102
+ if (fmt[i] === '-') {
103
+ part += fmt[i++];
104
+ }
105
+ part += fmt[i];
106
+ // add any repeated characters that come after
107
+ while (i < fmt.length - 1 && fmt[i + 1] === fmt[i])
108
+ part += fmt[++i];
109
+ if (HOUR_PARTS[part]) {
110
+ const res = formatter(locale, TOKEN_MAP[part]).format(date).split(' ');
111
+ // save the AM/PM for the correct spot
112
+ results.push(res[0]);
113
+ dayPeriod = res[1];
114
+ continue;
115
+ }
116
+ if (part === '%P' || part === '%p') {
117
+ // leave this flag in place for later
118
+ dayPeriodIndex = results.length;
119
+ results.push(part);
120
+ continue;
121
+ }
122
+ if (ERA_PARTS[part] || TZ_PARTS[part]) {
123
+ // the formatting includes the date/year so we take what comes after
124
+ const res = formatter(locale, TOKEN_MAP[part]).format(date).split(' ');
125
+ results.push(res.slice(1).join(' '));
126
+ continue;
127
+ }
128
+ // format any other parts
129
+ let res = formatter(locale, TOKEN_MAP[part]).format(date);
130
+ if (part === '%S' && res.length === 1)
131
+ res = '0' + res;
132
+ results.push(res);
133
+ }
134
+ else {
135
+ // output as literal
136
+ results.push(part);
137
+ }
138
+ }
139
+ // insert the AM/PM at the correct spot
140
+ if (dayPeriodIndex >= 0)
141
+ results[dayPeriodIndex] = dayPeriod || '';
142
+ return results.join('');
143
+ }
144
+ const DATETIME_DEFAULT = {};
145
+ /** 10/14/1983 */
146
+ const DATE_SHORT = {
147
+ year: n,
148
+ month: n,
149
+ day: n
150
+ };
151
+ /** Oct 14, 1983 */
152
+ const DATE_MED = {
153
+ year: n,
154
+ month: s,
155
+ day: n
156
+ };
157
+ /** Fri, Oct 14, 1983 */
158
+ const DATE_MED_WITH_WEEKDAY = {
159
+ year: n,
160
+ month: s,
161
+ day: n,
162
+ weekday: s
163
+ };
164
+ /** October 14, 1983 */
165
+ const DATE_FULL = {
166
+ year: n,
167
+ month: l,
168
+ day: n
169
+ };
170
+ /** Tuesday, October 14, 1983 */
171
+ const DATE_HUGE = {
172
+ year: n,
173
+ month: l,
174
+ day: n,
175
+ weekday: l
176
+ };
177
+ /** 09:30 AM */
178
+ const TIME_SIMPLE = {
179
+ hour: n,
180
+ minute: n
181
+ };
182
+ /** 09:30:23 AM */
183
+ const TIME_WITH_SECONDS = {
184
+ hour: n,
185
+ minute: n,
186
+ second: n
187
+ };
188
+ /** 09:30:23 AM EDT */
189
+ const TIME_WITH_SHORT_OFFSET = {
190
+ hour: n,
191
+ minute: n,
192
+ second: n,
193
+ timeZoneName: s
194
+ };
195
+ /** 09:30:23 AM Eastern Daylight Time */
196
+ const TIME_WITH_LONG_OFFSET = {
197
+ hour: n,
198
+ minute: n,
199
+ second: n,
200
+ timeZoneName: l
201
+ };
202
+ /** 09:30 */
203
+ const TIME_24_SIMPLE = {
204
+ hour: n,
205
+ minute: n,
206
+ hourCycle: 'h23'
207
+ };
208
+ /** 09:30:23 */
209
+ const TIME_24_WITH_SECONDS = {
210
+ hour: n,
211
+ minute: n,
212
+ second: n,
213
+ hourCycle: 'h23'
214
+ };
215
+ /** 09:30:23 EDT */
216
+ const TIME_24_WITH_SHORT_OFFSET = {
217
+ hour: n,
218
+ minute: n,
219
+ second: n,
220
+ hourCycle: 'h23',
221
+ timeZoneName: s
222
+ };
223
+ /** 09:30:23 Eastern Daylight Time */
224
+ const TIME_24_WITH_LONG_OFFSET = {
225
+ hour: n,
226
+ minute: n,
227
+ second: n,
228
+ hourCycle: 'h23',
229
+ timeZoneName: l
230
+ };
231
+ /** 10/14/1983, 9:30 AM */
232
+ const DATETIME_SHORT = {
233
+ year: n,
234
+ month: n,
235
+ day: n,
236
+ hour: n,
237
+ minute: n
238
+ };
239
+ /** 10/14/1983, 9:30:33 AM */
240
+ const DATETIME_SHORT_WITH_SECONDS = {
241
+ year: n,
242
+ month: n,
243
+ day: n,
244
+ hour: n,
245
+ minute: n,
246
+ second: n
247
+ };
248
+ /** Oct 14, 1983, 9:30 AM */
249
+ const DATETIME_MED = {
250
+ year: n,
251
+ month: s,
252
+ day: n,
253
+ hour: n,
254
+ minute: n
255
+ };
256
+ /** Oct 14, 1983, 9:30:33 AM */
257
+ const DATETIME_MED_WITH_SECONDS = {
258
+ year: n,
259
+ month: s,
260
+ day: n,
261
+ hour: n,
262
+ minute: n,
263
+ second: n
264
+ };
265
+ /** Fri, 14 Oct 1983, 9:30 AM */
266
+ const DATETIME_MED_WITH_WEEKDAY = {
267
+ year: n,
268
+ month: s,
269
+ day: n,
270
+ weekday: s,
271
+ hour: n,
272
+ minute: n
273
+ };
274
+ /** October 14, 1983, 9:30 AM EDT */
275
+ const DATETIME_FULL = {
276
+ year: n,
277
+ month: l,
278
+ day: n,
279
+ hour: n,
280
+ minute: n,
281
+ timeZoneName: s
282
+ };
283
+ /** October 14, 1983, 9:30:33 AM EDT */
284
+ const DATETIME_FULL_WITH_SECONDS = {
285
+ year: n,
286
+ month: l,
287
+ day: n,
288
+ hour: n,
289
+ minute: n,
290
+ second: n,
291
+ timeZoneName: s
292
+ };
293
+ /** Friday, October 14, 1983, 9:30 AM Eastern Daylight Time */
294
+ const DATETIME_HUGE = {
295
+ year: n,
296
+ month: l,
297
+ day: n,
298
+ weekday: l,
299
+ hour: n,
300
+ minute: n,
301
+ timeZoneName: l
302
+ };
303
+ /** Friday, October 14, 1983, 9:30:33 AM Eastern Daylight Time */
304
+ const DATETIME_HUGE_WITH_SECONDS = {
305
+ year: n,
306
+ month: l,
307
+ day: n,
308
+ weekday: l,
309
+ hour: n,
310
+ minute: n,
311
+ second: n,
312
+ timeZoneName: l
313
+ };
@@ -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>
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './collections/index.js';
2
+ export * from './decimal/index.js';
2
3
  export * from './functools/index.js';
3
4
  export * from './globals/index.js';
4
5
  export * from './i18n/index.js';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './collections/index.js';
2
+ export * from './decimal/index.js';
2
3
  export * from './functools/index.js';
3
4
  export * from './globals/index.js';
4
5
  export * from './i18n/index.js';
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * This module includes functions that deal with or produce iterators.
3
3
  *
4
- * @module Iterators
4
+ * @module itertools
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
@@ -72,7 +72,7 @@ contains('abcd', 'bc')
72
72
  // true
73
73
  ```
74
74
  *
75
- * @see {@link Collections.find}
75
+ * @see {@link collections.find}
76
76
  */
77
77
  export declare function contains(collection: Iterable<any>, value: any): any;
78
78
  /**
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * This module includes functions that deal with or produce iterators.
3
3
  *
4
- * @module Iterators
4
+ * @module itertools
5
5
  */
6
6
  import { call, isEmpty, isObject, notNull, ValueError } from '../globals/index.js';
7
7
  import { add, bool, compare, eq, id } from '../operators/index.js';
@@ -99,7 +99,7 @@ contains('abcd', 'bc')
99
99
  // true
100
100
  ```
101
101
  *
102
- * @see {@link Collections.find}
102
+ * @see {@link collections.find}
103
103
  */
104
104
  export function contains(collection, value) {
105
105
  if (!collection)
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Math and algebraic operations.
3
3
  *
4
- * @module Math
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.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Math and algebraic operations.
3
3
  *
4
- * @module Math
4
+ * @module math
5
5
  */
6
6
  import { call } from '../globals/index.js';
7
7
  import { reduce } from '../itertools/index.js';
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Generic operations for both values and objects.
3
3
  *
4
- * @module Operators
4
+ * @module operators
5
5
  */
6
6
  /** This is the identity function. It always returns the same value that was passed in */
7
7
  export declare const id: (x: any) => any;
@@ -35,8 +35,8 @@ export declare type Comparer<T = any> = (x: T, y: T) => number;
35
35
  * @param {*} y Other value to compare
36
36
  * @returns {number} The comparison result
37
37
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
38
- * @see {@link Operators.eq}
39
- * @see {@link Operators.lt}
38
+ * @see {@link operators.eq}
39
+ * @see {@link operators.lt}
40
40
  */
41
41
  export declare function compare(x: any, y: any): number;
42
42
  /**
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Generic operations for both values and objects.
3
3
  *
4
- * @module Operators
4
+ * @module operators
5
5
  */
6
6
  import { call, isObject } from '../globals/index.js';
7
7
  /** This is the identity function. It always returns the same value that was passed in */
@@ -36,8 +36,8 @@ export const isinstance = (x, y) => x instanceof y;
36
36
  * @param {*} y Other value to compare
37
37
  * @returns {number} The comparison result
38
38
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
39
- * @see {@link Operators.eq}
40
- * @see {@link Operators.lt}
39
+ * @see {@link operators.eq}
40
+ * @see {@link operators.lt}
41
41
  */
42
42
  export function compare(x, y) {
43
43
  if (x === y || eq(x, y))
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * This module implements pseudo-random generators for various distributions.
3
3
  *
4
- * @module Random
4
+ * @module random
5
5
  */
6
6
  /**
7
7
  * Returns a random float between a [min, max) value (max exclusive).
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * This module implements pseudo-random generators for various distributions.
3
3
  *
4
- * @module Random
4
+ * @module random
5
5
  */
6
6
  import { isObject } from '../globals/index.js';
7
7
  /**
@@ -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.3",
4
+ "version": "1.1.2",
5
5
  "files": [
6
6
  "dist/*"
7
7
  ],
@@ -67,5 +67,5 @@
67
67
  "bugs": {
68
68
  "url": "https://github.com/adrian2x/atomic-fns/issues"
69
69
  },
70
- "homepage": "https://github.com/adrian2x/atomic-fns#readme"
70
+ "homepage": "https://atomic-fns.dev"
71
71
  }